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 InstructionPurpose
ENTRYPOINTThe executable that always runs
CMDDefault 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 FieldOverrides DockerPurpose
commandENTRYPOINTThe executable to run
argsCMDArguments passed to the executable

Critical rule: Kubernetes command and args fully 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

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 sleep so you can kubectl exec into the container and inspect the filesystem. Source: CKA Day 11

Troubleshooting Commands and Arguments

SymptomCauseFix
executable file not foundWrong path in commandVerify the binary exists in the image at that path
unknown flagArguments malformedCheck quoting and list syntax in YAML
Container exits immediatelycommand runs and exits (e.g., echo)Use a long-running command or add sleep infinity
CrashLoopBackOffCombined command + args produce invalid invocationTest 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 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


Tags: kubernetes pod command args entrypoint docker cka devops yaml