Skip to main content
In this guide, we’ll explore how Docker uses the CMD and ENTRYPOINT instructions to define the default process of a container. You’ll learn how to override or extend these defaults at runtime and bake permanent changes into your images.

Why Containers Exit Immediately

When you run a container without specifying a command, Docker launches the default process defined in the image’s Dockerfile. If that process ends, the container exits:
Unlike virtual machines, containers are lightweight and designed to run a single task—such as a web server, database, or script. When that main process completes or fails, the container stops.
A container only runs as long as its main process is alive. Defining a long-running service or shell will keep it running.

Examining Official Images

Popular Docker images set up their primary service using CMD or ENTRYPOINT. Let’s look at two examples:

Nginx Dockerfile Excerpt

MySQL Dockerfile Excerpt

The Default Ubuntu Container Runs Bash

The official Ubuntu image uses bash as its default command. Without an interactive TTY, Bash exits immediately:
Running docker run ubuntu without -t gives Bash no TTY, so it exits immediately—causing the container to stop.

Overriding CMD at Runtime

You can override the default CMD by appending your own command in docker run:
This runs sleep 5, keeps the container alive for 5 seconds, then exits.

Making the Change Permanent with CMD

To bake a default command into your image, declare a new CMD in your Dockerfile:
Build and run:

Shell Form vs Exec Form

CMD can use shell form:
or exec form (JSON array):
With exec form, Docker does not invoke a shell, and the first element must be the executable.

ENTRYPOINT vs CMD

Use ENTRYPOINT to fix the executable but allow arguments to vary:
Then:
With only CMD, any arguments passed to docker run replace the entire command line.
ENTRYPOINT locks in your executable. Combine it with CMD to set default parameters.

Combining ENTRYPOINT and CMD

To specify both a fixed executable and default arguments:
  • docker run ubuntu-sleeper runs sleep 5
  • docker run ubuntu-sleeper 10 runs sleep 10
Always use the JSON array form for both ENTRYPOINT and CMD when combining them. This ensures proper argument handling.

Quick Comparison

Overriding ENTRYPOINT

You can also override ENTRYPOINT at runtime:
This runs sleep2.0 10 instead of the original sleep.

Watch Video