Skip to content

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 command and args override 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:

Container start β†’ Main process runs β†’ Process exits β†’ Container stops

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

docker run ubuntu

What happens:

  • Ubuntu image default command = bash
  • No interactive terminal attached
  • bash exits
  • container stops
docker ps
docker ps -a

Warning

If the startup command finishes, container status becomes Exited.


⏱️ Override Default Command at Runtime

Anything after the image name overrides Dockerfile CMD.

docker run ubuntu sleep 5

Final startup command:

sleep 5

Tip

Runtime command replaces CMD completely.


🧱 CMD β€” Default Command

CMD defines the default command + arguments if none are provided at runtime.

CMD sleep 5
CMD ["sleep", "5"]

Success

Always prefer JSON form β€” safer argument parsing.


❌ Wrong CMD JSON Format

CMD ["sleep 5"]

Bug

Executable and arguments must be separate array elements.

Correct:

CMD ["sleep","5"]

πŸ” CMD Is Replaced by Runtime Command

Dockerfile:

CMD ["sleep","5"]

Runtime:

docker run myimg sleep 10

Final command:

sleep 10

Note

Runtime command fully replaces CMD.


πŸšͺ ENTRYPOINT β€” Fixed Executable

ENTRYPOINT defines the base executable.

Runtime values are appended as arguments.

ENTRYPOINT ["sleep"]
docker run myimg 10

Final command:

sleep 10

Abstract

ENTRYPOINT = fixed program
CMD = default parameters


⚠️ ENTRYPOINT Without Default Args

ENTRYPOINT ["sleep"]
docker run myimg

Result:

sleep: missing operand

Warning

ENTRYPOINT needs arguments β€” provide via CMD or runtime.


βœ… Best Practice β€” ENTRYPOINT + CMD

FROM ubuntu
ENTRYPOINT ["sleep"]
CMD ["5"]
docker run myimg

Runs β†’ sleep 5

docker run myimg 20

Runs β†’ sleep 20

Success

ENTRYPOINT + CMD = best pattern for flexible containers.


πŸ”§ Override ENTRYPOINT at Runtime

docker run --entrypoint sleep2.0 myimg 30

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:

  • command
  • args

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:

ENTRYPOINT ["sleep"]
CMD ["5"]

Pod:

containers:
  - name: sleeper
    image: ubuntu-sleeper

Final command:

sleep 5

Success

Image defaults are used.


πŸ”’ Override Only Arguments (args)

Equivalent to:

docker run ubuntu-sleeper 10

Pod:

containers:
  - name: sleeper
    image: ubuntu-sleeper
    args: ["10"]

Final command:

sleep 10

Note

args overrides Docker CMD only.


πŸ” Override Executable (command)

Equivalent to Docker:

docker run --entrypoint sleep2.0 ubuntu-sleeper 10

Pod:

containers:
  - name: sleeper
    image: ubuntu-sleeper
    command: ["sleep2.0"]
    args: ["10"]

Final command:

sleep2.0 10

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:

command: ["sleep 10"]

Bug

Must be split into executable + argument.

Correct:

command: ["sleep"]
args: ["10"]

πŸ§ͺ 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