Site Tools


hosting:docker:compose

Docker Compose

Define and run multi-container applications with a single YAML file.

Docker Compose simplifies managing multiple containers that work together.

Overview

Docker Compose allows you to define and run multi-container applications using a single YAML file. Instead of running each container separately, you define all services in one place and start them all with a single command.

Basic Structure

```yaml services:

web:
  image: nginx:latest
  ports:
    - "8080:80"
  networks:
    - app-network
db:
  image: postgres:16
  environment:
    POSTGRES_PASSWORD: example
  volumes:
    - postgres-data:/var/lib/postgresql/data
  networks:
    - app-network

networks:

app-network:

volumes:

postgres-data:

```

Docker Compose Commands

```bash docker compose up -d # Start services in background docker compose down # Stop and remove containers docker compose logs # View logs docker compose ps # List containers docker compose restart # Restart services docker compose up -d –build # Rebuild and start ```

Service Discovery

Containers on the same network can reach each other using service names. In the example above, the web container can reach the database at `db:5432`.

Key Concepts

Services:

  • Define each container as a service
  • Specify image, ports, volumes, networks
  • Service name becomes hostname for other containers

Networks:

  • Enable communication between containers
  • Default network created automatically
  • Custom networks for isolation

Volumes:

  • Persist data across container restarts
  • Named volumes managed by Docker
  • Bind mounts link host directories

References

See Also

hosting/docker/compose.txt · Last modified: by 127.0.0.1