‹ All Docker Interview Questions questions

Docker Interview Question 4: PID1 Signal Handling

Docker Interview Questions · Test 1 · question 4 of 10

Container Runtimehard

A Node.js API registers a SIGTERM handler that closes database connections and logs "shutting down". Its Dockerfile ends with the shell-form instruction ENTRYPOINT node server.js.

Every docker stop takes about 10 seconds, the "shutting down" message is never logged, and docker exec api ps shows PID 1 is /bin/sh -c node server.js while node has a different PID. You need the container to shut down gracefully and promptly on docker stop. What should you change?

Select one answer
Show answer and explanation

Answer:

  • C. Rewrite the instruction in exec form, ENTRYPOINT ["node", "server.js"], so node runs as PID 1

docker stop sends SIGTERM to the container's main process (PID 1) and, after a grace period, SIGKILL. The default grace period is 10 seconds for Linux containers, which is the delay being seen. The shell form of ENTRYPOINT starts the command as a subcommand of /bin/sh -c, which does not pass signals, so the executable is not PID 1 and never receives the SIGTERM. The shell ignores it, the timeout expires, and the container is killed without the handler running. The exec form runs node directly as PID 1, so it receives SIGTERM and can exit cleanly at once.

Option A only lengthens the wait before SIGKILL. node still never gets the signal, so shutdown becomes slower and is still not graceful.

Option B makes the stop fast but kills the process without letting the handler run, the opposite of a graceful shutdown.

Option D is a restart policy. It controls whether a container is restarted, not how signals reach the process.

References: Dockerfile reference - ENTRYPOINT; docker container stop

Practice Test 1 ›

Study guides for Docker Interview Questions