Docker is a powerful containerization technology that allows you to package and distribute applications along with their dependencies in a consistent and portable way. One of the key components of creating a Docker image is the Dockerfile, which is a script that contains instructions for building the image.
In this hands-on article, we will explore the basic Dockerfile commands that you need to know to create efficient and effective Docker images.
Essential Commands
1. FROM
: Choosing the base image
The FROM
command specifies the base image for your Docker image. It’s the starting point for your image and typically represents the operating system and runtime environment.
FROM nginx:stable
2. LABEL
: Adding metadata
The LABEL
command adds metadata to the image, such as version information, author, and description. It’s helpful for documentation and organization.
LABEL maintainer="Your Name <your.name@example.com>"
3. ENV
: Setting environment variables
With the ENV
command, we establish environment variables within the container. These variables become accessible to the applications running within the container.
ENV DB_HOST=localhost DB_PORT=5432
4. RUN
: Executing Commands
The RUN
command executes shell commands within the container during the image build. It’s often used to install software packages and configure the environment.
RUN apt-get update && apt-get install -y nginx
5. COPY
and ADD
: Adding files and directories
The COPY
and ADD
commands copy files and directories from the host into the container. COPY
is used for straightforward file copying, while ADD
can also handle URL downloads and extraction of compressed archives.
COPY ./app /app
6. WORKDIR
: Setting the working directory
The WORKDIR
command designates the working directory within the container. Subsequent commands execute within the context of this directory.
WORKDIR /app
7. EXPOSE
: Exposing ports
The EXPOSE
command informs Docker that the container will listen on specified ports at runtime.
EXPOSE 80
8. CMD
: Defining the default command
The CMD
command specifies the default command to run when the container starts. It provides the default behavior for the container
CMD ["nginx", "-g", "daemon off;"]
9. ENTRYPOINT
: Run a command
It configures the container to run as an executable, providing a way to set a default command and accept additional parameters.
ENTRYPOINT ["executable", "param1", "param2"]
10. VOLUME
: Mount a volume
It creates a mount point for external volumes, allowing data to persist outside the container.
VOLUME ["/data"]
Dockerfile Example
The Dockerfile below uses nginx:stable image, sets a maintainer label, defines environment variables, copies an app folder to the container, sets the working directory, exposes port 80, and runs nginx server.
FROM nginx:stable
LABEL maintainer="John Doe <john.doe@example.com>"
ENV DB_HOST=localhost DB_PORT=5432
COPY ./app /app
WORKDIR /app
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Run a command to build an image tagged my-website:0.0.1
docker build -t my-website:0.0.1 .
- Create a container and run the image
docker run --rm --name my-website -d -p 8080:80 my-website:0.0.1
- Browse the url http://localhost:8080 and you should get a response as shown below.

Best Practices
When working with Dockerfiles, it’s important to follow best practices to create efficient and secure Docker images. Some best practices include:
- Keep your images minimal by removing unnecessary files.
- Use the official base images whenever possible.
- Minimize the number of layers in your image by grouping related commands.
- Use environment variables for configuration to make your images more flexible.