Skip to content

Phase 3 — Containerising the Application with Docker and Docker Compose

Phase 2 left the application running on a developer VM using npm start. Phase 3 takes that same application and containerises it — packaging every service into its own Docker container, wiring them together with Docker Compose, and verifying the result with an automated test suite.

The constraint that made this interesting: the developer’s source repository must not be modified. All Docker infrastructure lives in a separate repository. This mirrors how a DevOps engineer would approach an existing application in a real organisation.


The most important structural decision in this phase was keeping infrastructure completely separate from application code. Two repositories, two concerns.

The developer repository contains the Node.js backend, static frontend, and PostgreSQL schema. It is cloned once and never touched again.

The DevOps repository contains everything Docker — the Dockerfiles, Nginx configuration, startup scripts, Docker Compose orchestration, environment variable templates, and the complete documentation set. Nothing from the application source lives here.

This separation is standard practice in organisations that take deployment seriously. It keeps infrastructure changes reviewable independently of application changes, and it means the same source code can be deployed in multiple ways without the developer’s repository needing to know or care.


Four services run in isolation and communicate through two separate Docker networks.

The frontend is an Nginx container serving static files and proxying all API requests to the backend. It sits on the frontend network and cannot reach the database or cache directly — that isolation is enforced at the Docker network layer, not by configuration.

The backend is a Node.js container running the Express API. It sits on both networks, bridging the user-facing side and the data layer. All data access flows through here.

PostgreSQL and Redis sit on the backend network only. The frontend container has no DNS entry for either service. It cannot reach them regardless of what the application code does.

The frontend image uses a two-stage build — Node.js compiles the assets in one stage, Nginx serves them in another. The final image is around 90MB rather than 500MB. No build tools, no Node.js runtime, no development dependencies make it into production.


Nine distinct issues surfaced across build, startup, and runtime. None were trivial. A few stand out.

The sed empty pattern error in the startup script took careful reading to diagnose. The script was designed to inject environment variables into HTML files at container startup — substituting placeholders with real values. The bug was subtle: when a variable is unset, the shell expands it to an empty string before sed sees it, producing an empty search pattern. Alpine’s sed refuses to run with an empty pattern and exits. The fix required escaping the dollar sign on the left side of each substitution so the shell does not expand it.

The chown ordering issue was a classic Docker layering mistake. Permissions were set on the HTML directory before the compiled files were copied into it. The copy ran as root, so the files arrived owned by root regardless of the earlier chown. The fix was simply reordering two Dockerfile instructions — but understanding why required knowing how Docker layers work.

The build context dockerfile path resolution caught me out. Docker Compose resolves the dockerfile path relative to the build context directory, not relative to the docker-compose.yml file. Using a relative path across repositories fails silently in some versions and loudly in others. Absolute paths are the reliable solution.

Every issue is documented inline in the phase documentation — the symptom, root cause, exact fix, and how to prevent it next time.


An automated test suite covers 25 tests across four sections: infrastructure health, API endpoints, complete game flow from user creation through match completion and scoring, and data persistence across container restarts.

All 25 passed.


  • Multi-stage Docker builds and layer caching optimisation
  • Network isolation enforced at the Docker layer, not by policy
  • POSIX-compliant shell scripting for Alpine Linux containers
  • Diagnosing and resolving environment drift between development and container environments
  • Two-repository infrastructure pattern as an enterprise practice

Phase 4 covers Kubernetes — deploying the same application to a Raspberry Pi cluster using k3s, with persistent storage via a Synology NAS and GitOps management through Flux CD.

← Phase 2 — Running the Application from Source on Arch Linux

Repository: sjchristie/humor-memory-game-devops
Documentation: docs/DOCKER_OVERVIEW.md