Technotes

Technotes for future me

Docker CMD vs ENTRYPOINT

CMD

CMD instruction in Dockerfile is used to provide a default command for an existing container.

It is similar to RUN instruction but the main difference is that RUN executes commands and creates new image layers while CMD sets the command and its parameters to be executed by default after the container is started. If the command is passed in the docker run command then the CMD command is omitted.

CMD ["executable","param1","param2"]  #exec form
CMD ["param1","param2"]    # default parameters to ENTRYPOINT
CMD command param1 param2    #shell form

There is a catch in CMD instructions if more than one CMD instruction is found then only the last CMD will take effect.

ENTRYPOINT

ENTRYPOINT ["executable", "param1", "param2"]  #exec form (preferred)
ENTRYPOINT command param1 param2  #shell form

It serves as the starting point for a Docker container’s runtime process. When you create a Docker image and instantiate it as a container, the ENTRYPOINT command executes by default.

If multiple ENTRYPOINT instructions are defined in the Dockerfile, then only the last ENTRYPOINT will have an effect. Also, You can override the ENTRYPOINT setting using docker run --entrypoint flag.

The main difference between the exec form and the shell form is invoking the shell and the execution part.

In exec form, the shell is not invoked. Command line arguments to docker run <image> will be appended after all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD.

While in shell form, shell is invoked and prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c.

Source:
https://medium.com/@tushar_jain_/the-art-of-crafting-dockerfile-af7f9a85c39b

Last updated on 4 Jan 2024
Published on 22 Jan 2020
Edit on GitHub