Skip to content

Docker Compose

Christopher Stevens edited this page Oct 19, 2020 · 4 revisions

YAML concepts

A docker-compose file is basically a series of docker run commands, turned into a structured data format instead of a list of command line parameters. So each : is a variable assignment.

Network in docker-compose

When you want to define a network for services in a docker-compose file to run in the syntax is as follows:

networks: 
  <network_name>:
    driver: bridge
    ipam:
      config:
        - subnet: '<ipv4_address>/<address_range>'

An example for the subnet would be subnet: '172.168.0.0/24'

Then under each service you want to add to a network you place a corresponding networks: tag, as such:

<service_name>:                                                                                                                                                                                                                                                                                                                     
  image: <image_name>                                                                                                                                                                                                                                                                                                
  container_name: <container_name>                                                                                                                                                                                                                                                                                                    
  networks:                                                                                                                                                                                                                                                                                                                
    <network_name>:                                                                                                                                                                                                                                                                                                               
      ipv4_address: <ipv4_address>

And the <ipv4_address> under the service must be in the subnet the network provides. Further note that one service may be part of multiple networks.

Volumes in docker-compose

Of note there are two types of docker volume:

  1. Bind type volumes (mounted to a particular part of your filesystem, with a path specified in the docker-compose.yaml)
  2. Named volumes (managed by docker, not necessarily something you can directly access in your filesystem, must use docker volume [command])

docker-compose provides mechanisms for doing both of these types of volume. Below is an example of a freeipa service that has both types. Note that for the named volume, you need to define a volumes tab to create it. In this example the data volume is the named volume, while /srv/sys/fs/cgroup:/sys/fs/cgroup:ro is a bind type. There are also ways to explicitly state each volume type. Example to be added later.

services:
    freeipa:
        image: cloyne/freeipa-server
        container_name: freeipa-server-container
        volumes:
        - /srv/sys/fs/cgroup:/sys/fs/cgroup:ro
        - data:/data
    
volumes:
        data: