4.02 Commands and Arguments in Docker & Kubernetes
This guide explains how commands and arguments work in both Docker and Kubernetes in a simple way.
It focuses on:
- How containers decide what to run at startup
- How CMD and ENTRYPOINT behave in Docker
- How Kubernetes
commandandargsoverride image behavior - Correct patterns and common mistakes
π³ Commands and Arguments in Docker
Docker containers run one main process. When that process exits, the container stops.
π― Core Concept β Container = Main Process
A container is not a virtual machine. It does not boot an OS.
It runs a single primary process.
Lifecycle:
Startup command can come from:
- Dockerfile
CMD - Dockerfile
ENTRYPOINT - Command passed in
docker run
Note
If there is no longβrunning process = container exits immediately.
βΆοΈ Why docker run ubuntu Stops Immediately
What happens:
- Ubuntu image default command = bash
- No interactive terminal attached
- bash exits
- container stops
Warning
If the startup command finishes, container status becomes Exited.
β±οΈ Override Default Command at Runtime
Anything after the image name overrides Dockerfile CMD.
Final startup command:
Tip
Runtime command replaces CMD completely.
π§± CMD β Default Command
CMD defines the default command + arguments if none are provided at runtime.
Success
Always prefer JSON form β safer argument parsing.
β Wrong CMD JSON Format
Bug
Executable and arguments must be separate array elements.
Correct:
π CMD Is Replaced by Runtime Command
Dockerfile:
Runtime:
Final command:
Note
Runtime command fully replaces CMD.
πͺ ENTRYPOINT β Fixed Executable
ENTRYPOINT defines the base executable.
Runtime values are appended as arguments.
Final command:
Abstract
ENTRYPOINT = fixed program
CMD = default parameters
β οΈ ENTRYPOINT Without Default Args
Result:
Warning
ENTRYPOINT needs arguments β provide via CMD or runtime.
β Best Practice β ENTRYPOINT + CMD
Success
ENTRYPOINT + CMD = best pattern for flexible containers.
π§ Override ENTRYPOINT at Runtime
Tip
Useful for debugging or temporary overrides.
π§ Docker Startup Resolution Order
docker run command
β
Replaces CMD
β
Appended to ENTRYPOINT (if present)
β
Final executed process
π CMD vs ENTRYPOINT β Quick Compare
| Behavior | CMD | ENTRYPOINT |
|---|---|---|
| Default executable | β | β |
| Default args | β | β |
| Runtime command | Replaces | Appends |
| Best for | Defaults | Fixed program |
βΈοΈ Commands and Arguments in Kubernetes
Kubernetes lets you override container startup behavior using:
commandargs
These map directly to Docker behavior.
π― Core Idea β Pod Startup Control
Container startup inside a Pod can come from:
- Docker ENTRYPOINT
- Docker CMD
- Pod
command - Pod
args
Note
Kubernetes can override both ENTRYPOINT and CMD.
π§± Mapping β Docker vs Kubernetes
| Docker | Kubernetes |
|---|---|
| ENTRYPOINT β‘οΈ | command |
| CMDβ‘οΈ | args |
Tip
command = executable
args = parameters
βΆοΈ Default Behavior (No Pod Overrides)
Dockerfile:
Pod:
Final command:
Success
Image defaults are used.
π’ Override Only Arguments (args)
Equivalent to:
Pod:
Final command:
Note
args overrides Docker CMD only.
π Override Executable (command)
Equivalent to Docker:
Pod:
Final command:
Warning
command replaces Docker ENTRYPOINT.
π§ Kubernetes Override Flow
Dockerfile ENTRYPOINT + CMD
β
Image default command
β
Pod args β replaces CMD
Pod command β replaces ENTRYPOINT
β οΈ Common Mistake
Wrong:
Bug
Must be split into executable + argument.
Correct:
π§ͺ Minimal Test Pod
apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: sleeper
image: ubuntu
command: ["sleep"]
args: ["15"]
π§ Exam Checks
Question
Which Pod field overrides Docker ENTRYPOINT?
command
Question
Which Pod field overrides Docker CMD?
args
Question
Are command and args arrays?
Yes β always list form.
β Final Summary
Summary
- Containers run one main process
- Docker CMD = default args
- Docker ENTRYPOINT = fixed executable
- Runtime command replaces CMD
- Runtime args append to ENTRYPOINT
- Kubernetes command β ENTRYPOINT
- Kubernetes args β CMD
- Always use JSON/list form