Sub topic 9
Exec form ENTRYPOINT when there is no shell
This chapter assumes you can write a basic Dockerfile and that you have already built a static Go binary into a scratch or distroless image. Here you will see why shell-form instructions fail in those images, and how to write ENTRYPOINT and CMD so they work.
Two forms of the same instruction
ENTRYPOINT, CMD, and RUN each accept two syntaxes:
| Form | Example | What Docker runs |
|---|---|---|
| Shell form | `CMD /app --port 8080` | `/bin/sh -c "/app --port 8080"` |
| Exec form | `CMD ["/app", "--port", "8080"]` | `/app` directly, with the given arguments |
Shell form wraps your command in /bin/sh -c. That wrapper needs a shell inside the image. A scratch image contains no files at all except what you copy in, so there is no /bin/sh. The distroless static, base, and cc images also leave out the shell, along with package managers and common utilities.
The failure
Build the image from the earlier chapters, but use shell form:
# Fails on scratch and distroless: shell form needs /bin/sh
CMD /app --port 8080
# Works: exec form runs the binary directly
CMD ["/app", "--port", "8080"]With the shell form, the container exits at start with an error such as exec: "/bin/sh": stat /bin/sh: no such file or directory. The binary is present and correct. Docker fails because it tries to start the shell first.
How ENTRYPOINT and CMD combine
In exec form, ENTRYPOINT sets the program and CMD supplies default arguments. Docker joins the two into one argument list.
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
EXPOSE 8080
ENTRYPOINT ["/app"]
CMD ["--port", "8080"]This image runs /app --port 8080 by default. Arguments passed after the image name in docker run replace CMD but keep ENTRYPOINT:
docker run --rm myimage --port 9090That command runs /app --port 9090. If you put everything in CMD instead, the same docker run argument would replace the whole command, and Docker would try to execute --port as a program.
Things a shell used to do for you
Without a shell, some conveniences stop working. Exec form does no processing of its own.
- No variable expansion.
CMD ["/app", "--port", "$PORT"]passes the literal text$PORT. Have the program read the environment variable itself, for example withos.Getenv("PORT")in Go. - No pipes, redirects, or
&&. Put that logic in the program, or do it in the build stage. - No
RUNin the final stage.RUNalso uses a shell by default, and there is nothing to run in ascratchimage. Do file setup, such asmkdirorchmod, in the build stage and copy the result across. - JSON syntax is strict. Exec form must be a valid JSON array with double quotes. Single quotes make Docker fall back to shell form, which then fails.
Signals and PID 1
Exec form has one more benefit. Your binary becomes PID 1 and receives signals such as SIGTERM directly from docker stop. With shell form, the shell is PID 1, and it may not forward the signal, so your program is killed after the stop timeout instead of shutting down cleanly. Go programs that call signal.Notify for graceful shutdown depend on this.
Exercise
A scratch image has ENTRYPOINT ["/app"] and CMD ["--port", "8080"]. You run docker run myimage --port 9090. What command runs inside the container, and what would happen if the Dockerfile used CMD ["/app", "--port", "8080"] with no ENTRYPOINT?
Show answer
The first image runs /app --port 9090, because the run argument replaces CMD and ENTRYPOINT stays. With only CMD ["/app", "--port", "8080"], the run argument replaces the whole command, so Docker tries to execute --port as a program and fails.
Exercise
Why does CMD /app --port 8080 fail on scratch even though /app is a valid static binary?
Show answer
Shell form runs the command as /bin/sh -c "/app --port 8080", and scratch has no /bin/sh. Exec form starts /app directly.