Docker Compose is a tool to run multiple containers with one command using a YAML file.

Without Compose you run 4-5 commands just to start your app. With Compose you run one command and everything starts.


Without vs With Docker Compose

Without (manual):

docker network create my-network
docker run -d --name redis-cache --network my-network redis
docker run -d --name mongo-db --network my-network mongo
docker build -t my-app .
docker run -d --name app-container --network my-network -p 5000:5000 my-app

With Compose:

docker-compose up

Same result, one command.


Install Check

Comes built-in with Docker Desktop on Windows and Mac.

docker-compose --version

Basic Structure of docker-compose.yml

version: '3.8'

services:
  service-name:
    image: redis          # use existing image from Docker Hub
    # OR
    build: .              # build from your Dockerfile
    ports:
      - "8080:80"         # host:container
    environment:
      - KEY=value         # environment variables
    volumes:
      - data:/app         # persistent storage
    networks:
      - my-net            # which network to join
    depends_on:
      - database          # start this after database starts

networks:
  my-net:                 # Docker creates this automatically

volumes:
  data:                   # Docker creates this automatically

Example 1 — Networks (Node App + MongoDB)

Services on the same network talk to each other using the service name as hostname.

version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    networks:
      - app-network
    depends_on:
      - database

  database:
    image: mongo
    networks:
      - app-network

networks:
  app-network: