Compose networks

Compose creates a network for each project and attaches every service to it. Services reach each other by service name, like db or cache, on their own ports.

How it works

The default network is named after the project, usually the folder: shop_default. You can declare several networks, front for the proxy and back for the database, so that the proxy never reaches the database directly.

What breaks

Inside the stack, use the service name and the container port (db:5432), never localhost: localhost is the container itself. Published ports are for clients outside the server.

In compose.yaml

services:
  proxy:
    image: nginx:1.27
    networks: [front]
  api:
    build: ./api
    networks: [front, back]
  db:
    image: postgres:16
    networks: [back]

networks:
  front:
  back:

See it break, then fix it

Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.