Docker Compose Cheatsheet

Published: 2022-09-09

This page lists some common Docker Compose configurations and commands. Docker Compose is very useful for self-hosting because it is relatively simple to use and requires all the features that you need for self-hosting. One the one hand, Docker Compose setups are specified via declarative files meaning that re-installing a server should be possible within a few hours if have your backups. One the other hand, Docker Compose is much simpler than container orchestration tools such as Kubernetes. If you have never used the tool before, then take a look at the simple tutorial.

First of all, the most common elements of docker-compose.yml are:

version: '3'

    image: 'caddy:2.7.5-alpine'
    container_name: 'caddy'
      - '80:80'
      - '443:443'
      - '/data/caddy:/data:rw'
      - './index.html:/public/index.html:ro'
      - './Caddyfile:/Caddyfile:ro'
    user: '2000:2000'
      driver: 'json-file'
        max-size: '10m'
        max-file: '10'
        MY_USERNAME: 'username'
        - 'MY_PASSWORD.env'
    command: 'caddy run --config /Caddyfile --adapter caddyfile'
    restart: 'unless-stopped'

The next docker compose calls, assume that you are in the same directory as your docker-compose.yml file.

To start a configuration and attach standard out to the logs, use:

$ docker compose up

Note that hitting CTRL + C will stop the services again. So, to start a configuration in the background (without seeing the logs), --detach it:

$ docker compose up -d

and take it down again via:

$ docker compose down

To test a Docker image manually, use:

$ docker run -it --rm caddy:2.5.1-alpine sh

where sh starts the default shell. sh is available in most Docker images.

Make sure to specify container_name: caddy in docker-compose.yml. This allows for stepping into the running container with:

$ docker exec -it caddy sh

and viewing the logs with:

$ docker logs caddy
The text is licensed under CC BY-NC-SA 4.0 and the code under Unlicense.