Skip to main content

Configuring Docker Compose Networks and Volumes

1. Defining Networks in Compose

Property Description Default Example
name Name of the network Generated from project name and network name myapp_default
driver Network driver to use bridge overlay
driver_opts Options for the network driver None { "com.docker.network.bridge.name": "docker0" }
attachable Allow external containers to connect false true
enable_ipv6 Enable IPv6 support false true
internal Restrict external access false true
labels Metadata labels for the network None { "com.example.description": "Frontend network" }

Example: Defining a Custom Network

version: '3.8'
services:
  web:
    image: nginx
    networks:
      - frontend

  api:
    image: node:18
    networks:
      - backend

networks:
  frontend:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1

  backend:
    driver: overlay
    attachable: true
Note: Networks defined in docker-compose.yml are created automatically when you run docker compose up.

2. Defining Volumes in Compose

Property Description Values
volumes Defines volume mounts for the service volume_name or source:target
volume_driver Driver to use for volume creation local, aws, gcp
volume_driver_opts Options passed to the volume driver Object with key-value pairs

Example: Named Volume

services:
  web:
    image: nginx
    volumes:
      - my_data:/usr/share/nginx/html

volumes:
  my_data:

Example: Bind Mount

services:
  web:
    image: nginx
    volumes:
      - ./local_dir:/usr/share/nginx/html

Example: Volume with Options

services:
  db:
    image: postgres
    volumes:
      - type: volume
        source: db_data
        target: /var/lib/postgresql/data
        volume:
          nocopy: true

volumes:
  db_data:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: "/host/path"

3. Creating Named Volumes in Compose

Property Description Example
volumes.
volume_name
Name of the named volume to create data_volume
volumes.
volume_name.
driver
Driver to use for volume creation (optional) local
volumes.
volume_name.
driver_opts
Options to pass to the driver (optional) { "o": "bind" }
volumes.
volume_name.
external
Use an existing volume (optional) true

Example: Define a Named Volume in Compose

version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - data_volume:/usr/share/nginx/html

volumes:
  data_volume:
    driver: local

Example: Use External Volume in Compose

version: '3.8'
services:
  db:
    image: postgres
    volumes:
      - existing_db:/var/lib/postgresql/data

volumes:
  existing_db:
    external: true

4. Using Bind Mounts in Compose

Property Description Example
bind Maps a host directory or file to a container path bind: source=/host/path, target=/container/path
source Host path to mount (required) source=/var/log/app
target Container path to mount (required) target=/app/logs
read_only Mounts the volume as read-only read_only=true

Example: Mounting Host Logs to Container

services:
  app:
    image: myapp
    volumes:
      - type: bind
        source: /var/log/app
        target: /app/logs
Note: Bind mounts are useful for development and debugging, but can cause portability issues in production environments.

5. Using External Networks

Property Description Example
external Specifies that the network is created outside of Docker and should not be managed by Compose. external: true
name Name of the external network to use. Must match an existing Docker network. name: my-external-network

Example: Using an External Network in docker-compose.yml

version: '3.8'
services:
  web:
    image: nginx
    networks:
      - external-net

networks:
  external-net:
    external:
      name: my-existing-network
Note: The external network must already exist in Docker before running the Compose file.

6. Using External Volumes

Property Description Example
external Indicates that the volume is created outside of Docker and should not be managed by Docker external: true
name Name of the external volume to use name: my-volume

Example: Using External Volume in Compose File

version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - my-volume:/usr/share/nginx/html

volumes:
  my-volume:
    external: true
    name: my-existing-volume
Note: The external volume must already exist in Docker before the service is started.

7. Configuring Network Drivers

Driver Description Use Case Notes
bridge Default driver; creates an isolated network for containers Local development, microservices communication Containers on the same bridge network can communicate via service names
host Shares the host's network namespace directly High-performance scenarios, direct host port access Container does not get its own IP; uses host's network stack
none Disables networking entirely for the container Security-sensitive containers, isolated environments Container has no network access; no IP address assigned
overlay Creates a distributed network across multiple Docker daemons Multi-host Docker Swarm or Kubernetes environments Requires a key-value store (e.g., Consul, Etcd) for discovery
macvlan Assigns a MAC address to each container, making it appear as a physical device Legacy app compatibility, network segmentation Requires a physical network interface and gateway configuration
ipvlan Assigns an IP address to each container, allowing it to communicate over the network Network segmentation, legacy app compatibility Supports L2 and L3 modes; more flexible than macvlan

Example: Using bridge driver in docker-compose.yml

version: '3.8'
services:
  web:
    image: nginx
    networks:
      - frontend
  api:
    image: node:18
    networks:
      - frontend
      - backend
networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

Example: Using overlay driver in swarm mode

version: '3.8'
services:
  web:
    image: nginx
    networks:
      - app-network
networks:
  app-network:
    driver: overlay
    attachable: true

8. Setting Volume Driver Options

Option Description Values Notes
driver Specifies the volume driver to use local, aws_ecs, vsphere, local-persist Default is local
driver_opts Driver-specific options to pass to the volume driver Map of key-value pairs Used to configure volume behavior (e.g., o=uid=1000,gid=1000)

Example: Using local-persist driver with options

version: '3.8'
services:
  app:
    image: nginx
    volumes:
      - type: volume
        source: mydata
        target: /data
volumes:
  mydata:
    driver: local-persist
    driver_opts:
      mountpoint: /mnt/data
Note: The local-persist driver allows mounting volumes to a specific directory on the host.

9. Setting Network Aliases in Services

Network aliases allow services to be referenced by alternative names within a Docker network, enhancing service discoverability and configuration flexibility.

Property Description Example
networks.<network_name>.aliases Defines aliases for a service within a specific network
networks:
  app-network:
    aliases:
      - api-server
      - web-api

Example: Defining Network Aliases

version: '3.8'
services:
  web:
    image: nginx
    networks:
      app-network:
        aliases:
          - frontend
          - api-endpoint
  api:
    image: node:18
    networks:
      app-network:
        aliases:
          - backend
          - rest-api

networks:
  app-network:
    driver: bridge
Note: Aliases are only effective within the scope of the network they are defined in. A service with an alias can be reached using that alias from other services on the same network.

10. Configuring IPAM Settings

Property Description Values Notes
driver IPAM driver to use default, host-local Default is default
config IPAM configuration blocks Array of config objects Each block defines subnet, gateway, etc.
subnet Network subnet CIDR 172.20.0.0/16 Must be valid CIDR notation
gateway Gateway IP for subnet 172.20.0.1 Must be within subnet
aux_addresses Additional IP to name mappings { "mydb": "172.20.0.10" } Used for static IP assignments

Example: IPAM Configuration

version: '3.8'
services:
  web:
    image: nginx
    networks:
      mynetwork:
        ipv4_address: 172.20.0.10
networks:
  mynetwork:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1
          aux_addresses:
            db: 172.20.0.11

11. Using Network Subnets

Property Description Example
networks.subnet Defines the subnet for a custom network "172.20.0.0/16"
networks.ip_range IP range to allocate from for containers "172.20.1.0/24"
networks.gateway Gateway IP for the network "172.20.1.1"

Example: Defining a Custom Network with Subnet

version: '3.8'
services:
  web:
    image: nginx
    networks:
      custom-net:
        ipv4_address: 172.20.1.10
  db:
    image: postgres
    networks:
      custom-net:
        ipv4_address: 172.20.1.20
networks:
  custom-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
          ip_range: 172.20.1.0/24
          gateway: 172.20.1.1

12. Sharing Volumes Between Services

Volume Type Description Usage Example
named volume A user-defined volume with a name, managed by Docker.
version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - mydata:/usr/share/nginx/html
  db:
    image: mysql
    volumes:
      - mydata:/var/lib/mysql
volumes:
  mydata: {}
bind mount Mounts a host directory or file into a container.
version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - ./app:/usr/share/nginx/html
tmpfs Mounts a temporary filesystem in memory.
version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - type: tmpfs
        target: /tmp

Example: Named Volume for Data Persistence

version: '3.8'
services:
  app:
    image: node:18
    volumes:
      - app-data:/app/data
  db:
    image: postgres:15
    volumes:
      - app-data:/var/lib/postgresql/data
volumes:
  app-data:
    driver: local
Note: Named volumes are managed by Docker and are ideal for sharing data between services or persisting data beyond the lifecycle of containers.
Warning: Bind mounts can cause issues in multi-host environments or when the host directory does not exist.

Was this article helpful?