archive2021

Laying out Docker Compose stacks on a home server

One directory per stack under /srv, data next to the compose file, secrets in .env. A simple layout that makes backups and moving to a new machine easy.

3 min read#ops#docker#linux

My home server runs about ten small services now: Mosquitto, InfluxDB, Grafana, a few scripts. They grew one by one, and each one was set up a bit differently. Some had data in named volumes, some in my home directory, one in /opt. When I wanted to move everything to a new disk, I had to look up each service to find where its data was. This weekend I cleaned it up. Here is the layout I settled on.

One directory per stack, under /srv

The Filesystem Hierarchy Standard says /srv is for “site-specific data served by this system”. That is exactly what these services are, and the directory is empty on a fresh Debian or Raspberry Pi OS install, so nothing else will put files there.

/srv/
├── iot/
│   ├── docker-compose.yml
│   ├── .env
│   ├── mosquitto/
│   │   ├── config/mosquitto.conf
│   │   └── data/
│   ├── influxdb/data/
│   └── grafana/data/
└── media/
    ├── docker-compose.yml
    ├── .env
    └── ...

A “stack” is a group of services that belong together and start together. The sensor pipeline is one stack. Something unrelated gets its own directory.

The directory name becomes the Compose project name, so containers are called iot_mosquitto_1, iot_grafana_1 and so on. That makes docker ps easier to read.

Data next to the compose file

I switched from named volumes to bind mounts inside the stack directory:

services:
  influxdb:
    image: influxdb:2.0
    restart: unless-stopped
    volumes:
      - ./influxdb/data:/var/lib/influxdb2

Named volumes are fine, but they live in /var/lib/docker/volumes/, away from everything else. With bind mounts, the whole stack (config, data, compose file) is one directory. Backing it up is one rsync of /srv, with the snapshot setup from my rsync –link-dest post.

One thing to watch: some images run as a fixed user ID, and the bind-mounted directory must be writable by that user. Grafana was the one that failed for me. The container log said it could not write to /var/lib/grafana. I fixed it with:

sudo chown -R 472:472 /srv/iot/grafana/data

472 is the user ID the official Grafana image uses. Check the image documentation for others.

Secrets in .env

Passwords and tokens go in a .env file in the same directory. docker-compose reads it automatically and fills in ${VARIABLES}:

    environment:
      DOCKER_INFLUXDB_INIT_PASSWORD: ${INFLUX_PASSWORD}
chmod 600 /srv/iot/.env

If you keep the compose files in Git (I do, in a private repo), add .env to .gitignore and keep a .env.example with empty values instead.

Pin image versions

I used to write image: grafana/grafana with no tag. Then a docker-compose pull brought a new major version and a dashboard broke. Now every image has at least a major version tag, like influxdb:2.0 or eclipse-mosquitto:2. I update on purpose, one stack at a time.

Daily commands

From inside a stack directory:

docker-compose up -d          # start or apply changes
docker-compose logs -f grafana
docker-compose pull && docker-compose up -d   # update images

I added a small shell function so I do not have to cd each time:

dc() { (cd "/srv/$1" && shift && docker-compose "$@"); }
# usage: dc iot logs -f mosquitto

What I have not solved

Database files copied while the database is running may not be consistent. For InfluxDB I now run influx backup to a directory inside the stack before the nightly rsync. For Grafana, which uses SQLite, I have not done anything special yet. I stop nothing during the backup, and so far restoring a copy worked, but I have only tried it once.

Moving to the new disk, by the way, was just rsync -a /srv/ /mnt/newdisk/srv/, changing the mount, and docker-compose up -d in each directory. That was the point of all this.