Skip to content

5. From "Runs in Docker" to "Runs in Docker Correctly"

Closing the gap between “runs” and “runs correctly”

Section titled “Closing the gap between “runs” and “runs correctly””

docker compose up starting the stack was a big step past manual setup, but none of those containers were production-ready: the backend shipped tooling it never used at runtime, the build context could have leaked .env files with real credentials into the image, the process didn’t handle shutdown signals cleanly, and everything ran as root.

.dockerignore stops secrets and bloat from ever entering the build context in the first place — .env, .git, node_modules never get sent to the Docker daemon at all, which matters more for security than image size: a credential that never enters a layer can’t later be pulled back out of one with docker history.

A real multi-stage build for the backend split dependency installation from the runtime image, so the final image ships only node_modules, the source it actually needs, and nothing else — no npm, no build tooling. Both containers were also switched to run as non-root users, so a dependency vulnerability caps out at “unprivileged process inside a container” rather than a path to the host.

Real graceful shutdown, and the bug it caused

Section titled “Real graceful shutdown, and the bug it caused”

The original shutdown handler called process.exit(0) the instant it received SIGTERM — any in-flight request, a checkout mid-write, got dropped outright. The fix: stop accepting new connections, let in-flight requests finish, close the Postgres pool and Redis client cleanly, then exit, with an internal timeout so a genuinely stuck request can’t hang past Docker’s own kill window.

Rebuilding with the new shutdown logic surfaced a real crash: EADDRINUSE, the moment the container started. server.js had ended up with two separate app.listen() calls — an old one left over from earlier lessons, and a new one added to capture the server reference the shutdown handler needed. Both tried to bind port 3001; the second one failed instantly. Fixed by removing the redundant original call — a reminder that adding graceful shutdown isn’t purely additive, it touches the exact code that starts the server.

None of this hardening is visible to a user browsing the app — that’s the point. It’s entirely about what happens when a deployment rolls out or a container gets compromised. Graceful shutdown specifically is close to mandatory for zero-downtime deploys: when an orchestrator sends SIGTERM to old containers while routing traffic to new ones, skipping graceful drain means visible connection resets on every single deploy. Non-root containers are increasingly policy-enforced rather than optional — Kubernetes Pod Security Standards can reject root-running pods outright, and most serverless container platforms carry similar restrictions.


That closes out the DevOps Foundation module. Next: Automation & Operations, starting with GitHub Actions.