Skip to main content
In this guide, we’ll dive into how Docker handles commands, arguments, and entrypoints. You’ll learn to:
  • Understand why docker run ubuntu exits immediately
  • See how Docker images set a default process with CMD
  • Override the default command at runtime
  • Bake custom commands into your own image
  • Differentiate between CMD and ENTRYPOINT
  • Combine CMD and ENTRYPOINT for flexible defaults
  • Replace an entrypoint on the fly

1. Why docker run ubuntu Exits Immediately

First, run the following:
You’ll observe:
  • docker run ubuntu starts a container, then it exits right away.
  • docker ps shows no active containers.
  • docker ps -a lists your Ubuntu container with an Exited status.
Containers are designed to run a single process. When that process ends, the container stops.
Without an interactive shell or long-running process, the default /bin/bash has no TTY and quits immediately—so does the container.

2. How Images Define Default Commands (CMD)

Docker images declare a default executable in their Dockerfile using CMD. For example:
A combined snippet illustrating both setups:
Use the JSON array form for CMD and ENTRYPOINT to avoid shell string parsing.

3. Overriding the Default Command at Runtime

Append a new command to docker run to replace the CMD entirely:
Here, the container runs sleep 5 instead of Bash, pauses for 5 seconds, then exits.

4. Baking Your Custom Command into a New Image

To make the override permanent, author a custom Dockerfile:
Build and run:

5. ENTRYPOINT vs. CMD

  • CMD: default command, easily swapped by arguments you supply.
  • ENTRYPOINT: fixed executable; any extra args in docker run are appended.

6. Combining ENTRYPOINT with Default CMD Arguments

Define both to set defaults that users can override:
  • docker run ubuntu-sleeper → runs sleep 5
  • docker run ubuntu-sleeper 10 → runs sleep 10

7. Replacing the Entrypoint at Runtime

Use --entrypoint to swap out the image’s entrypoint completely:

Watch Video