Both CMD
and ENTRYPOINT
instructions define what command gets executed when running a container. There are few rules that describe their co-operation.
- Dockerfile should specify at least one of
CMD
orENTRYPOINT
commands. ENTRYPOINT
should be defined when using the container as an executable.CMD
should be used as a way of defining default arguments for anENTRYPOINT
command or for executing an ad-hoc command in a container.CMD
will be overridden when running the container with alternative argumen
Docker has a default entrypoint which is /bin/sh -c
but does not have a default command. When you run docker like this:
docker run -i -t ubuntu bash
The entrypoint is the default /bin/sh -c
, the image is ubuntu
and the command is bash
. The command is run via the entrypoint. i.e., the actual thing that gets executed is /bin/sh -c bash
. This allowed Docker to implement RUN
quickly by relying on the shell’s parser. Later on, people asked to be able to customize this, so ENTRYPOINT
and --entrypoint
were introduced.