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 greeterCMD is how an image knows what to do by default. If you do pass a command to docker run, it overrides the CMD.
Challenge
MediumWrite 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 greeterPassing a command to docker run overrides CMD.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.