The Short Answer
| ENTRYPOINT | CMD | |
|---|---|---|
| Sets | The program | Default arguments |
| Replaced by | --entrypoint | Arguments after the image |
| Typical use | Fixed tool or start script | Defaults a user may change |
Both are written in a Dockerfile; how to write and build one is in Dockerfile: how to write one.
CMD Alone
FROM nginx:stable
CMD ["nginx", "-g", "daemon off;"]
The container runs this command by default. Anything you type after the image name replaces it completely: docker run --rm myimage nginx -v prints the version and exits. Most official server images work this way.
ENTRYPOINT and CMD Together
FROM alpine:3.24
RUN apk add --no-cache curl
ENTRYPOINT ["curl", "-s"]
CMD ["https://example.com"]
Docker appends CMD to ENTRYPOINT, so the container runs curl -s https://example.com. Arguments at docker run replace only the CMD part:
docker run --rm curlimage -I https://example.com
This runs curl -s -I https://example.com. The image behaves like a command, and CMD holds the defaults.
Exec Form vs Shell Form
Each instruction has two forms. The exec form is a JSON list: CMD ["nginx", "-g", "daemon off;"]. The shell form is plain text: CMD nginx -g "daemon off;", which Docker runs as /bin/sh -c followed by your text.
The difference matters when the container stops. The first process in a container has process ID 1 and receives the stop signal from docker stop. In exec form your program is that process and can shut down cleanly. In shell form the shell is process 1 and usually does not pass SIGTERM on, so Docker waits for the timeout and then kills the program. Use the exec form unless you need shell features such as variable expansion. With exec form, variables like $PORT are not expanded, because no shell runs.
How They Combine
- Only CMD: CMD runs as the command.
- Only ENTRYPOINT, exec form: ENTRYPOINT runs; arguments at docker run are appended to it.
- Both in exec form: ENTRYPOINT runs with CMD as its arguments.
- ENTRYPOINT exec form, CMD shell form: ENTRYPOINT receives
/bin/sh -cand your text as arguments, which is rarely what you want. - ENTRYPOINT in shell form: CMD and arguments at docker run are ignored.
- Neither: not allowed; a Dockerfile must set at least one, or inherit one from its base image.
Setting ENTRYPOINT in your Dockerfile also clears a CMD inherited from the base image, so set CMD again if you need defaults.
Override Both at Run Time
docker run --rm -it --entrypoint sh myimage
docker run --rm myimage --help
The first command replaces ENTRYPOINT with a shell, which is the usual way to look inside an image whose start script fails. Passing --entrypoint also clears the image's CMD. The second replaces only CMD. For a container that is already running, open a shell with docker exec instead.
The Start Script Pattern
#!/bin/sh
set -e
# prepare configuration, wait for the database, and so on
exec "$@"
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
The script does its preparation and then replaces itself with the CMD through exec "$@", so the real program becomes process 1 and receives signals. Many official images use this pattern. A program that starts child processes of its own can also run with docker run --init, which adds a small init process that forwards signals and collects finished child processes.
In Docker Compose
services:
web:
image: myimage:1.0
entrypoint: ["docker-entrypoint.sh"]
command: ["nginx", "-g", "daemon off;"]
entrypoint: overrides ENTRYPOINT and command: overrides CMD. After changing either, apply it with docker compose up -d.