Menu
Coddy logo textTech

The CMD Instruction

Part of the Introduction to Docker section of Coddy's Terminal journey — lesson 23 of 40.

The CMD instruction sets the default command a container runs when you start it without specifying one. It is usually written in the JSON array form:

FROM alpine
CMD ["echo", "container ran"]

Build this image and run it with no command of your own, and it prints container ran automatically:

docker build -t greeter .
docker run greeter

CMD is how an image knows what to do by default. If you do pass a command to docker run, it overrides the CMD.

challenge icon

Challenge

Medium

Write a Dockerfile that starts FROM alpine and sets CMD ["echo", "built and running"]. Build it tagged auto, then run it with no command so the CMD prints.

Cheat sheet

CMD sets the default command a container runs when started without specifying one (JSON array form):

FROM alpine
CMD ["echo", "container ran"]

Build and run:

docker build -t greeter .
docker run greeter

Passing a command to docker run overrides CMD.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction to Docker