iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
CMD sets a default for when a container starts; it does not run a command while the image is being built. RUN executes during the build and saves its results in an image layer. At startup, Docker combines the image’s CMD with its ENTRYPOINT, if present. You can replace the default command or arguments when you run the container.
What CMD does—and when it takes effect
Docker processes a CMD instruction while building an image and records its value in the image configuration. The configured default takes effect when a container starts. As Docker’s Dockerfile reference explains, “CMD doesn’t execute anything at build time, but specifies the intended command for the image.”
Use RUN for work that must happen during the build, such as installing a package or creating a file. Use CMD to define what should happen by default at container startup.
FROM alpine
RUN apk add --no-cache curl
CMD ["curl", "--version"]
Here, apk add runs during the build. The default curl --version command is used when a container starts without a replacement command.
#1 Best Overall
CMD forms and their behavior
Docker documents three forms:
CMD ["executable", "param1", "param2"]
CMD ["param1", "param2"]
CMD command param1 param2
- Exec form with an executable: The first form supplies a command and its arguments as a JSON array.
- Exec form with arguments only: The second form supplies default arguments for an
ENTRYPOINT; it does not name an executable itself. - Shell form: The third form is written as a command line rather than a JSON array.
Only the last CMD instruction in a Dockerfile takes effect. If you intend CMD to supply default arguments to ENTRYPOINT, Docker recommends using exec form for both instructions.
How CMD and ENTRYPOINT combine
A Dockerfile should specify at least one of CMD or ENTRYPOINT. Use ENTRYPOINT when the image should behave like a particular executable, and use CMD to provide that executable’s default arguments or, without an ENTRYPOINT, a replaceable default command.
Rank #2
Keep the executable fixed and make its arguments configurable
ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8000"]
With this exec-form pair, starting the image without extra arguments runs python app.py --port 8000. Running docker run my-image --port 9000 keeps the entrypoint and replaces the default CMD arguments, so the effective command is python app.py --port 9000.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Provide a default command users can replace
Without an ENTRYPOINT, CMD supplies the default command. Arguments after the image name replace that default rather than being added to it. This is useful when users may want to run a different command in the same image.
Rank #3
Shell-form ENTRYPOINT is an exception
A shell-form ENTRYPOINT does not follow the argument-passing behavior above: Docker’s reference says it ignores CMD and command-line arguments passed to docker run. If users need to override default arguments while retaining a fixed executable, use exec form for ENTRYPOINT and CMD.
Override CMD when starting a container
Docker’s container run guide documents the form docker run [OPTIONS] IMAGE [COMMAND] [ARG...]. The command and arguments are optional when the image 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
Positional command and argument values after the image name replace CMD. The --entrypoint option replaces the image’s ENTRYPOINT and clears its default CMD. These are startup-time choices: neither changes the Dockerfile nor rebuilds the image.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Choose the pattern that matches the image
- Use
CMDalone when the image needs a convenient default command that users can replace wholesale. - Use exec-form
ENTRYPOINTplus exec-formCMDwhen the executable should stay fixed but users should be able to change its default arguments.
Docker’s Dockerfile overview also distinguishes the instructions directly: RUN performs build-time work, while CMD sets the command used when a container starts.
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
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.

