Exec Form vs. Shell Form

Difficulty: Intermediate

Question

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?

Answer

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.

What each form actually executes

Exec formShell form
Syntax["executable", "arg1"]executable arg1
Process startedThe executable itself/bin/sh -c "executable arg1" (cmd /S /C on Windows)
PID 1Your appThe shell
Variable expansion ($PORT)No, no shell involvedYes
Pipes, &&, globbingNoYes
Needs a shell in the imageNo (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.

Why PID 1 and signals matter

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:

  1. /bin/sh is PID 1 and receives SIGTERM.
  2. A shell acting as PID 1 does not forward the signal to its child process.
  3. Your Node or Java process never hears it, keeps serving, and gets SIGKILLed after the timeout. The container exits with code 137.

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.

When you need shell features

  • Wrap explicitly and exec: CMD ["sh", "-c", "exec node server.js --port=$PORT"]. The exec replaces the shell with node, so node becomes PID 1.
  • Use an entrypoint script that does setup and ends with exec "$@", combined with ENTRYPOINT ["/entrypoint.sh"] and CMD ["node", "server.js"].
  • Add a tiny init with docker run --init (Docker ships tini as docker-init) or install tini in the image. It forwards signals and reaps zombie processes.

ENTRYPOINT and CMD interaction

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.

docker stop with shell form CMD. The shell receives SIGTERM but does not pass it on, so the app is force-killed after the grace period.
  1. Docker daemon → /bin/sh (PID 1): SIGTERM
  2. /bin/sh (PID 1) → node app: Nothing forwarded, app keeps serving
  3. Docker daemon → /bin/sh (PID 1): SIGKILL after 10 second grace period
  4. Docker daemon → node app: Killed with the container, exit 137

Code examples

Exec form in the Dockerfile

# 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.

Handling SIGTERM in the app

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.

Entrypoint script that keeps exec semantics

#!/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.

Key points

  • Exec form runs the binary directly; shell form runs /bin/sh -c and makes the shell PID 1.
  • docker stop sends SIGTERM, waits 10 seconds by default, then SIGKILL (exit 137).
  • Exec form needs double-quoted JSON; anything else falls back to shell form.
  • PID 1 ignores signals it has no handler for, so add a handler or use --init (tini).
  • Shell-form ENTRYPOINT ignores CMD and docker run arguments.

Common follow-up questions

  • What does docker run --init do and when do you need it?
  • How do zombie processes appear inside containers?
  • How would you read an environment variable in CMD while keeping exec semantics?
  • How does Kubernetes terminationGracePeriodSeconds relate to this?

Interview tips

  • Lead with the PID 1 explanation; the syntax difference alone is a weak answer.
  • Mention exit code 137 after a slow docker stop as the symptom you would notice in practice.
  • Bring up exec "$@" in entrypoint scripts; it shows you have written production Dockerfiles.

Concepts covered

Exec form, Shell form, PID 1, SIGTERM, Graceful shutdown, tini

More Docker & Containers questions