Dockerfile reference
EXPOSE
The EXPOSE
instruction informs Docker that the container will listen on the specified network ports at runtime. It won’t actually publish the port. It documents what ports are intended to be published. To actually publish the port at runtime, you can use the -p
flag (e.g., docker run -p 800:80
).
It’s useful to remember that the docker network
command supports creating networks for communication among containers without the need to expose or publish specific ports (See networking overview).
FROM
The FROM
instruction initialises a new build stage and sets the base image for subsequent instructions.
COPY
COPY [--chown=<user>:<group>] [--chmod=<perms>] <src>... <dest>
The COPY
instruction copies new files or directories from <src>
to the filesystem of the container at the path <dest>
.
All new files and directories are created with a UID and GID of 0, unless the optional --chown
flag specifies a specific username, groupname or UID/GID combination.
Important points to note:
- The
<src>
path must be inside the context of the build. You cannotCOPY ../something /something
. - If
<dest>
doesn’t exist, it is created along with all missing directories in its path.
CMD
Provides the default command for an executing container. In other words, it’s the default command that will execute when the container is started. If the user specifies an argument to docker run <user-args...>
, then they will override the default specified in CMD
.
Forms
CMD ["executable","param1","param2"]
(exec form, this is the preferred form)CMD ["param1","param2"]
(as default parameters to ENTRYPOINT)CMD command param1 param2
(shell form)
RUN
The RUN
instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
The default shell for the shell form can be changed using the SHELL
command.
Forms
RUN <command>
( the command is run in a shell, which by default is/bin/sh -c
on Linux orcmd /S /C
on Windows) (shell form)RUN ["executable", "param1", "param2"]
(exec form)
WORKDIR
Sets the working directory for any of the following instructions that follow in the Dockerfile:
- RUN
- CMD
- ENTRYPOINT
- COPY
- ADD
By default, an initial WORKDIR
will likely be set by the base image you’re using but in case you’re building an image from scratch using FROM scratch
, the default working directory is /
. It’s good practice to set the WORKDIR
explicitly at the start of your Dockerfile
to avoid operations in unintended locations.
If the WORKDIR
doesn’t exist, it’ll be created.
If a relative path is used, it’ll be relative to the path of the previous WORKDIR
instruction.
Sources
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment