Sub topic 12
ENTRYPOINT: the fixed executable
This chapter assumes you can write a basic Dockerfile, build an image with docker build, and start a container with docker run. It also assumes you know the difference between shell form and exec form, and what CMD does.
What ENTRYPOINT does
ENTRYPOINT sets the executable that runs when a container starts. It makes the image behave like a single program. Anything you put after the image name in docker run is added to the entrypoint as arguments. It does not replace the entrypoint.
This differs from CMD. A CMD is a default that docker run image some-command replaces entirely. An ENTRYPOINT stays in place unless you override it deliberately.
FROM alpine:3.20
ENTRYPOINT ["ping", "-c", "3"]docker build -t pinger .
docker run --rm pinger localhost
docker run --rm pinger 127.0.0.1Both runs execute ping -c 3 and append the address you gave. The image always runs ping, and you only choose the target.
Use exec form
Write ENTRYPOINT in exec form, as a JSON array. Shell form wraps the command in /bin/sh -c, and that causes two problems:
- Arguments from
docker runand fromCMDare ignored, because they are passed to the shell and not to your program. - Your program runs as a child of the shell, not as PID 1, so it may not receive stop signals.
# Shell form: runs as /bin/sh -c "ping -c 3 localhost"
ENTRYPOINT ping -c 3 localhost
# Exec form: runs ping directly
ENTRYPOINT ["ping", "-c", "3", "localhost"]Overriding the entrypoint
Use the --entrypoint flag when you need a different executable, for example to open a shell and inspect an image:
docker run --rm -it --entrypoint sh pingerArguments after the image name then go to sh, not to ping. The flag takes only an executable name. Pass any further arguments after the image name.
Combining with CMD
When both instructions exist, CMD supplies default arguments to the ENTRYPOINT. The docker run arguments replace the CMD values and leave the entrypoint untouched.
FROM alpine:3.20
ENTRYPOINT ["ping"]
CMD ["-c", "3", "localhost"]Running docker run --rm pinger executes ping -c 3 localhost. Running docker run --rm pinger -c 1 127.0.0.1 executes ping -c 1 127.0.0.1.
A common pattern: wrapper scripts
Many images use a script as the entrypoint to prepare the environment, then hand control to the real process. End the script with exec "$@". That replaces the shell with your program, so the program becomes PID 1 and receives signals.
#!/bin/sh
set -e
echo "Starting with arguments: $@"
exec "$@"FROM python:3.12-slim
WORKDIR /app
COPY entrypoint.sh app.py ./
RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
CMD ["python", "app.py"]The script runs first. It then executes whatever CMD or docker run supplies.
Common errors
- Arguments ignored: the entrypoint is in shell form. Switch to exec form.
exec format erroror permission denied: the script lacks the executable bit or a valid#!line. Runchmod +xat build time.- Container does not stop quickly: the process is not PID 1 because a shell sits in front of it. Use exec form or
exec "$@". - Only the last ENTRYPOINT counts: if a Dockerfile has several, the final one wins.
Exercise
A Dockerfile has ENTRYPOINT ["ping"] and CMD ["-c", "3", "localhost"]. What command runs for docker run --rm pinger -c 1 127.0.0.1, and why does CMD not run?
Show answer
The container runs ping -c 1 127.0.0.1. Arguments after the image name replace the CMD values, while the ENTRYPOINT stays fixed, so ping is still the executable.
Exercise
You wrote ENTRYPOINT ping -c 3 (shell form) and run docker run pinger localhost. Why does the target not reach ping, and how do you fix it?
Show answer
Shell form runs /bin/sh -c "ping -c 3", and the shell does not forward the extra argument to ping. Change it to exec form: ENTRYPOINT ["ping", "-c", "3"].