Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Docker Compose, Roles Split

A complete separate-role deployment in one Compose file: the acme, admin and worker roles each in a container of their own, over PostgreSQL, from the published image, 0.6.1 or later. What it buys over the single container is the point of the split: the container parsing untrusted JWS and CSRs from the network never mounts the CA key, and neither does the one holding operator sessions.

Everything here is the stock image and the stock configuration; the separation comes from which volumes each container mounts and which environment variables it is given.

What each container touches

ServiceRunsPublishesMounts
postgresThe databasenothingpostgres
initacme-proxy init, once, then exitsnothingca, ca-key
workerserve --role workernothingca, ca-key
acmeserve --role acme3000ca read-only
adminserve --role admin3001, on the host’s loopbackca read-only, admin-tls

Four named volumes hold the state outside the database:

  • ca — the CA certificate ca.pem, the exported CRL ca.crl and its lock. The worker writes them; acme and admin only read ca.pem, which is what GET /ca.pem serves.
  • ca-key — the CA private key, ca.key, alone. Only init and worker mount it, so the key is not merely unreadable from the other two containers: it is not in their filesystem at all.
  • admin-tls — the web admin’s self-signed certificate, generated by the admin container on its first start.
  • postgres — the database files.

Each volume is mounted on a directory the image provides for it — /data/ca, /data/ca-key and /data/admin-tls — and the signer.local_ca and admin.tls path keys, set as environment variables, point the server there, so that each volume holds exactly one kind of thing. Nothing in acme writes to disk, which is why its root filesystem can be read_only.

Those directories are what make the volumes writable. The server runs as the image’s user, uid 1000, and a new named volume takes the owner of the image directory it is mounted on: the image ships the three owned by that user (ca-key with mode 0700), so no ownership step is needed, unlike a bind mount in Containers. Mounted anywhere else, Docker creates the volume owned by root and init fails with Permission denied; images before 0.6.1 do not have the directories.

The configuration

Every container reads one config.toml, mounted read-only. What differs per role is in the Compose file, as environment variables, so the file is the same one an all-in-one deployment would use. A minimal one:

[server]
base_url = "https://acme.example.com"

[profiles.default]

server.base_url is the public URL the clients use; with a reverse proxy terminating TLS in front of port 3000, it is the proxy’s address (see Reverse proxy). Filters, challenges and the rest are added here exactly as in any other deployment.

The database password goes in a .env file beside the Compose file, which Compose reads on its own:

printf 'POSTGRES_PASSWORD=%s\n' "$(openssl rand -hex 24)" > .env
chmod 600 .env

It reaches the containers inside database.url, which the server never logs in clear.

The Compose file

docker-compose.yml:

x-acme-proxy: &acme-proxy
  image: ghcr.io/acme-proxy/acme-proxy:0.7.0
  restart: unless-stopped
  environment: &environment
    ACME_PROXY_CONFIG: /etc/acme-proxy/config.toml
    ACME_PROXY_DATABASE__URL: postgres://acme:${POSTGRES_PASSWORD}@postgres/acme
    ACME_PROXY_SIGNER__LOCAL_CA__CERT_PATH: /data/ca/ca.pem
    ACME_PROXY_SIGNER__LOCAL_CA__CRL_PATH: /data/ca/ca.crl
    RUST_LOG: acme_proxy=info

services:
  postgres:
    image: docker.io/library/postgres:17
    restart: unless-stopped
    environment:
      POSTGRES_USER: acme
      POSTGRES_DB: acme
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "acme", "-d", "acme"]
      interval: 5s
      retries: 12

  # Migrates the schema and generates the CA, then exits. Re-running it is
  # harmless: an existing CA is loaded, never replaced.
  init:
    <<: *acme-proxy
    restart: "no"
    command: ["init"]
    environment:
      <<: *environment
      ACME_PROXY_SIGNER__LOCAL_CA__KEY_PATH: /data/ca-key/ca.key
    volumes:
      - ./config.toml:/etc/acme-proxy/config.toml:ro
      - ca:/data/ca
      - ca-key:/data/ca-key
    depends_on:
      postgres:
        condition: service_healthy

  worker:
    <<: *acme-proxy
    command: ["serve", "--role", "worker"]
    environment:
      <<: *environment
      ACME_PROXY_SIGNER__LOCAL_CA__KEY_PATH: /data/ca-key/ca.key
    volumes:
      - ./config.toml:/etc/acme-proxy/config.toml:ro
      - ca:/data/ca
      - ca-key:/data/ca-key
    depends_on:
      init:
        condition: service_completed_successfully

  acme:
    <<: *acme-proxy
    command: ["serve", "--role", "acme"]
    read_only: true
    ports:
      - "3000:3000"
    volumes:
      - ./config.toml:/etc/acme-proxy/config.toml:ro
      - ca:/data/ca:ro
    depends_on:
      init:
        condition: service_completed_successfully

  admin:
    <<: *acme-proxy
    command: ["serve", "--role", "admin"]
    environment:
      <<: *environment
      ACME_PROXY_ADMIN__ENABLED: "true"
      ACME_PROXY_ADMIN__BIND_ADDRESS: 0.0.0.0:3001
      ACME_PROXY_ADMIN__BASE_URL: https://localhost:3001
      ACME_PROXY_ADMIN__TLS__ENABLED: "true"
      ACME_PROXY_ADMIN__TLS__CERT_PATH: /data/admin-tls/admin.pem
      ACME_PROXY_ADMIN__TLS__KEY_PATH: /data/admin-tls/admin.key
    ports:
      - "127.0.0.1:3001:3001"
    volumes:
      - ./config.toml:/etc/acme-proxy/config.toml:ro
      - ca:/data/ca:ro
      - admin-tls:/data/admin-tls
    depends_on:
      init:
        condition: service_completed_successfully

volumes:
  postgres:
  ca:
  ca-key:
  admin-tls:

Three things in it carry the rules of a split deployment:

  • init runs first, and alone. The three roles wait for it to exit successfully, so the schema is migrated and the CA exists before any of them starts. That is the initialise-once rule as Compose ordering: a role that started early would refuse a schema that is behind, and nothing but init and the worker can create a CA.
  • Only init and worker are told where the key is. The other two never read signer.local_ca.key_path, so leaving it at its default, a file that does not exist in their containers, is harmless.
  • No metrics.bind_address per role. Each container has its own network namespace, so the port clash the systemd split must avoid does not arise. To scrape them, set ACME_PROXY_METRICS__ENABLED and bind 0.0.0.0:3002 in all three; the role label tells the series apart (see Monitoring).

The image carries no HTTP client, so the acme-proxy services have no healthcheck; postgres has one because init must not start before the server accepts connections.

First start

docker compose up -d
docker compose logs init
docker compose ps -a

init should end with Profile `default` is ready. and Initialisation complete., then show as Exited (0); the other services are Up. Each of the three roles logs server_startup with its roles field. acme and admin also log the advisory server_role_no_worker — expected here, since the worker is the third container.

The CA certificate to distribute to clients is served at https://acme.example.com/profile/default/ca.pem, or copied out of the volume:

docker compose cp worker:/data/ca/ca.pem ./ca.pem

Trusting the CA covers what to do with it.

Reaching the web admin

Inside a container, a loopback bind would be the container’s own loopback, which no published port reaches. So the admin service binds every interface of the container, which needs TLS on, and Compose publishes the port on the host’s loopback alone. From an operator’s workstation, the SSH tunnel is then the way in, as with any other deployment:

$ ssh -N -L 3001:127.0.0.1:3001 ca.example.com

Open https://localhost:3001 and accept the self-signed certificate, which was generated for the host of admin.base_url. To serve the panel under a real name with a real certificate, change ACME_PROXY_ADMIN__BASE_URL and put the certificate and key in the admin-tls volume, at the two paths above.

Create the first operator once, through the running admin container. The password arrives on stdin, never in the command line:

printf '%s' "$PASSWORD" | docker compose exec -T admin acme-proxy admin user create alice

Day-to-day operation

  • The admin CLI runs in any of the role containers, which all carry the configuration and reach the database: docker compose exec worker acme-proxy order list. Use -T when piping into it.
  • Reloading the configuration after editing config.toml means signalling all three, since each reloads on its own SIGHUP: docker compose kill -s HUP acme admin worker. Each logs server_config_reloaded. See Reloading the Configuration.
  • Scale acme if you need to, never admin: its sign-in rate limiter is per process, so two replicas would each grant their own budget.
  • Back up the ca-key volume. It is the one piece of state that cannot be regenerated without redistributing trust. pg_dump covers the database, and the ca volume holds nothing the worker cannot write again.

Upgrading

Change the image: line in the x-acme-proxy block, then migrate before any role starts on the new tag:

docker compose exec -T postgres pg_dump -U acme acme > acme-before-upgrade.sql
docker compose pull
docker compose stop acme admin worker
docker compose run --rm init
docker compose up -d
docker compose logs -n 50 worker acme admin

init migrates and loads the existing CA, so it is the migration step as well: it stops the upgrade on the error rather than leaving three services in a restart loop. Upgrading has the rest of the advice, the changelog’s Breaking section first of all.

Coming from a single SQLite container

An existing all-in-one container moves across with its accounts, orders, audit trail and CA. Two things decide the order: the target of acme-proxy transfer must be migrated and empty, and the old CA files must be in place before init first runs, or it generates a new CA that none of the certificates already issued chain to.

With the old container stopped and its /data in a volume named old-data:

docker compose up -d postgres
docker compose run --rm init migrate
docker compose run --rm -T -v old-data:/old --entrypoint sh init -c '
  cp /old/ca.pem /data/ca/ca.pem &&
  cp /old/ca.key /data/ca-key/ca.key && chmod 600 /data/ca-key/ca.key &&
  target="$ACME_PROXY_DATABASE__URL" &&
  ACME_PROXY_DATABASE__URL=sqlite:///old/acme.db \
    acme-proxy transfer --yes --to "$target"'
docker compose up -d

The second command migrates without generating anything. The third runs once in the init container, the one that mounts both CA volumes, and reads the old database through old-data, copying it into the PostgreSQL URL the container was configured with. init then logs local_ca_loaded rather than local_ca_generated — check it did before pointing any client at the new deployment.