Dockerfile Directives
Dockerfile directives are the set of commands that you can use to define the steps needed to create a Docker image. Each directive in a Dockerfile performs a specific function, such as specifying a base image, installing dependencies, or configuring how the container should behave when it runs. These directives are written in the Dockerfile in a specific order, and each one has a purpose in building the image.
Here are the main Dockerfile directives you’ll encounter:
1. FROM
The the FROM
directive sets the base image for the Docker image you are building. Every Dockerfile must start with a FROM
directive.
Syntax:
Example:
This uses the official ubuntu
image, version 20.04
, as the base for the Docker image.
2. LABEL
The the LABEL
directive adds metadata to the image. Labels are key-value pairs that can describe information such as the image's maintainer, version, and more.
Syntax:
Example:
This adds metadata specifying the maintainer and version of the image.
3. RUN
The The RUN
directive executes commands in a new layer on top of the current image and commits the results. It's typically used to install dependencies or make modifications to the image (e.g., running package managers like apt-get
, npm
, etc.).
Syntax:
Example:
This updates the package lists for Ubuntu and installs Python 3.
4. COPY
The The COPY
directive copies files and directories from the host machine into the Docker image. The syntax allows you to specify the source and destination paths.
Syntax:
Example:
This copies everything from the ./app
directory on the host machine into /usr/src/app
inside the image.
5. ADD
Similar to COPY
, the ADD
directive copies files and directories into the image, but it also has additional features, such as automatically extracting compressed files (e.g., .tar
files) and supporting URLs as sources.
Syntax:
Example:
This downloads a tarball file from the URL and adds it to /tmp
.
Note: Use COPY
for simple copying of files as it's more predictable. ADD
is useful when you need to handle compressed files or URLs.
6. WORKDIR
The the WORKDIR
directive sets the working directory for subsequent commands in the Dockerfile. If the directory doesn’t exist, it will be created.
Syntax:
Example:
This sets /usr/src/app
as the working directory for all subsequent commands.
7. CMD
The the CMD
directive specifies the default command to run when a container is started from the image. You can only have one CMD
in a Dockerfile. If multiple CMD
directives are provided, only the last one is used.
Syntax:
Or
Example:
This tells Docker to run the node server.js
command when the container starts.
8. ENTRYPOINT
The A ENTRYPOINT
directive defines the command that will always run when the container starts, and it’s often used together with CMD
for additional flexibility. The ENTRYPOINT
is used to set a fixed command, while CMD
can be used to provide default arguments.
Syntax:
Example:
This ensures that when the container starts, it always runs python3 app.py
.
If you use both ENTRYPOINT
and CMD
, CMD
will be passed as arguments to ENTRYPOINT
.
9. EXPOSE
The The EXPOSE
directive informs Docker that the container will listen on the specified network ports at runtime. It does not actually expose the ports; it serves as documentation and helps Docker with networking.
Syntax:
Example:
This indicates that the container will listen on port 8080
.
10. ENV
The the ENV
directive sets an environment variable inside the container. These variables can be accessed by the application running inside the container.
Syntax:
Example:
This sets the environment variable NODE_ENV
to production
.
11. VOLUME
The the VOLUME
directive creates a mount point inside the container that can be used for persistent data storage. You can use volumes to persist data even if the container is removed.
Syntax:
Example:
This creates a mount point at /data
Inside the container.
12. USER
The the USER
directive sets the user name or UID (and optionally the group) to use when running the container. This is important for running the application as a non-root user.
Syntax:
Example:
This ensures the container runs as the node
user.
13. ARG
The A ARG
directive defines build-time variables that can be passed during the docker build
process. They are not available after the image is built (unlike environment variables set with ENV
).
Syntax:
Example:
This sets the VERSION
argument, which can be used in the Dockerfile during the build.
14. STOPSIGNAL
The the STOPSIGNAL
directive specifies the signal to send to the container to gracefully stop it. The default signal is SIGTERM
.
Syntax:
Example:
This sets the stop signal to SIGINT
, which is commonly used for graceful shutdowns.
15. HEALTHCHECK
The A HEALTHCHECK
directive defines a command that Docker can run to check if the container is still healthy. If the container fails the health check, it can be marked as unhealthy.
Syntax:
Example:
This checks the health of the container by running curl
to see if the application inside is responding.
16. SHELL
The SHELL
directive allows you to specify which shell to use for RUN
commands. The default shell is /bin/sh -c
, but you can change it to something else like bash
.
Syntax:
Example:
Summary of Key Dockerfile Directives
-
FROM: Specifies the base image.
-
LABEL: Adds metadata.
-
RUN: Executes commands inside the image.
-
COPY: Copies files from host to image.
-
ADD: Similar to
COPY
, but also supports URLs and auto-extraction. -
WORKDIR: Sets the working directory.
-
CMD: Specifies the default command to run.
-
ENTRYPOINT: Defines the fixed command to run.
-
EXPOSE: Exposes a port.
-
ENV: Sets environment variables.
-
VOLUME: Defines a mount point for persistent data.
-
USER: Sets the user to run commands.
-
ARG: Defines build-time variables.
-
STOPSIGNAL: Sets the stop signal for the container.
-
HEALTHCHECK: Defines a health check command.
-
SHELL: Specifies the shell to use for
RUN
commands.
Each of these directives serves a specific purpose in building and configuring Docker images. They can be combined to automate the building process of images with necessary dependencies, environment variables, and configurations.