- Understand why
docker run ubuntuexits 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
CMDandENTRYPOINT - Combine
CMDandENTRYPOINTfor flexible defaults - Replace an entrypoint on the fly
1. Why docker run ubuntu Exits Immediately
First, run the following:
docker run ubuntustarts a container, then it exits right away.docker psshows no active containers.docker ps -alists your Ubuntu container with an Exited status.
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:
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 todocker run to replace the CMD entirely:
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 customDockerfile:
5. ENTRYPOINT vs. CMD
| Instruction | Purpose | Runtime Override |
|---|---|---|
| CMD | Sets a default command and arguments | Fully replaceable |
| ENTRYPOINT | Configures the primary executable | Appends runtime args |
- CMD: default command, easily swapped by arguments you supply.
- ENTRYPOINT: fixed executable; any extra args in
docker runare appended.
6. Combining ENTRYPOINT with Default CMD Arguments
Define both to set defaults that users can override:
docker run ubuntu-sleeper→ runssleep 5docker run ubuntu-sleeper 10→ runssleep 10
7. Replacing the Entrypoint at Runtime
Use--entrypoint to swap out the image’s entrypoint completely: