0
0
Dockerdevops~5 mins

CMD instruction for default command in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your container to run a specific command automatically when it starts. The CMD instruction in a Dockerfile sets this default command so you don't have to type it every time.
When you want your container to start a web server automatically without extra commands.
When you have a script that should run every time the container launches.
When you want to provide a default behavior but still allow users to override it.
When you build a base image that other images will extend and want a sensible default command.
When you want to simplify running containers by not typing the command manually.
Config File - Dockerfile
Dockerfile
FROM alpine:3.18
CMD ["echo", "Hello from default command"]

The FROM alpine:3.18 line sets the base image to Alpine Linux version 3.18, a small and simple Linux distribution.

The CMD instruction sets the default command to run when the container starts. Here, it runs echo "Hello from default command".

This means if you run the container without specifying a command, it will print the message automatically.

Commands
This command builds a Docker image named 'example-cmd' using the Dockerfile in the current directory. It prepares the image with the default CMD instruction.
Terminal
docker build -t example-cmd .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/2 : FROM alpine:3.18 3.18: Pulling from library/alpine Digest: sha256:... Status: Downloaded newer image for alpine:3.18 ---> abcdef123456 Step 2/2 : CMD ["echo", "Hello from default command"] ---> Running in 7890abcde123 Removing intermediate container 7890abcde123 ---> 123456abcdef Successfully built 123456abcdef Successfully tagged example-cmd:latest
Runs a container from the 'example-cmd' image. Since no command is given, it uses the default CMD from the Dockerfile and prints the message.
Terminal
docker run --rm example-cmd
Expected OutputExpected
Hello from default command
--rm - Automatically removes the container after it exits to keep the system clean
Runs a container but overrides the default CMD by specifying a new command. This shows how CMD can be replaced at runtime.
Terminal
docker run --rm example-cmd echo Overriding default command
Expected OutputExpected
Overriding default command
--rm - Automatically removes the container after it exits
Key Concept

If you remember nothing else from this pattern, remember: CMD sets the default command your container runs, but you can override it when you start the container.

Common Mistakes
Using CMD with shell form without quotes like CMD echo Hello
This can cause unexpected behavior because Docker treats it as a single string and runs it in a shell, which may not handle arguments correctly.
Use the exec form with JSON array syntax like CMD ["echo", "Hello"] to run the command directly.
Adding multiple CMD instructions in one Dockerfile
Only the last CMD instruction takes effect; earlier ones are ignored.
Use only one CMD instruction per Dockerfile or combine commands in a script.
Expecting CMD to run during image build
CMD runs only when the container starts, not during image build.
Use RUN instructions for build-time commands and CMD for runtime defaults.
Summary
The CMD instruction in a Dockerfile sets the default command to run when a container starts.
You build the image with the Dockerfile, then run the container to see the default command execute.
You can override the CMD by specifying a different command when running the container.