CMD sets an image’s default command or default arguments for when a container starts; it does not run a command during the image build. For build-time work—such as installing packages or creating files—use RUN. At startup, Docker combines the image’s CMD with its ENTRYPOINT, if present, and command-line arguments can replace the defaults.
What does CMD do in a Dockerfile?
CMD records the default action or arguments in the image configuration. Building the image stores that configuration; the configured default takes effect when you start a container. Docker’s Dockerfile reference puts it plainly: “CMD doesn’t execute anything at build time, but specifies the intended command for the image.”
As an Amazon Associate I earn from qualifying purchases.
By contrast, RUN executes during the build, and its result becomes part of an image layer. For example:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →FROM alpine
RUN apk add --no-cache curl
CMD ["curl", "--version"]
The package installation happens while the image is built. If you start a container from that image without supplying a replacement command, Docker uses curl --version as the default. See Docker’s Dockerfile overview for the distinction between build instructions and the command used when a container starts.
#1 Best Overall
Which CMD syntax should you use?
Docker documents three forms:
CMD ["executable", "param1", "param2"]
CMD ["param1", "param2"]
CMD command param1 param2
- Exec form with an executable: The first form specifies the default command and its arguments.
- Exec form with arguments only: The second form provides default arguments for an
ENTRYPOINT; it is not a complete command by itself. - Shell form: The third form is written as a command line rather than a JSON array. Docker processes it through a shell.
Only the last CMD instruction in a Dockerfile takes effect. If you intend CMD to provide default arguments to an ENTRYPOINT, Docker recommends using exec form for both instructions.
How CMD and ENTRYPOINT work together
A Dockerfile should specify at least one of CMD or ENTRYPOINT. Use ENTRYPOINT when the image should behave like a particular executable; use CMD for a default command or for default arguments to that executable.
Rank #2
CMD supplies a replaceable default command
Without an ENTRYPOINT, CMD supplies the default command. Arguments provided after the image name in docker run replace that default command and its arguments.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Exec-form ENTRYPOINT keeps the executable fixed
ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8000"]
With this combination, the default startup runs python app.py --port 8000. If you run docker run my-image --port 9000, Docker keeps the exec-form entrypoint and replaces the CMD arguments with --port 9000. This is useful when the executable should stay fixed but users need to change its options.
Rank #3
Shell-form ENTRYPOINT does not pass arguments the same way
Docker’s reference says a shell-form ENTRYPOINT ignores both CMD and command-line arguments supplied to docker run. If you expect runtime arguments to reach an entrypoint, do not assume shell form behaves like the exec-form pattern above.
How to override CMD when starting a container
Docker’s running containers guide documents the command shape as docker run [OPTIONS] IMAGE [COMMAND] [ARG...]. The command is optional when the image already has a default.
# Use the image's CMD default
docker run my-image
# Replace CMD with another command and its arguments
docker run my-image echo hello
# Replace ENTRYPOINT; this also clears the image's default CMD
docker run --entrypoint /bin/sh my-image
- To use the default: Run the image name without a command after it. Docker uses the image’s startup configuration.
- To replace CMD: Put the replacement command and any arguments after the image name. If there is no
ENTRYPOINT, these replaceCMD; with an exec-formENTRYPOINT, they become its arguments and replace theCMDarguments. - To replace ENTRYPOINT: Use
--entrypointbefore the image name, followed by the image and any arguments for the new entrypoint. Docker clears the image’s defaultCMDwhen this option is used.
These are choices made for a container startup. They do not change the Dockerfile or rebuild the image.
Quick Recap
Best Value
- Docker, Docker Swarm, Docker Compose, Programmer, Developer, Coding, Programming, Software Engineer, Code, DevOps, Deploy, Deployment, Kubernetes, Salt, Puppet, Chef, Terraform, Container, AWS, Azure, Cloud, Geek, Funny, Computer, Software, Tech, IT
- Integration, Scrum, Compile, Compilation, Science, Bug, Debug, Python, Linux, Java, Javascript, Scala, Dotnet, Kotlin
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
Choosing between CMD and ENTRYPOINT
| Pattern | Use it when | Runtime behavior |
|---|---|---|
CMD alone |
The image needs a default command that users may replace wholesale. | Arguments after the image name replace the default command and arguments. |
Exec-form ENTRYPOINT plus exec-form CMD |
The executable should remain fixed while users can adjust its default arguments. | Arguments after the image name replace the CMD arguments and are passed to the entrypoint. |
Shell-form ENTRYPOINT |
Use only when its shell-command behavior is intended. | Docker’s reference says it ignores CMD and docker run command-line arguments. |
Common CMD mistakes
- Expecting a build-time command: Use
RUNfor actions that must happen while building.CMDsets startup defaults. - Assuming CMD always names an executable: With an
ENTRYPOINT, exec-formCMDcan contain only default arguments. - Expecting runtime arguments to be added to CMD: Arguments after the image name replace the
CMDdefaults. With exec-formENTRYPOINT, they replace the default arguments passed to that entrypoint. - Using shell-form ENTRYPOINT but expecting normal argument passing: Docker documents that shell-form
ENTRYPOINTignoresCMDanddocker runarguments. - Adding several CMD instructions: Only the last one applies.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




