WittCode💻

Docker Entrypoint vs. CMD

By

Learn about the Docker ENTRYPOINT and CMD instructions including how to use them and how they are different. We will also go over the different syntaxes for ENTRYPOINT and how both instructions are used together.

Table of Contents 📖

ENTRYPOINT vs. CMD

Both the ENTRYPOINT and CMD instructions are executed when a Docker container is created from an image. However, the instructions supplied to CMD can be overwritten while the instructions supplied to ENTRYPOINT can not.

CMD Demonstration

As a demonstration, lets create a simple docker image and use CMD to echo "Download WittCepter!" (my amazing chrome extension) to the console.

FROM alpine:latest
CMD ["echo", "Download WittCepter!"]

Now lets build this image and give it the tag cmd-i.

docker build -t cmd-i .

Now lets create a container from this image. We will call the container cmd-c.

docker run --name cmd-c cmd-i
Download WittCepter!

Note how when the container is created, the string "Download WittCepter!" is printed to the console. However, as we are using CMD to run this command, we can overwrite it. Lets create another container from this image called cmd-c-2 and supply a command to it.

docker run --name cmd-c-2 cmd-i echo WittCepter is amazing!
WittCepter is amazing!

Note how the echo command we supplied at the end of docker run has overwritten the command that we supplied in our Dockerfile.

ENTRYPOINT Demonstration

Now lets try the same with ENTRYPOINT. First, lets change our Dockerfile to use ENTRYPOINT instead of CMD.

FROM alpine:latest
ENTRYPOINT ["echo", "Download WittCepter!"]

Now lets create an image from this new Dockerfile.

docker build -t entrypoint-i .

Now lets create a container from this image and see what we get.

docker run --name entrypoint-c entrypoint-i
Download WittCepter!

Now lets do what we did last time and add another command at the end of docker run.

docker run --name entrypoint-c-2 entrypoint-i echo WittCepter is amazing!
Download WittCepter! WittCepter is amazing!

Here, we can see how the ENTRYPOINT instruction was not overwritten. Instead, the extra command is appended to the end of the ENTRYPOINT instruction.

ENTRYPOINT Syntax

However, this isn't always the case as the syntax used with the ENTRYPOINT instruction impacts how it works. The syntax we used above is known as exec form. Another syntax that can be supplied to the ENTRYPOINT instruction is the shell form. If we use the shell form then the command supplied to docker run will not be appended to the ENTRYPOINT instruction.

ENTRYPOINT echo "Download WittCepter!"

Now lets build an image from this new Dockerfile.

docker build -t entrypoint-i-2 .

Now lets create a container from this new image and add an extra command to docker run.

docker run --name entrypoint-c-3 entrypoint-i-2 echo WittCepter is amazing
Download WittCepter!

Note how the extra command is now ignored and not appended.

Using CMD and ENTRYPOINT Together

Because of the behavior of CMD and ENTRYPOINT, they are often used together to run a default command if no extra commands are supplied. For example, lets create an image such that when a container is created from it, it will echo "Download WittCepter" unless something else is supplied.

FROM alpine:latest
ENTRYPOINT [ "echo" ]
CMD [ "Download WittCepter" ]

First lets build an image from this.

docker build -t cmd-entrypoint-i .

Now lets create a container from it without supplying any arguments.

docker run --name cmd-entrypoint-c cmd-entrypoint-i
Download WittCepter

Now lets create a container from it using arguments.

docker run --name cmd-entrypoint-c-2 cmd-entrypoint-i WittCepter is amazing!
WittCepter is amazing!

So here, Download WittCepter is the default text, but if something else is supplied then that is used instead.