Docker for Full-Stack Apps

Package your API and run PostgreSQL and Redis locally with Docker and Docker Compose, so every machine runs the same stack.

Intermediate · about 6 hours · difficulty 3/5

Learn first: Environments, Config and Secrets, Terminal and Command Line

Docker packages your app with its runtime and dependencies into an image that runs the same everywhere. Even if you deploy to a platform that builds for you, Docker is the easiest way to run PostgreSQL and Redis locally.

Core concepts

  • Image: a read-only template built from a Dockerfile.
  • Container: a running instance of an image.
  • Volume: persistent storage, so your local database survives restarts.
  • Port mapping: expose a container port (5432) on your machine.

A good Node.js Dockerfile

  • Start from an official slim Node LTS image.
  • Copy package files first and install dependencies, so the layer is cached.
  • Use a multi-stage build: build in one stage, copy only the output and production dependencies into a small final image.
  • Run as a non-root user and add a .dockerignore (node_modules, .env, .git).

Docker Compose

One docker-compose.yml can start your API, PostgreSQL and Redis together with a single command, with environment variables and health checks. New teammates go from clone to running app in minutes.

Pitfalls

  • Copying .env files or secrets into images.
  • Huge images built from full base images with dev dependencies.
  • Losing local data because no volume was configured.

You are ready to move on when docker compose up starts your API, PostgreSQL and Redis, and your API image is a multi-stage build under a few hundred megabytes.

Key concepts

  • Images, containers and volumes
  • Writing a Dockerfile
  • Layer caching
  • Multi-stage builds
  • Docker Compose
  • .dockerignore and non-root users
  • Health checks

Mini project

Write a multi-stage Dockerfile for your API and a docker-compose.yml that runs the API, PostgreSQL and Redis with volumes and health checks, then document one-command setup in the README.

Check yourself

Why copy package.json and install dependencies before copying the rest of your source code in a Dockerfile?

Free resources