Every image stores the program a container runs by default. You can replace it per container.
Two parts: ENTRYPOINT and CMD
process = ENTRYPOINT + CMD| Image | ENTRYPOINT | CMD | Runs |
|---|---|---|---|
alpine:3.20 | none | ["/bin/sh"] | /bin/sh |
nginx:1.27 | ["/docker-entrypoint.sh"] | ["nginx","-g","daemon off;"] | /docker-entrypoint.sh nginx -g daemon off; |
pier/backup:1.0 (lab image) | ["/usr/local/bin/backup.sh"] | ["all"] | /usr/local/bin/backup.sh all |
Read them from the image:
docker image inspect --format 'ENTRYPOINT={{json .Config.Entrypoint}} CMD={{json .Config.Cmd}}' nginx:1.27ENTRYPOINT=["/docker-entrypoint.sh"] CMD=["nginx","-g","daemon off;"]Replace CMD: arguments after the image
docker run [OPTIONS] IMAGE cat /etc/alpine-release
└──────── new CMD ───────┘docker run --rm alpine:3.20 cat /etc/alpine-release3.20.3The container runs cat, prints, and exits. Options for Docker go before the image name; everything after it belongs to the container.
Server images accept other commands too. Test an nginx config without starting the server:
docker run --rm nginx:1.27 nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulArguments go to the entrypoint
When the image has an ENTRYPOINT, your arguments replace CMD and are passed to it:
docker run --rm pier/backup:1.0 usersbackup: target=users
backup: writing /backups/users.tar.gz
backup: doneThat also means you cannot run another program this way:
docker run --rm pier/backup:1.0 cat /usr/local/bin/backup.shbackup: target=cat
backup: writing /backups/cat.tar.gz
backup: donecat became an argument of backup.sh.
Replace ENTRYPOINT: --entrypoint
docker run --rm --entrypoint cat pier/backup:1.0 /usr/local/bin/backup.sh#!/bin/sh
# backup.sh: archive one data set into /backups
...--entrypoint cat pier/backup:1.0 /usr/local/bin/backup.sh
└─ new ENTRYPOINT ┘ └────── new CMD ───────┘--entrypoint takes one program name. Its arguments still go after the image.
Summary
| You want | Command |
|---|---|
| Default program | docker run IMAGE |
| Same entrypoint, other arguments | docker run IMAGE ARGS... |
| A different program (no entrypoint) | docker run IMAGE PROGRAM ARGS... |
| A different program (image has an entrypoint) | docker run --entrypoint PROGRAM IMAGE ARGS... |
docker ps -a shows what each container ran in the COMMAND column, and the override stays with the container: docker start runs the same command again.