How to override the CMD command in the docker run line
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
CMD in a Dockerfile defines the default command a container will run when it starts. If you want something different for one invocation, you do not need to rebuild the image; you can override that default directly on the docker run command line.
The detail that trips people up is the interaction between CMD and ENTRYPOINT. Overriding CMD is simple, but if the image also defines an ENTRYPOINT, your command may become arguments to that entrypoint rather than replacing the whole process.
Override CMD at Runtime
If an image only defines CMD, anything you place after the image name in docker run replaces it.
Example Dockerfile:
Run with the default command:
Override the CMD:
The second invocation ignores the Dockerfile CMD and runs the command you supplied.
Understand the ENTRYPOINT Interaction
If the image defines both ENTRYPOINT and CMD, Docker combines them. In that model:
- '
ENTRYPOINTdefines the executable' - '
CMDprovides default arguments'
Example:
Running the image normally executes:
If you run:
you are overriding the CMD arguments, not the ENTRYPOINT. The result becomes:
That is often exactly what you want for application images.
Override the Entry Point When Needed
If you need to bypass the image entrypoint entirely, use --entrypoint.
That starts a shell instead of the configured application. This is useful for debugging, inspecting files, or verifying environment variables inside the image.
You can also combine --entrypoint with extra arguments:
Without --entrypoint, the text after the image name might be treated as arguments to the existing entrypoint, which can be confusing if you expected a shell command to run directly.
Shell Form Versus Exec Form
Dockerfiles support both shell form and exec form for CMD, but exec form is usually safer:
instead of:
Exec form avoids an extra shell layer and handles signals more predictably. That matters for containers that need graceful shutdown behavior under orchestration systems.
From the override perspective, both can be replaced at docker run time, but images using exec form are easier to reason about.
A Practical Debugging Pattern
A common use case is replacing the startup command so you can inspect a broken container:
Then inside the container, you can verify files, dependencies, and startup scripts manually. This is often faster than rebuilding the image blindly.
Another common case is changing a default command for a one-off task:
That works only if my-image does not have an entrypoint that interferes. If it does, inspect the Dockerfile first.
Common Pitfalls
- Forgetting that text after the image name overrides
CMD, not necessarilyENTRYPOINT. - Assuming your override replaces the entire startup process when the image has an entrypoint.
- Using shell form in the Dockerfile and then getting confusing quoting behavior.
- Debugging a container with a nonexistent shell. Minimal images may not include
/bin/shorbash.
Summary
- Put a command after the image name in
docker runto overrideCMD. - If the image has an
ENTRYPOINT, your override may become its arguments instead of replacing it. - Use
--entrypointwhen you need to replace the configured executable itself. - Prefer exec form in Dockerfiles because it is easier to reason about and handles signals better.
- Inspect both
CMDandENTRYPOINTbefore assuming how a container startup command will behave.
Related reading
- How to pass arguments to a Dockerfile?
- How to pass arguments to Shell Script through docker run
- How to pass Docker CLI --gpus Options in Kubernetes or enable GPU support without installing nvidia-docker2 Docker 19.03
- How to pass docker run parameter via kubernetes pod
- How to pass environment variable to docker-compose up
- How to pass image pull secret while using 'kubectl run' command?
- How to perform kaniko Docker build and push in separate GitLab CI stages?
- How to persist data in a dockerized postgres database using volumes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.