Brain Dump

Docker Compose

Tags
sys-design

Is a wrapper around docker which simplifies container orchestration and multi-image management. At a high level docker-compose lets you configure multiple services and the dependencies between them so you can reliably spin up an execution environment for them.

Note: Docker compose also allows communication between containers through a private network exposed only to the current docker-compose project. Each service acts as a logical host in the network. To take full advantage of this you must declare what ports are exposed by a service either in docker-compose.yml or the Dockerfile.

docker-compose.yaml

services:
  server:
    # Build image from Dockerfile in cwd (the default)
    build: .
    volumes:
      # read only mount cwd into /workarea
      - .:/workarea:ro
    # Run the image with the docker run --init flag set.
    init: true
    # Run this command when the container is up and running.
    command: ["/bin/bash", "server.py"]
    # Check the health of the container every 3 seconds by
    # checking whether port 5000 is listening or not.
    healthcheck:
      test: 'nc -vz localhost 5000'
      interval: 3s

  hello:
    volumes:
      - .:/workarea:ro
    command: ["/bin/bash", "hello.sh"]
    # Wait until service server is up and healthy
    depends_on:
      server:
	condition: service_healthy
Code Snippet 1: An example docker-compose file defining only a single service named hello.

Command Reference

Up

docker-compose up --detach
Code Snippet 2: Bring up a container suite.

This starts all the services defined in the docker-compose file and then returns control to the shell. If the images for any of the containers have not yet been built then this command also pulls and builds all of them.

Note: up also mounts any configured volumes into the images, sets up a private network on which our service is a logical host, and manages DNS on the private network.

Down

docker-compose down

This shuts down all the containers tied to the current docker-compose project that are up.