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 nginxENTRYPOINT ["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 command and argsfully replace the image defaults. They are not appended. If you specify command, the image’s ENTRYPOINT is ignored. If you specify args, the image’s CMD is ignored. Source: CKA Day 11
Useful for debugging: override the app with sleep so you can kubectl exec into 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 imperativelykubectl run debug --image=nginx --restart=Never --command -- sleep 3600# Generate a Pod YAML with a custom commandkubectl run my-pod --image=busybox --restart=Never --dry-run=client -o yaml > pod.yaml# Then edit pod.yaml to add command and args
Exam Tip: On the CKA exam, if a task says “run this container with the command /bin/sh”, use command: ["/bin/sh"] and args: ["-c", "..."] in the YAML. Do not assume the image’s default entrypoint is what you need. Source: CKA Day 11
Practical Practice
Exam-style hands-on tasks for this topic. Complete each task before reviewing the solution. Time yourself — CKA tasks average 5–7 minutes.
Task 1: Override Pod Command
Override the command of a Pod to run sleep 3600 instead of the default entrypoint.
Requirements: Use kubectl run with --command.
Verification:kubectl get pod <pod> && kubectl exec <pod> -- ps aux | grep sleepSolution:
kubectl run debug --image=nginx --restart=Never --command -- sleep 3600kubectl get pod debugkubectl exec debug -- ps aux | grep sleep
Task 2: Pass Arguments to a Container
Pass arguments --port 8080 --verbose to a container, overriding the image CMD.
Requirements: Use YAML with command and args.
Verification:kubectl get pod <pod> -o yaml | grep -A 3 argsSolution:
kubectl run app --image=myapp:1.0 --restart=Never --dry-run=client -o yaml > pod.yaml# Edit pod.yaml:# spec:# containers:# - name: app# image: myapp:1.0# command: ["/bin/my-app"]# args: ["--port", "8080", "--verbose"]kubectl apply -f pod.yamlkubectl get pod app -o yaml | grep -A 3 args
Task 3: Init Container Wait Loop
Create an init container that runs sh -c "until nc -z db 5432; do sleep 2; done".
Requirements: Add to an existing Pod spec. Use --dry-run=client -o yaml.
Verification:kubectl get pod <pod> shows Init:0/1 then Running.
Solution:
kubectl run app --image=nginx --restart=Never --dry-run=client -o yaml > pod.yaml# Edit pod.yaml to add:# initContainers:# - name: wait-for-db# image: busybox:1.36# command: ["/bin/sh"]# args: ["-c", "until nc -z db 5432; do sleep 2; done"]kubectl apply -f pod.yamlkubectl get pod app -w