Skip to content

Instantly share code, notes, and snippets.

@npodonnell
Last active July 30, 2020 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npodonnell/83371892dcefb855b6dad7c45638273c to your computer and use it in GitHub Desktop.
Save npodonnell/83371892dcefb855b6dad7c45638273c to your computer and use it in GitHub Desktop.

Docker Compose Cheatsheet

N. P. O'Donnell, 2020

Facts about Docker Compose

  • Docker Compose is not a part of Docker
  • Consists of 2 parts: CLI and .yaml files
  • Deals in services (a service is comprised of several containers)
  • Default compose file is docker-compose.yml but any filename may be used
  • First line of compose file is version, but compose file version does not correspond to docker compose version.

Installing/Housekeeping

sudo apt-get install docker-compose

-or-

pip install docker-compose

Discover version:

docker-compose version

Useful Commands

Bring everything up. Run in a directory with a docker-compose.yml file

docker-compose up

Same thing, but daemonized:

docker-compose up -d

Same thing, but force a rebuild:

docker-compose up --build

Bring everything down

docker-compose down

To refresh a service:

docker-compose up

docker-compose.yml format

A very simple docker-compose.yml:

version: '2.0'
services:
    solution:
        build .
        image: solution

Note: the build . directive tells docker-compose to build the solution image and the location of the Dockerfile is ..

Remote Deployments

docker-compose can be run on a remote machine with:

DOCKER_HOST=ssh://<user>@<host> docker-compose up -d

and

DOCKER_HOST=ssh://<user>@<host> docker-compose down

This requires key-based SSH to be set up.

Compose v2 vs v3

Compose v2 is primarily used for local development and v3 is used for orchestrators such as swarm or kubernetes.

Bind Mounts

A bind mount is a mechanism where directory on the host machine can be accessed in the container through a mount on the container's file system. Bind mounts are handy for local development.

To create a bind mount, use the volumes property in the compose file:

volumes:
  # Just specify a path and let the Engine create a volume
  - /container/location

  # Specify an absolute path mapping
  - ./myfolder/location:/container/location

Volumes

Volumes are similar to bind mounts but use the docker engine's volume subsystem, and are more suited to production use. Volumes use a similar syntax to bind mounts:

    volumes:
      - type: volume
          source: db-data
          target: /var/lib/mysql/data

Shelling into a Service

For debugging, you may want to shell into one of the containers (services) while it's running:

docker-compose exec <service> /bin/sh

Or after it runs:

docker-compose run <service> /bin/sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment