CoursesDocker for beginnersBuilding your own images

CMD vs ENTRYPOINT

What actually runs when a container starts.

Beginner10 min · lesson 11 of 16
In plain terms
ENTRYPOINT is the machine itself — say, a blender — that the container always is. CMD is the default speed setting printed on the dial, which you can override when you press start. Together: a blender preset to “smoothie” that you can switch to “crush ice” on the spot.

Two instructions decide what runs when a container starts, and beginners constantly confuse them. CMD sets the default command and arguments — and anything you type after the image name on docker run replaces it. ENTRYPOINT sets a fixed executable that always runs, with docker run arguments appended to it rather than replacing it. Roughly: CMD is “the default, override me freely,” ENTRYPOINT is “this container is this program.”

Dockerfile
# CMD only: the default, fully overridable
CMD ["nginx", "-g", "daemon off;"]
# docker run myimage echo hi -> runs "echo hi" instead
# ENTRYPOINT only: fixed program, run args become its arguments
ENTRYPOINT ["curl"]
# docker run myimage https://example.com -> runs "curl https://example.com"

Using them together

The idiomatic pattern combines the two: ENTRYPOINT is the executable, and CMD supplies default arguments that a user can override. The container behaves like a command-line tool — run it with no arguments and it does the sensible default; run it with arguments and they flow through to the entrypoint.

Dockerfile
ENTRYPOINT ["python", "backup.py"]
CMD ["--help"]
# docker run backup -> python backup.py --help
# docker run backup /data --gzip -> python backup.py /data --gzip
Prefer the exec form (JSON array)
Write CMD and ENTRYPOINT as a JSON array — ["nginx", "-g", "daemon off;"] — not as a bare string. The array (exec) form runs your process directly as PID 1, so it receives stop signals and shuts down cleanly. The string (shell) form wraps it in /bin/sh, which swallows SIGTERM, so docker stop hangs for ten seconds and then kills it. The bracket syntax is not cosmetic.