Get startedGet started for free

Choosing a start command for your Docker image

1. Choosing a start command for your Docker image

Up to this point the images we created don't run anything by default. Now we'll see how to add a default start command to our images and how to override the start command when we start an image.

2. What is a start command?

When we start a container from the hello-world image, the container will start, print text, and then stop. The creators of this image chose a start command that prints text and then exits. This makes sense in the context of a hello-world image.

3. What is a start command?

It wouldn't make sense for the creators of an image with the goal of running python to do the same. Instead, it would be useful for their image to start a python session for the user automatically and also to exit the session once that python session is stopped. Docker images have this flexibility; using the CMD instruction we can choose any shell command to execute when a container is started from the image.

4. Running a shell command at startup

Like everything else we add to images, we can add a start command using an instruction in the Dockerfile. This instruction is CMD. CMD accepts a single parameter, the shell command to run when the image starts. The shell command runs when somebody starts a container; it is not executed when using docker build to create an image from the Dockerfile. Adding a CMD instruction to a Dockerfile does not increase the image size and does not add any time to the build. If multiple CMD instructions exist in a single Dockerfile, then only the last one will have any effect.

5. Typical usage

When building an image for a specific use, it makes sense to set the CMD instruction to start an application related to this particular use. This could be starting a python based data analysis or starting software like a database that accepts outside connections. Another typical pattern is to run a script at startup that starts multiple other applications.

6. When will it stop?

With the CMD instruction, we set a shell command to run when a container is started from the image. The container will stay running until this shell command exits. The hello-world image only prints text and exits right after doing so. An image that starts a database by default will stop if the database is stopped or crashes. The more general the use case of the image, the more flexible the start command should be. For example, if we make a general usage Ubuntu image, simply opening a shell would make sense as a start command. The image will then exit when the user exits the shell.

7. Overriding the default start command

While the CMD instruction sets a default start command for the image, this default start command can be overridden when starting an image using the Docker run command. Just like we pass the image we want to start to Docker run, we can pass a second optional argument, which will override the CMD instruction set in the image. Often when replacing the start command of an image, we will run the image in interactive mode, using the 'dash it' flag. Using a shell as new start command, for example, bash, allows us to look around the image, discover files and see what's installed.

8. Summary

Here is a summary of the new commands and instructions you can refer back to when completing the exercises.

9. Let's practice!

Now, let's try adding a start command to the images we've been making.