In a nutshell
- RUN executes command(s) in a new layer and creates a new image. It is often used to install software packages.
- CMD sets the default command and/or parameters, which can be overwritten from the command line when the docker container runs.
- ENTRYPOINT configures a container that will run as an executable.
All three are quite identical when all can be used to run something inside a Dockerfile.
RUN
RUN instructions are usually used to install
your applications and packages. It executes any commands on top of the current image and creates a new layer by committing the results. Often, you will find multiple RUN instructions in a Dockerfile.
With RUN instruction, Docker will run it as a layer, and the next time you re-build with the Dockerfile, it will not need to run again.
For example, if you want to install a package or create a directory inside of your Docker image, then RUN will be what you want to use. For example, RUN mkdir -p /path/to/folder.
A good illustration of RUN instruction would be to install multiple version control systems packages:
RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion
Note that apt-get update
and apt-get install
are executed in a single RUN instruction. This is done to make sure that the latest packages will be installed. If apt-get install
Were in a separate RUN instruction, it would reuse a layer added by apt-get update
, which could been outdated for a long time already.
CMD
CMD instruction allows you to set a default command, executed only when you run a container without specifying a command. The default command will be ignored if the Docker container runs with a command. If the Dockerfile has multiple CMD instructions, all but the last CMD instructions are ignored.
CMD has three forms:
CMD ["executable","param1","param2"]
(exec form, preferred)CMD ["param1","param2"]
(sets additional default parameters for ENTRYPOINT in exec form)CMD command param1 param2
(shell form)
CMD lets you define a default command to run when your container starts.
CMD is the command the container executes by default when you launch the built image. A Dockerfile will only use the final CMD
defined. The CMD
can be overridden when starting a container with docker run $image $other_command
.
This is a run-time operation, but you still need to rebuild your Docker image to change what your CMD does. A running image is also called a container.
For example, if you were creating a Dockerfile for your web application, a reasonable CMD would be to start its app server.
ENTRYPOINT
ENTRYPOINT is also closely related to CMD
and can modify the way a container starts an image.
ENTRYPOINT instruction allows you to configure a container that will run as an executable. It looks similar to CMD because it allows you to specify a command with parameters. The difference is that the ENTRYPOINT command and parameters are not ignored when the Docker container runs with command line parameters. (There is a way to ignore ENTTRYPOINT, but it is unlikely that you will do it.)
ENTRYPOINT has two forms:
ENTRYPOINT ["executable", "param1", "param2"]
(exec form, preferred)ENTRYPOINT command param1 param2
(shell form)
Be very careful when choosing the ENTRYPOINT form because form behaviour differs significantly.
Exec form
Exec's form of ENTRYPOINT allows you to set commands and parameters, and then you can use either form of CMD to set additional parameters that are more likely to be changed. ENTRYPOINT arguments are always used, while CMD ones can be overwritten by command line arguments provided when the Docker container runs. For example, the following snippet in Dockerfile
ENTRYPOINT ["/bin/echo", "Hello"]
CMD ["world"]
When the container runs as docker run -it <image>
will produce output
Hello world
But when the container runs as docker run -it <image> John
will result in
Hello John
Shell form
Shell form of ENTRYPOINT ignores any CMD or docker run command line arguments.
In the bottom
Use RUN instructions to build your image and install packages and dependencies. It will add layers to the initial image.
Use ENTRYPOINT rather than CMD when building an executable Docker image, and you must execute a command. Additionally, use CMD to provide extra default arguments that could be overwritten from the command line when the Docker container runs.
Choose CMD if you must provide a default command and/or arguments that can be overwritten from the command line when the docker container runs.