Exec Form vs. Shell Form

Difficulty: Intermediate

Question

Explain the difference between the 'Exec form' and 'Shell form' of instructions like CMD and RUN.

Answer

- **Exec form** (JSON): `["executable", "param"]`. Recommended. It runs the executable directly without a shell wrapper. It receives OS signals properly (SIGTERM). - **Shell form**: `executable param`. It runs the command as `/bin/sh -c executable param`.

**Why it matters**: In the shell form, the shell process becomes PID 1, not your application. Shells often don't pass OS signals to their children. This means when you run `docker stop`, your app won't get the 'graceful shutdown' signal and will be killed abruptly after 10 seconds.

Code examples

Exec vs Shell

# EXEC FORM (Recommended)
CMD ["node", "server.js"]

# SHELL FORM (Avoid for entry/cmd)
CMD node server.js
# Internally runs as: /bin/sh -c 'node server.js'

The JSON array syntax ensures your app is PID 1 and shuts down gracefully.

Key points

Concepts covered

Signals, PID 1, Dockerfile