Pod Commands and Arguments
How Kubernetes overrides container startup behavior via command and args — and how they map to Docker’s ENTRYPOINT and CMD. Synthesized from CKA Day 11 — Multi Container Pod Kubernetes: Sidecar vs Init Container.
The Docker Baseline
To understand Kubernetes command and args, you must first understand Docker image defaults:
| Dockerfile Instruction | Purpose |
|---|---|
ENTRYPOINT | The executable that always runs |
CMD | Default arguments passed to ENTRYPOINT |
Example Dockerfile:
FROM nginx
ENTRYPOINT ["nginx"]
CMD ["-g", "daemon off;"]At runtime, the container executes: nginx -g "daemon off;"
Kubernetes Overrides
Kubernetes provides two fields that override the Docker defaults:
| Kubernetes Field | Overrides Docker | Purpose |
|---|---|---|
command | ENTRYPOINT | The executable to run |
args | CMD | Arguments passed to the executable |
Critical rule: Kubernetes
commandandargsfully replace the image defaults. They are not appended. If you specifycommand, the image’sENTRYPOINTis ignored. If you specifyargs, the image’sCMDis ignored. Source: CKA Day 11
Override Scenarios
Scenario 1: Override only args
spec:
containers:
- name: nginx
image: nginx
args: ["-g", "daemon off;", "-c", "/etc/nginx/custom.conf"]Result: nginx -g "daemon off;" -c /etc/nginx/custom.conf
Scenario 2: Override only command
spec:
containers:
- name: debug
image: nginx
command: ["sleep"]Result: sleep (no arguments, because CMD from image is also ignored)
Scenario 3: Override both
spec:
containers:
- name: app
image: my-app
command: ["/bin/my-app"]
args: ["--port", "8080", "--env", "production"]Result: /bin/my-app --port 8080 --env production
Scenario 4: Shell form
spec:
containers:
- name: init-script
image: busybox
command: ["/bin/sh"]
args: ["-c", "echo 'Starting...' && mkdir -p /data && touch /data/ready"]Result: /bin/sh -c "echo 'Starting...' && mkdir -p /data && touch /data/ready"
Common Patterns
Init Container with a Wait Loop
initContainers:
- name: wait-for-db
image: busybox:1.36
command: ["/bin/sh"]
args:
- "-c"
- |
until nc -z db 5432; do
echo "Waiting for database..."
sleep 2
done
echo "Database is up!"Sidecar with Arguments
containers:
- name: nginx
image: nginx
- name: nginx-exporter
image: nginx/nginx-prometheus-exporter
args: ["-nginx.scrape-uri", "http://localhost:8080/stub_status"]Custom Entrypoint for Debugging
containers:
- name: app
image: my-app:1.0
command: ["sleep"]
args: ["3600"]Useful for debugging: override the app with
sleepso you cankubectl execinto the container and inspect the filesystem. Source: CKA Day 11
Troubleshooting Commands and Arguments
| Symptom | Cause | Fix |
|---|---|---|
executable file not found | Wrong path in command | Verify the binary exists in the image at that path |
unknown flag | Arguments malformed | Check quoting and list syntax in YAML |
| Container exits immediately | command runs and exits (e.g., echo) | Use a long-running command or add sleep infinity |
CrashLoopBackOff | Combined command + args produce invalid invocation | Test locally with docker run first |
CKA Exam Patterns
# Override command imperatively
kubectl run debug --image=nginx --restart=Never --command -- sleep 3600
# Generate a Pod YAML with a custom command
kubectl run my-pod --image=busybox --restart=Never --dry-run=client -o yaml > pod.yaml
# Then edit pod.yaml to add command and argsExam Tip: On the CKA exam, if a task says “run this container with the command
/bin/sh”, usecommand: ["/bin/sh"]andargs: ["-c", "..."]in the YAML. Do not assume the image’s default entrypoint is what you need. Source: CKA Day 11
Related Pages
- Pod Fundamentals — container basics and YAML structure
- Multi-Container Pods — customizing commands per container
- Init Containers — writing initialization scripts with command/args
- Sidecar Pattern — customizing sidecar startup behavior
- Kubernetes Environment Variables — combining env vars with custom commands
- Deployment, ReplicaSet & Replication Controller — rolling out command changes
- CKA Certification — exam relevance
- CKA Study Roadmap — Day 11 in the 40-day plan
Tags: kubernetes pod command args entrypoint docker cka devops yaml