Docker Compose

Docker Compose is a tool that lets you build multi-container applications and the way that you define the interactions between these containers is through a YAML file. Commonly named docker-compose.yaml, this file specifies what containers your application needs, how to build them, and what configurations they should have. Let’s take a simple example from the Docker Compose documentation:

services:
  frontend:
    image: awesome/webapp
    ports:
      - "443:8043"
    networks:
      - front-tier
      - back-tier
    configs:
      - httpd-config
    secrets:
      - server-certificate

  backend:
    image: awesome/database
    volumes:
      - db-data:/etc/data
    networks:
      - back-tier

volumes:
  db-data:
    driver: flocker
    driver_opts:
      size: "10GiB"

configs:
  httpd-config:
    external: true

secrets:
  server-certificate:
    external: true

networks:
  front-tier: {}
  back-tier: {}

At the top level you’ll recognize a map with five keys: services, volumes, configs, secrets, and networks. services is itself a map that allows you to name many different containers. In this example we have one named frontend and one named backend. Under the frontend map, you can see that networks specifies a list with two entries: front-tier and back-tier. Note the ports map – the first list entry is wrapped in double quotes because it contains the reserved : character. Also note how both keys in networks map to the empty map {} – this is because their named presence is all that matters, they require no additional configuration.

Docker Compose files are a great example of using YAML for configuration, especially in a hierarchical context. It’s extremely intuitive to understand how many services you have defined and what their individual configurations are. Each piece of configuration can be contained in its own separate map but use shared names to help relate things to each other.

%d bloggers like this: