TExcelTAD
Home › Docker › CMD vs ENTRYPOINT

CMD vs ENTRYPOINT

Both instructions define what runs when a container starts, and they are designed to work together:

  • ENTRYPOINT is the executable: the part that is always run.
  • CMD supplies default arguments (or, without an ENTRYPOINT, the default command). Anything you type after the image name in docker run replaces CMD.
dockerfile
FROM python:3.12-slim
COPY greet.py /usr/local/bin/greet.py
ENTRYPOINT ["python", "/usr/local/bin/greet.py"]
CMD ["--name", "world"]
bash
docker run greeter                    # python greet.py --name world
docker run greeter --name Ada         # python greet.py --name Ada
docker run --entrypoint sh -it greeter   # override the executable itself

How Docker combines them

The final command is ENTRYPOINT + CMD, with a twist for shell form: a shell-form ENTRYPOINT ignores CMD and docker run arguments completely. This model reproduces the rules from Docker's reference table:

python · try it yourself
import json

def to_argv(value):
    if value is None:
        return None
    if value.startswith("["):
        return json.loads(value)                  # exec form
    return ["/bin/sh", "-c", value]               # shell form

def final_command(entrypoint=None, cmd=None, run_args=None):
    ep = to_argv(entrypoint)
    command = run_args if run_args else to_argv(cmd)
    if ep is None:
        return command or "error: no command specified"
    if entrypoint.startswith("["):
        return ep + (command or [])
    return ep                                     # shell-form ENTRYPOINT ignores CMD and arguments

cases = [
    ("CMD exec only",          None, '["nginx", "-g", "daemon off;"]', None),
    ("CMD shell only",         None, "nginx -g 'daemon off;'", None),
    ("run args replace CMD",   None, '["python", "app.py"]', ["python", "-V"]),
    ("ENTRYPOINT + CMD",       '["python", "greet.py"]', '["--name", "world"]', None),
    ("ENTRYPOINT + run args",  '["python", "greet.py"]', '["--name", "world"]', ["--name", "Ada"]),
    ("shell ENTRYPOINT",       "python greet.py", '["--name", "world"]', ["--name", "Ada"]),
    ("nothing",                None, None, None),
]
for label, ep, cmd, args in cases:
    print(f"{label:<22} {final_command(ep, cmd, args)}")
CMD exec only          ['nginx', '-g', 'daemon off;']
CMD shell only         ['/bin/sh', '-c', "nginx -g 'daemon off;'"]
run args replace CMD   ['python', '-V']
ENTRYPOINT + CMD       ['python', 'greet.py', '--name', 'world']
ENTRYPOINT + run args  ['python', 'greet.py', '--name', 'Ada']
shell ENTRYPOINT       ['/bin/sh', '-c', 'python greet.py']
nothing                error: no command specified

The second-to-last line is a common bug: with ENTRYPOINT python greet.py (shell form) the arguments you pass are silently dropped.

Which one should I use?

Image purpose Pattern
A general app image (web server, worker) just CMD ["executable", "arg"], easy to override with a shell for debugging
An image that behaves like a command-line tool ENTRYPOINT ["tool"] + CMD ["--help"] so docker run tool --version works
Setup before the main process (wait for DB, render config) ENTRYPOINT ["/entrypoint.sh"] + CMD ["app"], and the script ends with exec "$@"

The wrapper-script pattern:

bash
#!/bin/sh
set -e
echo "rendering config for $APP_ENV"
envsubst < /etc/app/config.tmpl > /etc/app/config.yaml
exec "$@"     # replace the shell with CMD, so the app becomes PID 1

Why exec form matters: signals and PID 1

docker stop sends SIGTERM to the container's PID 1 and waits (10 seconds by default) before sending SIGKILL. With shell form, PID 1 is /bin/sh, which does not forward signals to its child. Your app never hears the SIGTERM, can't shut down cleanly, and is killed after the timeout.

python · try it yourself
def docker_stop(pid1, child=None, grace=10):
    print(f"PID 1 = {pid1}" + (f", child = {child}" if child else ""))
    print("  -> SIGTERM to PID 1")
    if pid1 == "/bin/sh -c":
        print("  sh ignores it and does not forward it to the child")
        print(f"  ... waiting {grace}s ...")
        print("  -> SIGKILL: app killed mid-request, no cleanup")
        return grace
    print("  app closes connections, flushes data and exits 0")
    return 0.3

shell_form = docker_stop("/bin/sh -c", child="python app.py")
exec_form = docker_stop("python app.py")
print(f"stop took {shell_form}s with shell form vs {exec_form}s with exec form")
PID 1 = /bin/sh -c, child = python app.py
  -> SIGTERM to PID 1
  sh ignores it and does not forward it to the child
  ... waiting 10s ...
  -> SIGKILL: app killed mid-request, no cleanup
PID 1 = python app.py
  -> SIGTERM to PID 1
  app closes connections, flushes data and exits 0
stop took 10s with shell form vs 0.3s with exec form

Fixes, in order of preference: use exec form; in wrapper scripts end with exec "$@"; or run with docker run --init (or Compose's init: true), which puts a tiny init process (tini) at PID 1 that forwards signals and reaps zombie processes.

Tip: your app should handle SIGTERM itself, by finishing in-flight requests and closing database connections. Most web servers (gunicorn, nginx, Node's http with a handler) do.

Test yourself

An image has ENTRYPOINT ["curl"] and CMD ["--help"]. What runs for docker run img -I https://example.com?

  1. curl --help
  2. curl -I https://example.com
  3. -I https://example.com
  4. curl --help -I https://example.com

Why can a container with CMD python app.py (shell form) take 10 seconds to stop?

  1. Python starts slowly
  2. PID 1 is /bin/sh, which does not forward SIGTERM, so Docker waits and then sends SIGKILL
  3. Docker always waits 10 seconds
  4. The image is too large

What does the line exec "$@" at the end of an entrypoint script do?

  1. Prints the arguments
  2. Replaces the shell with the CMD/run arguments, making the app PID 1
  3. Runs the script again
  4. Exits with an error