Difficulty: Intermediate
Explain the difference between the exec form and the shell form of CMD, ENTRYPOINT and RUN. Why does it matter for signals and graceful shutdown?
The exec form is a JSON array, CMD ["node", "server.js"], and Docker runs that executable directly as PID 1. The shell form is a plain string, CMD node server.js, and Docker wraps it in /bin/sh -c, so the shell becomes PID 1 and your app may run as its child. Use exec form for CMD and ENTRYPOINT so your app receives SIGTERM on docker stop and can shut down gracefully; shell form is fine for RUN.
| Exec form | Shell form | |
|---|---|---|
| Syntax | ["executable", "arg1"] | executable arg1 |
| Process started | The executable itself | /bin/sh -c "executable arg1" (cmd /S /C on Windows) |
| PID 1 | Your app | The shell |
Variable expansion ($PORT) | No, no shell involved | Yes |
Pipes, &&, globbing | No | Yes |
| Needs a shell in the image | No (works on distroless) | Yes |
Exec form must be valid JSON with double quotes. CMD ['node','server.js'] is not valid JSON, so Docker treats it as shell form. The SHELL instruction changes which shell the shell form uses.
docker stop sends SIGTERM to PID 1, waits a grace period (10 seconds by default, changeable with docker stop -t or --stop-timeout on docker run), then sends SIGKILL. With shell form:
/bin/sh is PID 1 and receives SIGTERM.The result is dropped in-flight requests, unflushed logs and half-processed queue messages. For an order service doing rolling deploys at Swiggy scale, that is a real outage risk. Some shells exec a single simple command directly instead of forking, but that is an implementation detail you should not rely on.
There is a second subtlety: the kernel does not apply default signal actions to PID 1 in a namespace. If your app is PID 1 and installs no SIGTERM handler, the signal is simply ignored. So exec form plus a handler in the app (or an init process) is the full fix.
CMD ["sh", "-c", "exec node server.js --port=$PORT"]. The exec replaces the shell with node, so node becomes PID 1.exec "$@", combined with ENTRYPOINT ["/entrypoint.sh"] and CMD ["node", "server.js"].docker run --init (Docker ships tini as docker-init) or install tini in the image. It forwards signals and reaps zombie processes.Shell-form ENTRYPOINT ignores CMD and any arguments passed to docker run. Exec-form ENTRYPOINT receives CMD (or the run arguments) appended as parameters. This is another reason to default to exec form.
# Dockerfile
FROM node:22-alpine
WORKDIR /app
COPY . .
# Exec form: node is PID 1 and receives SIGTERM
CMD ["node", "server.js"]
# Avoid: sh -c becomes PID 1 and swallows SIGTERM
# CMD node server.js
With exec form, docker stop delivers SIGTERM straight to node.
const http = require('http');
const server = http.createServer((req, res) => res.end('ok'));
server.listen(3000);
// Without a handler, node running as PID 1 ignores SIGTERM
process.on('SIGTERM', () => {
console.log('SIGTERM received, draining connections');
server.close(() => process.exit(0));
});
PID 1 gets no default signal behaviour from the kernel, so the app must handle SIGTERM explicitly, or you run it under --init.
#!/bin/sh
# entrypoint.sh: run migrations, then hand PID 1 to the app
set -e
node scripts/migrate.js
exec "$@"
# Dockerfile
# ENTRYPOINT ["/entrypoint.sh"]
# CMD ["node", "server.js"]
exec replaces the shell process with the CMD, so the app becomes PID 1 and gets signals directly.
Exec form, Shell form, PID 1, SIGTERM, Graceful shutdown, tini