Docker Config Reference (docker-compose.yml)

We put out an excellent docker-compose.yml guide on how to configure and set them up!

Docker Config Reference (docker-compose.yml)
We go over numerous docker-compose.yml examples

Here are 30 powerful docker-compose.yml examples. I used to spend HOURS trying to find these types of examples, LLM's make this very proliferant.

Example 1: Basic Single Service (Nginx)

This example deploys a simple Nginx web server without additional configurations.

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

Example 2: Multi-Service Application (Web and Database)

This configuration includes a web application and a PostgreSQL database, with dependency management.

version: '3.8'
services:
  web:
    image: node:20
    depends_on:
      - db
    ports:
      - "3000:3000"
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: example

Example 3: Custom Network Configuration

This example creates a custom bridge network for isolation between services.

version: '3.8'
services:
  app:
    image: alpine
    networks:
      - mynet
networks:
  mynet:
    driver: bridge

Example 4: Volume Mounts (Bind Mount)

This mounts a host directory to the container for persistent data.

version: '3.8'
services:
  data-app:
    image: busybox
    volumes:
      - /host/path:/container/path

Example 5: Named Volumes

This uses a named volume for data persistence across container restarts.

version: '3.8'
services:
  db:
    image: mysql:8
    volumes:
      - db-data:/var/lib/mysql
volumes:
  db-data:

Example 6: Static IP Address in Custom Network

This assigns a static IPv4 address to a service within a custom subnet.

version: '3.8'
services:
  server:
    image: nginx
    networks:
      appnet:
        ipv4_address: 172.28.0.10
networks:
  appnet:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16

Example 7: Environment Variables

This passes environment variables to configure a Redis service.

version: '3.8'
services:
  cache:
    image: redis:7
    environment:
      - REDIS_PASSWORD=secret
      - REDIS_PORT=6379

Example 8: Port Mapping with Multiple Ports

This exposes multiple ports for a multi-protocol application.

version: '3.8'
services:
  multi-port:
    image: some/image
    ports:
      - "8080:80"
      - "8443:443"

Example 9: Depends_on with Healthcheck

This ensures a web service starts only after a database is healthy.

version: '3.8'
services:
  web:
    image: webapp
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

Example 10: Restart Policy

This sets a restart policy for high availability.

version: '3.8'
services:
  critical-app:
    image: important/service
    restart: always

Example 11: Resource Limits (CPU and Memory)

This constrains resources for a service to prevent overuse.

version: '3.8'
services:
  limited:
    image: resource-heavy
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 512M

Example 12: Secrets Management

This uses Docker secrets for sensitive data injection.

version: '3.8'
services:
  secure-app:
    image: secure/image
    secrets:
      - my_secret
secrets:
  my_secret:
    file: ./secret.txt

Example 13: Configs for Configuration Files

This mounts external configuration files as configs.

version: '3.8'
services:
  configured:
    image: app
    configs:
      - source: app_config
        target: /etc/app.conf
configs:
  app_config:
    file: ./app.conf

Example 14: Multiple Networks

This connects a service to multiple custom networks.

version: '3.8'
services:
  multi-net:
    image: connector
    networks:
      - net1
      - net2
networks:
  net1:
  net2:

Example 15: Volume with Driver Options

This specifies a volume with custom driver options for advanced storage.

version: '3.8'
services:
  storage:
    image: data-handler
    volumes:
      - type: volume
        source: custom-vol
        target: /data
volumes:
  custom-vol:
    driver: local
    driver_opts:
      type: nfs
      o: addr=192.168.1.1
      device: :/var/nfs

Example 16: Logging Configuration

This customizes logging for a service using a specific driver.

version: '3.8'
services:
  logged:
    image: app
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

Example 17: User and Working Directory

This runs a service as a specific user with a custom working directory.

version: '3.8'
services:
  custom-user:
    image: base
    user: "1000:1000"
    working_dir: /app

Example 18: Command Override

This overrides the default command in the image.

version: '3.8'
services:
  overridden:
    image: some/image
    command: ["custom", "command", "--arg"]

Example 19: Entry Point Override

This customizes the entrypoint for script execution.

version: '3.8'
services:
  entry:
    image: script-runner
    entrypoint: /bin/sh

Example 20: Build from Dockerfile

This builds a service from a local Dockerfile.

version: '3.8'
services:
  built:
    build:
      context: .
      dockerfile: Dockerfile

Example 21: External Network

This references an external pre-existing network.

version: '3.8'
services:
  external-net:
    image: app
    networks:
      - external-net
networks:
  external-net:
    external: true

Example 22: IPv6 Enabled Network

This enables IPv6 in a custom network.

version: '3.8'
services:
  ipv6-app:
    image: nginx
    networks:
      - ipv6net
networks:
  ipv6net:
    enable_ipv6: true
    ipam:
      config:
        - subnet: 2001:db8::/64

Example 23: Tmpfs Mount

This uses a temporary file system mount for in-memory data.

version: '3.8'
services:
  tmpfs:
    image: memory-app
    tmpfs:
      - /run:size=64M

Example 24: Profiles for Conditional Services

This defines services that run only under specific profiles.

version: '3.8'
services:
  dev-tool:
    image: debugger
    profiles: ["dev"]

Example 25: Combined Features (Networks, Volumes, Static IP)

This integrates multiple features: custom network with static IP, named volume, and port mapping.

version: '3.8'
services:
  combined:
    image: full-app
    ports:
      - "80:80"
    volumes:
      - app-data:/data
    networks:
      combinet:
        ipv4_address: 172.29.0.5
networks:
  combinet:
    driver: bridge
    ipam:
      config:
        - subnet: 172.29.0.0/16
volumes:
  app-data:

Here is 5 mysql examples:

Example 26: Basic MySQL Deployment

This example deploys a standalone MySQL database with default configurations, exposing the standard port for external access.

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
    ports:
      - "3306:3306"

Example 27: MySQL with Persistent Volume

This configuration includes a named volume for data persistence, ensuring database files survive container restarts.

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydb
    volumes:
      - mysql-data:/var/lib/mysql
volumes:
  mysql-data:

Example 28: MySQL with Custom Network and Static IP

This assigns a static IP to the MySQL service within a custom bridge network, suitable for controlled environments.

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
    networks:
      dbnet:
        ipv4_address: 172.30.0.10
networks:
  dbnet:
    driver: bridge
    ipam:
      config:
        - subnet: 172.30.0.0/16

Example 29: MySQL with Healthcheck and Environment Variables

This incorporates a healthcheck to verify database readiness and uses environment variables for advanced configuration.

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_USER: appuser
      MYSQL_PASSWORD: appsecret
      MYSQL_DATABASE: appdb
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

Example 30: MySQL with Init Script and Bind Mount

This mounts an initialization script from the host to seed the database upon startup, utilizing a bind mount for flexibility.

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "3306:3306"

docker-compose.yml service name linking

In Docker Compose, automatic routing between two containers is inherently supported through the default networking behavior. When services are defined in the same docker-compose.yml file, they are automatically placed on a shared bridge network unless explicitly configured otherwise. This enables seamless communication via service discovery, where containers can resolve and route to each other using their service names as hostnames. No manual IP addressing or additional routing definitions are required, as Docker handles DNS resolution and internal routing within the network. This facilitates efficient inter-container connectivity, such as a web application querying a database service.

For instance, consider a scenario with a web service and a database service. The web service can connect to the database using the hostname "db" without further configuration.

Example docker-compose.yml

version: '3.8'

services:
  web:
    image: nginx:latest  # Example web server
    depends_on:
      - db  # Ensures the database starts first
    ports:
      - "8080:80"  # Exposes the web service externally (optional)

  db:
    image: mysql:8.0  # Example database
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: exampledb

In this setup:

  • The web service can access the db service at db:3306 (MySQL's default port).
  • Routing occurs automatically over the shared network created by Docker Compose.
  • To test, execute docker compose up in the directory containing the file, then from within the web container (e.g., via docker compose exec web sh), you could use tools like curl or ping db to verify connectivity.

This approach ensures isolation from external networks while providing reliable internal routing. If custom networks or advanced isolation are needed, you may define explicit networks in the file, but the default suffices for most cases.