Skip to content
Edit this page View source of this page

Swarm Mode

Doco-CD also supports Docker Swarm mode, which allows you to manage a cluster of Docker engines as a single virtual engine. This is useful for deploying applications across multiple nodes and ensuring high availability.

If the Docker daemon is running in Swarm mode, doco-cd will detect this automatically and deploy deployments as Swarm stacks instead of simple Compose projects.

Set swarm.enabled in an individual deployment configuration to override this selection: true deploys a Swarm stack and false deploys a Compose project. This lets both deployment types run through one doco-cd instance on the same Swarm manager. If omitted, auto-detection is used.

You can overwrite this globally to always deploy as Compose projects while running doco-cd in a Swarm environment by setting the DOCKER_SWARM_FEATURES environment variable to false (See the Docker Settings).

The deployment happens the same way as with Docker Compose projects, see the Deploy Settings

Replica count

Each deployment sets the replica count of a service to its deploy.replicas value. To let an external tool, for example an autoscaler, control the replica count of a replicated service, set the cd.doco.deployment.preserve_replicas service label to true. See Let an external tool control Swarm replicas.

Configs and Secrets

When deploying configs or secrets in Swarm mode, doco-cd will add a suffix to the name of configs and secrets. This suffix is a shortened SHA-256 hash of the config or secret content, which allows doco-cd to rotate them if their contents change. After rotating, doco-cd prunes old versions of configs or secrets according to the configured retention.

How many old revisions are kept is configurable via:

  • Deploy config: swarm.config_retention and swarm.secret_retention
  • Global config: DOCKER_SWARM_CONFIG_RETENTION and DOCKER_SWARM_SECRET_RETENTION

Set retention to -1 to disable automatic pruning for that scope and keep old revisions until you remove them manually.

For example, if you deploy a stack named myapp with a config named db-settings and the content of the config is hello world, doco-cd will create a config named myapp_db-settings_a948904f in the Swarm cluster.

If you later change the content of the config to hello universe, doco-cd will create a new config named myapp_db-settings_0b5c6934, redeploy the service/container with the new config and then prune older config revisions according to the configured retention.

Values from variables and inline content

Besides file, Swarm stacks support these sources for top-level secrets and configs:

  • environment: secrets and configs read the value from a variable.
  • content: configs use the inline value.

doco-cd resolves the variable from the deploy config environment, the env_files (default .env) and external secrets. SOPS-encrypted dotenv files are decrypted before use. doco-cd sends the value to the Swarm API from memory. It does not write a secret or config file for it.

Without an explicit name, the Swarm object is named <stack>_<key>_<hash>, where <key> is the key in the top-level secrets or configs section. With an explicit name, the Swarm object is named <name>_<hash>. When the value changes, the hash changes. doco-cd then creates a new object, updates the services that use it and prunes old revisions as described above.

These objects are managed by one stack. Use a unique name for each stack, or let doco-cd name the object. A deployment fails if it would take over an object already owned by another stack. To share one object across stacks, provision it separately and reference it with external: true in each stack.

Swarm secret from a SOPS-encrypted dotenv file

Encrypt the dotenv file with SOPS:

sops encrypt --age age1g3lcl... --input-type dotenv --output-type dotenv .env.plain > .env
.env (before encryption)
DB_PASSWORD=some-secret-password
docker-compose.yml
services:
  app:
    image: myapp:latest
    secrets:
      - db_password
    configs:
      - source: app_settings
        target: /etc/app/settings.conf

secrets:
  db_password:
    environment: DB_PASSWORD # Read from the decrypted .env file

configs:
  app_settings:
    content: |
      log_level=info

For a stack named myapp, doco-cd creates a secret named like myapp_db_password_<hash> and a config named like myapp_app_settings_<hash>.

Note

  • If a variable that a secret or config uses is not set, the deployment fails. The error names the variable but not a value.
  • driver cannot be combined with environment or content.
  • The hash in the name is a shortened SHA-256 hash of the value. Everyone who can list secrets or configs in the Swarm can read it. Use secret values that are hard to guess, because a short or common value can be found from its hash.

Image pruning

With prune_images enabled, doco-cd runs a global job on every node after a deployment. It prunes dangling images and removes unused images whose only name is a single digest reference, which is how Swarm pulls service images. Images that any container still uses, running or stopped, are kept, and so are images pulled within the last hour, which another deployment may be about to use. Images that also have a tag or a digest reference in another repository are kept as well.

By default, the job uses the official Docker CLI image pinned to a multi-platform digest. Each node pulls the variant for its platform from the same image index. To use another image, for example from a registry mirror, set DOCKER_SWARM_HELPER_IMAGE. The image needs sh, awk, date and the docker CLI. Make sure it supports all platforms in your Swarm. Use a digest reference such as image:tag@sha256:... for reproducible pulls. If Swarm features are enabled, doco-cd warns at startup about overrides without a digest. doco-cd sends credentials for the image's registry from its Docker config. If it cannot retrieve them, the job attempts an anonymous pull instead of reusing credentials from an earlier run.

Swarm keeps the containers of replaced tasks as task history so that a service can be rolled back, and those containers keep their images in use. With the default task history limit of 5, the images a deployment replaced therefore stay on the nodes until Swarm drops the old tasks. To free them on the next prune, lower the limit on a manager node:

docker swarm update --task-history-limit 1

docker service rollback still works with a lower limit or with 0, because Swarm rolls back to the previous service spec and pulls its image again if it is no longer on the node, so the registry has to be reachable.

Docker API Permissions

Swarm deployments need access to more Docker API resources than Compose deployments, namely services, tasks, nodes, configs and secrets.

See Docker API Permissions if you run doco-cd behind a Docker socket proxy.