Detailed Dockerfile syntax
FROM
Specifies the base image used to build the Docker image. FROM must be the first non-comment instruction in a Dockerfile. If the specified image is not available locally, Docker will automatically pull it from the public registry.
Example:
|
|
MAINTAINER
Specifies the maintainer information.
|
|
ENV
Sets environment variables that are available for subsequent RUN instructions and persist at runtime within the container.
ENV <key> <value>— sets a single variableENV <key>=<value> ...— allows setting multiple variables at once
Example:
RUN
Executes commands during image build time.
RUN <command>RUN ["executable", "param1", "param2"]
Example:
The first form runs the command in a shell (/bin/sh -c), while the second uses exec execution. Use the second form to specify a different shell or interpreter.
Each RUN instruction executes a command on top of the current image layer, commits the result as a new layer, and subsequent RUN commands operate on the resulting image. Docker images are layered—any historical commit point can be used to create a new image, similar to version control in source code. Long commands can be split across lines using \.
COPY
Copies files or directories from the host filesystem into the container.
COPY <src> <dest>
Example:
|
|
Copies <src> (a relative path from the Dockerfile’s location) to <dest> inside the container. When copying local directories, prefer COPY over ADD.
ADD
Copies files or directories from the host filesystem into the container.
ADD <src> <dest>
Example:
The <src> can be:
- A relative path from the Dockerfile’s directory,
- A URL (automatically downloaded and copied),
- A local tar file (automatically extracted into a directory).
VOLUME
Declares a mount point for data volumes.
Creates a mountable directory that can be mounted from the host or other containers. Typically used for databases or persistent data.
USER
Sets the user ID (UID) under which the container runs. This affects the user context for RUN commands.
USER <uid>
CMD
Supports three formats:
CMD ["executable","param1","param2"]— executes viaexec, recommended.CMD command param1 param2— runs in/bin/sh, suitable for interactive applications.CMD ["param1","param2"]— provides default parameters forENTRYPOINT.
Specifies the default command to run when starting a container. Only one CMD instruction is allowed per Dockerfile; if multiple are defined, only the last one takes effect.
WORKDIR
Sets the working directory for RUN, CMD, and ENTRYPOINT instructions.
Example:
|
|
ONBUILD
Configures instructions to be executed when the built image is used as a base for another image.
ONBUILD [INSTRUCTION]
Example: A Dockerfile creating image-A contains:
When a new image is built using FROM image-A, these ONBUILD instructions are automatically added:
Images with ONBUILD are recommended to include this in their tag (e.g., ruby:1.9-onbuild).
ENTRYPOINT
Defines the default executable to run when the container starts. Unlike CMD, it cannot be overridden by command-line arguments unless explicitly changed via docker run --entrypoint.
Only one ENTRYPOINT instruction is allowed per Dockerfile; later ones override earlier ones.
Supported formats:
ENTRYPOINT [ "nodejs", "server.js" ]ENTRYPOINT command param1 param2— runs in shell
EXPOSE
Informs Docker that the container listens on a specific port at runtime.
EXPOSE <port>
When starting the container, use -P to let Docker assign a random port mapping automatically.
Example:
|
|
Notes & Best Practices
- Keep the image minimal—avoid installing unnecessary packages.
- Create a
.dockerignorefile to exclude unwanted files/directories (syntax similar to.gitignore). - Prefer official Docker Hub images as base layers to reduce image size.
- Fix the initial instructions in the Dockerfile—avoid frequent changes to leverage caching effectively.
- Use
\to join multipleRUNcommands for better readability and maintainability. - Prefer
COPYoverADDwhenever possible. - Use the
-tflag when building images for easier management. - Do not map public ports directly in the Dockerfile.
- Always test the image locally before pushing it to a registry.