Difficulty: intermediate
Estimated Time: 15 minutes

In this scenario you will explore the different ways to determine what gets executed when you start a container, by defining the ENTRYPOINT and CMD commands in a Dockerfile.

Congratulations, you've got another scenario under your belt! You should now know the difference between ENTRYPOINT and CMD, and between the shell and exec form of commands in Dockerfiles.

Entrypoints and Commands

Step 1 of 4

Image entrypoints

The ENTRYPOINT and CMD commands in Dockerfiles can be a source of confusion. Let's explore how they work.

ENTRYPOINT specifies the default executable

Here is a Dockerfile that will build an image based on Alpine, specifying ls as the entrypoint.

FROM alpine

ENTRYPOINT ["ls"]

Build this image.

docker build -t ls:entrypoint .

  • In docker build, the -t parameter specifies the tag for the image

When you run this image, ls will be run by default. You can specify additional parameters at the end of your docker run command and they will be appended to the entrypoint:

docker run -t ls:entrypoint

  • In docker run, the -t parameter allocates a pseudo-tty for the container, which means we can see the output

docker run -t ls:entrypoint /bin

docker run -t ls:entrypoint -ltr

Overriding the entrypoint

Because the entrypoint is set up as ls, if you want to run a different executable in this container you'll have to explicitly override with the --entrypoint parameter.

docker run -t --entrypoint echo ls:entrypoint hello

Next step

Next we'll see what happens if we add a CMD command to the Dockerfile.

Dockerfile
hello.go