Dockerfile, Custom Images, and Environment Variables Explained for Beginners
Docker is one of the most important technologies in modern DevOps,
cloud computing, and software deployment. After learning Docker Images and
Containers, the next step is understanding Dockerfiles, building custom
images, and using environment variables.
Learn Dockerfile, custom Docker image creation, and environment variables
with practical examples. A beginner-friendly Docker guide for DevOps and
System Engineers.
In this guide, you'll learn these concepts with practical
examples.
What is a Dockerfile?
A Dockerfile is a text file that contains instructions for
building a Docker image. Instead of manually configuring containers every
time, you can define all steps inside a Dockerfile and create reproducible
images.
Example:
dockerfile FROM nginx COPY index.html /usr/share/nginx/html/index.html
In this example:
- * FROM nginx uses the Nginx image as the base image.
- * COPY copies a webpage into the Nginx web server directory.
Docker reads these instructions and builds a custom image.
Common Dockerfile Instructions
FROM
- Specifies the base image.
- FROM ubuntu
WORKDIR
Sets the working directory inside
the container.
- WORKDIR /app
COPY
Copies files from the host machine
into the container.
- COPY . .
RUN
Executes commands during image creation.
- RUN npm install
CMD
Defines the default command executed when
the container starts.
- CMD ["node","server.js"]
Building Custom Docker Images
Let's create a
simple custom image.
Project Structure
text project/ │ ├── Dockerfile ├── index.html
Dockerfile
- FROM nginx
- COPY index.html /usr/share/nginx/html/index.html
Build the Image
Run the following command inside the project directory:
- docker build -t mywebsite .
Explanation:
- docker build creates a Docker image.
- -t assigns a tag or image name.
- mywebsite is the image name.
- . represents the current directory.
Verify the Image
- docker images
You should see:
- REPOSITORY
- mywebsite
Run the Custom Image
- docker run -d -p 8080:80 mywebsite
Open:
- http://localhost:8080
Your custom webpage should now be visible in the browser.
What Are Environment Variables?
Environment variables allow you to pass configuration values to
containers without modifying application code.
This makes
applications easier to manage and deploy.
Example:
- docker run -e APP_NAME=HackingTruth nginx
Here:
- APP_NAME=HackingTruth
is an environment variable available inside the container.
Multiple Environment Variables
docker run \ -e DB_HOST=localhost \ -e DB_USER=root \ -e DB_PASSWORD=password \ myapp
This approach is commonly used for:
- * Database credentials
- * API keys
- * Application settings
- * Deployment configuration
Defining Environment Variables in Dockerfile
You
can also define variables directly inside a Dockerfile.
- FROM ubuntu
- ENV APP_NAME=HackingTruth
- ENV VERSION=1.0
Build the image:
- docker build -t myapp .
Run the container:
- docker run myapp
The environment variables will be available inside the container.
Why Use Dockerfiles and Environment Variables?
Dockerfiles provide:
- * Consistent deployments
- * Reproducible environments
- * Easier automation
Environment variables provide:
- * Better configuration management
- * Improved security practices
- * Easier deployment across environments
Together, they form the foundation of modern containerized
applications.
Conclusion
Understanding Dockerfiles, custom image creation, and environment
variables is essential for anyone learning Docker, DevOps, Cloud Computing, or
System Administration.
By mastering these concepts, you'll be able
to create reusable Docker images, automate deployments, and configure
applications efficiently. These skills are widely used in real-world
production environments and are frequently asked about in DevOps and System
Engineer interviews.













































