Use docker run command to pass arguments to CMD in Dockerfile
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

