Use docker run command to pass arguments to CMD in Dockerfile
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Passing runtime arguments correctly in Docker depends on how CMD and ENTRYPOINT are defined in your Dockerfile. Many confusing cases come from mixing shell form and exec form commands. Once you separate defaults from runtime overrides, argument passing becomes predictable.
Understand CMD Versus ENTRYPOINT
CMD provides default arguments or a default command. ENTRYPOINT defines the main executable that always runs. The most flexible pattern is to set executable in ENTRYPOINT and default options in CMD.
Build and run:
This runs with default CMD arguments.
Override CMD Arguments at Runtime
When ENTRYPOINT is set, extra values passed to docker run replace CMD only.
The executed command becomes python app.py --mode prod --port 9000.
If you define only CMD and no ENTRYPOINT, passing arguments to docker run replaces the whole command, which is often not what beginners expect.
Inspect Effective Runtime Command
To debug argument behavior, inspect image metadata.
You can also override entrypoint explicitly:
This is useful for one off diagnostics or shell access.
Pattern for Wrapper Scripts
If your container uses a shell script wrapper, make sure it forwards arguments using "$@".
Dockerfile:
Without argument forwarding, runtime overrides appear to be ignored.
Shell Form Versus Exec Form in Detail
Docker supports shell form and exec form for both CMD and ENTRYPOINT. Shell form runs through a shell process, which changes signal handling and quoting behavior. Exec form runs the binary directly and is preferred for most production services.
Shell form example:
Exec form example:
With shell form, argument escaping can differ between environments and stop signals may not reach the actual app process cleanly. Exec form avoids these issues and makes override behavior explicit.
Combining Environment Variables With Runtime Arguments
Use environment variables for stable defaults that may vary by deployment target, and runtime arguments for per run overrides.
This separation keeps images reusable across staging and production while preserving clear runtime control.
Common Pitfalls
- Using shell form
CMDwith complex quoting and expecting exec style behavior. - Defining both command logic and defaults in
CMDonly, then losing them on runtime override. - Forgetting argument forwarding in wrapper scripts.
- Assuming
docker run image arg1appends when noENTRYPOINTexists. - Overriding entrypoint in production accidentally and bypassing startup checks.
Summary
- Use
ENTRYPOINTfor executable andCMDfor default arguments. - Runtime arguments after image name replace
CMDvalues. - Inspect image config when command behavior is unclear.
- Wrapper scripts must forward arguments with
"$@". - Prefer exec form instructions for predictable argument handling.
- Keep container startup interfaces documented with concrete examples so operators know which flags override defaults and which require image rebuild.
- Test signal handling with stop and restart scenarios to ensure your chosen command form exits cleanly under orchestration platforms.
- Keep argument names backward compatible when evolving container interfaces so deployment scripts do not break after image updates.
- Validate runtime overrides in automated smoke tests so entrypoint and default argument contracts remain trustworthy over time.
Related reading
- Use host networking while building a VSCode devcontainer
- Use kind to install different Kubernetes version
- Use of Supervisor in docker
- Use private docker registry with Authentication in Jenkinsfile
- Use relative paths in Kubernetes config
- Using --add-host or extra_hosts with docker-compose
- Using GPU from a docker container?
- Using GPU in VS code container

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.