Lesson 250 of 296
Multi-stage builds and Docker Compose
What you will learn
- Multi-stage builds for small, secure production images
- Building a TypeScript app in Docker
- Docker Compose for local development
- Service names, networks, volumes and health checks
- Environment variables in Compose
Multi-stage builds
A TypeScript build needs the compiler and dev dependencies, but production only needs the compiled JavaScript and runtime dependencies. A multi-stage build uses one stage to build and copies only the results into a clean final stage:
FROM node:24-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM node:24-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build && npm prune --omit=dev
FROM node:24-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/package.json ./
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:3000/healthz || exit 1
CMD ["node", "dist/server.js"]AS deps,AS build,AS runtimename the stages;COPY --from=buildcopies files from an earlier stage.npm prune --omit=devremoves development dependencies after the build.- The final image contains no TypeScript compiler, no source code, no dev dependencies: smaller to download, faster to start, and with fewer packages that could have vulnerabilities.
HEALTHCHECKlets Docker mark the container healthy or unhealthy (orchestrators such as ECS often define their own health checks instead).
Tested with a small Express and TypeScript API on node:24-alpine (base image 243.7 MB): a single-stage build that installs all dependencies and compiles in place produced 299.7 MB; this multi-stage build produced 248.8 MB. The application's own layers shrank from about 56 MB to about 5 MB, the final image contained no typescript package, ran as the node user, reported healthy, and on docker stop the app logged that it received SIGTERM and exited with code 0.
Docker Compose
Compose runs several containers together from one YAML file: perfect for local development with an API, a database and Redis.
services:
api:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:app@db:5432/app
REDIS_URL: redis://cache:6379
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
db:
image: postgres:18-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
cache:
image: redis:8-alpine
volumes:
db-data:| Concept | Explanation |
|---|---|
| Service name as hostname | Compose puts all services on one network; the API reaches the database at host db, not localhost (inside a container, localhost is the container itself) |
ports: "3000:3000" | Publish to your machine: host port : container port |
volumes: db-data:... | A named volume, so database data survives docker compose down and restarts. From PostgreSQL 18 the official image keeps data in a versioned folder (/var/lib/postgresql/18/docker when this was tested), so the volume is mounted at /var/lib/postgresql. Older guides mount /var/lib/postgresql/data, which is right for version 17 and earlier |
depends_on with service_healthy | Start the API only after the database health check passes, not just after the container starts |
environment | Variables passed into containers; for secrets locally, use an env_file that is not committed |
Commands:
| Command | Does |
|---|---|
docker compose up -d --build | Build and start everything in the background |
docker compose logs -f api | Follow one service's logs |
docker compose exec db psql -U app | Run a command inside a service |
docker compose down | Stop and remove containers (volumes kept) |
docker compose down -v | Also delete volumes (deletes database data) |
This Compose file was run as written: the database became healthy first, the API started only after that, and inside the API container the names db and cache resolved to the other containers.
Compose is mainly for development and testing. In production, services usually run on a container platform (ECS, Kubernetes, Cloud Run, Fly.io) with a managed database rather than a database container. (The old top-level version: key in Compose files is obsolete and can be omitted.)
How to say it in an interview
"I use multi-stage builds: one stage installs dependencies, one compiles TypeScript and prunes dev dependencies, and a clean runtime stage copies only the compiled output and production modules, runs as a non-root user and defines a health check, which makes images smaller and safer. For local development I use Docker Compose to run the API with PostgreSQL and Redis; services reach each other by service name, named volumes keep data, and depends_on with service_healthy waits for the database to be ready. In production I use a container platform and managed databases instead of Compose."
Key words
| Word | Meaning |
|---|---|
| Multi-stage build | A Dockerfile with separate build and runtime stages |
| COPY --from | Copies files from an earlier stage |
| Docker Compose | Runs multi-container setups from a YAML file |
| Service | One container definition in Compose |
| Named volume | Persistent storage managed by Docker |
| depends_on | Start order and readiness between services |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.