Rotating Docker Swarm secrets and configs without downtime
Verify tmpfs secret mounts, immutability errors, zero-downtime start-first rotation, and rollback for Docker Swarm secrets and configs.
Docker Swarm
secretandconfigobjects are immutable once created. This post verifies how Swarm mounts secrets in memory withtmpfs, why in-place edits and in-use deletions fail, and how versioned names withstart-firstrolling updates rotate and roll back values with zero failed requests.
Every command and output below ran on an isolated 3-node DinD Swarm cluster (
docker:29-dind, Docker Engine29.10.1,linux/arm64) usingnginx:1.27-alpine(NGINX_VERSION=1.27.5). The stringsTEST_DUMMY_SECRET_V1_NOT_REALandTEST_DUMMY_SECRET_V2_ROTATEDare dummy test values, not real credentials.
How Swarm mounts secrets and configs
In standalone Docker Compose, a secrets entry bind-mounts a file from the host filesystem into /run/secrets/ inside the container. I covered how that compares with .env and environment precedence in my post on Docker Compose environment precedence and secrets.
Swarm handles secrets differently. Manager nodes store secret payloads in the encrypted Raft log and send them over mutual TLS only to worker nodes running tasks that need them. On the worker, Swarm mounts each secret into /run/secrets/<target> using an in-memory tmpfs filesystem instead of writing plaintext to disk. When the task stops, that tmpfs mount disappears from memory.
Swarm config objects serve non-sensitive files such as Nginx configs or application properties. They live unencrypted in the Raft log and can mount at any absolute path inside the container, such as /etc/app/config.env.
| Mechanism | Storage and encryption | Container mount type | Default path and mode | Plaintext in docker inspect |
|---|---|---|---|---|
Swarm secret | Manager Raft log (encrypted at rest) | In-memory tmpfs (ro) | /run/secrets/<name> (0444) | Hidden (metadata only) |
Swarm config | Manager Raft log (unencrypted) | Read-only file mount | Custom path or /<name> (0444) | Visible via config inspect |
Standalone Compose secrets | Local host disk file | Host file bind mount | /run/secrets/<name> | Not in .Config.Env |
Environment variable (-e) | Plaintext in service spec | Process env | None | Exposed in .Config.Env |
Native Swarm secrets and configs aren’t the right fit for every workload. Avoid them when you need payloads larger than Swarm’s 500 KB object limit, live in-place file hot-reloading without replacing tasks, or dynamic short-lived database leases and per-read audit logs. For those requirements, pair Swarm with an external store like HashiCorp Vault or a cloud secrets manager.
Deploying v1 and verifying tmpfs isolation
First, create a dummy v1 secret (app_secret_v1) and config (app_config_v1), then deploy a two-replica service that writes both values into an Nginx response page on startup.
1
2
3
4
5
6
7
8
9
10
11
# 1) Create v1 secret and config (dummy test values)
printf "TEST_DUMMY_SECRET_V1_NOT_REAL" | docker secret create app_secret_v1 -
printf "APP_MODE=canary-v1;TIMEOUT=30s" | docker config create app_config_v1 -
# 2) Deploy with start-first rolling update settings
docker service create --name secret-demo --replicas 2 \
--publish published=8090,target=80 \
--secret source=app_secret_v1,target=app_secret \
--config source=app_config_v1,target=/etc/app/config.env \
--update-order start-first --update-parallelism 1 --update-delay 2s \
nginx:1.27-alpine sh -c 'printf "host=%s\nconfig=%s\nsecret=%s\n" "$(hostname)" "$(cat /etc/app/config.env)" "$(cat /run/secrets/app_secret)" > /usr/share/nginx/html/index.html && exec nginx -g "daemon off;"'
Keeping the target fixed (app_secret and /etc/app/config.env) is what makes rotation painless. When you later swap the source object to app_secret_v2, the application still reads the exact same path.
Inspecting a running task container confirms the tmpfs mount, file permissions, and absence of plaintext in environment variables:
1
2
3
4
5
6
7
8
9
10
11
12
# Check /proc/mounts inside the task container
$ grep '/run/secrets' /proc/mounts
tmpfs /run/secrets/app_secret tmpfs ro,relatime 0 0
# Check mounted file permissions (default root:root, 0444)
$ ls -l /run/secrets/app_secret /etc/app/config.env
-r--r--r-- 1 root root 30 Dec 5 06:48 /etc/app/config.env
-r--r--r-- 1 root root 29 Dec 5 06:48 /run/secrets/app_secret
# Check container environment (.Config.Env) — no secret plaintext
$ docker inspect <container_id> --format '{{json .Config.Env}}'
["PATH=/usr/local/sbin:...","NGINX_VERSION=1.27.5","PKG_RELEASE=1","DYNPKG_RELEASE=1","NJS_VERSION=0.8.10","NJS_RELEASE=1"]
/run/secrets/app_secret mounts as a read-only tmpfs (ro,relatime), and .Config.Env contains no secret value. Running docker secret inspect app_secret_v1 also returns metadata only, never the payload.
Immutability and in-use deletion errors
When a password or config changes, you can’t edit the existing Swarm object in place. docker secret --help and docker config --help expose only create, inspect, ls, and rm—there is no update subcommand.
Attempting to delete an active object so you can recreate it with the same name fails immediately:
1
2
3
4
5
$ docker secret rm app_secret_v1
Error response from daemon: rpc error: code = InvalidArgument desc = secret 'app_secret_v1' is in use by the following service: secret-demo
$ docker config rm app_config_v1
Error response from daemon: rpc error: code = InvalidArgument desc = config 'app_config_v1' is in use by the following service: secret-demo
Both commands exit with code 1. Swarm enforces reference integrity so running tasks keep their mounted files and docker service rollback can still point back to the previous version.
Zero-downtime rotation with versioned names and start-first
The standard rotation pattern uses versioned object names (_v2). Create the new secret and config first, then swap references in a single docker service update call using --secret-rm/--secret-add and --config-rm/--config-add while preserving the same target paths.
flowchart TD
A["2 v1 tasks running"] --> B["Create v2 secret & config"]
B --> C["service update (start-first)"]
C --> D["Start v2 task first"]
D --> E["Overlap v1/v2, stop v1"]
Swarm defaults to --update-order stop-first, which stops an old task before starting its replacement. Setting --update-order start-first with --update-parallelism 1 starts each new v2 task first and shuts down the old v1 task only after the replacement is running. For the fundamentals of Swarm task replacement and rolling updates, see my earlier post on Docker Swarm service replicas and rescheduling.
I ran the rotation while polling the service over HTTP 12 times at 0.5-second intervals:
1
2
3
4
5
6
7
8
9
10
# 1) Create v2 secret and config (dummy test values)
printf "TEST_DUMMY_SECRET_V2_ROTATED" | docker secret create app_secret_v2 -
printf "APP_MODE=stable-v2;TIMEOUT=15s" | docker config create app_config_v2 -
# 2) Swap v1 for v2 on the same target paths with start-first
docker service update \
--secret-rm app_secret_v1 --secret-add source=app_secret_v2,target=app_secret \
--config-rm app_config_v1 --config-add source=app_config_v2,target=/etc/app/config.env \
--update-order start-first --update-parallelism 1 --update-delay 2s \
secret-demo
Here is the excerpt from the HTTP polling loop and docker service ps secret-demo during the transition:
1
2
3
4
5
6
7
8
9
10
11
12
13
# 12 HTTP polls at 0.5s intervals (0 failures, v1/v2 overlap observed)
sample#9: host=6c299d3846f8 config=APP_MODE=canary-v1;TIMEOUT=30s secret=TEST_DUMMY_SECRET_V1_NOT_REAL
sample#10: host=c5f87161b70b config=APP_MODE=stable-v2;TIMEOUT=15s secret=TEST_DUMMY_SECRET_V2_ROTATED
sample#11: host=6c299d3846f8 config=APP_MODE=canary-v1;TIMEOUT=30s secret=TEST_DUMMY_SECRET_V1_NOT_REAL
sample#12: host=c5f87161b70b config=APP_MODE=stable-v2;TIMEOUT=15s secret=TEST_DUMMY_SECRET_V2_ROTATED
TOTAL_FAILS=0
# docker service ps during rotation — new task Running before old task Shutdown
NAME NODE DESIRED STATE CURRENT STATE
secret-demo.1 swarm-wrk2 Running Running 2 seconds ago
\_ secret-demo.1 swarm-wrk1 Shutdown Running 2 seconds ago
secret-demo.2 swarm-wrk1 Running Running less than a second ago
\_ secret-demo.2 swarm-mgr1 Shutdown Running less than a second ago
All 12 requests succeeded (TOTAL_FAILS=0). While secret-demo.1 switched to v2, secret-demo.2 continued serving v1, producing a clean v1/v2 overlap before the second replica converged to v2.
Rolling back to v1 and cleaning up unused versions
If a rotated config or credential breaks production, keeping the previous version objects in Swarm lets you revert in one command:
1
2
3
4
5
6
# 1) Revert to the previous service spec (v1)
docker service rollback secret-demo
# 2) Remove the now-unused v2 objects
docker secret rm app_secret_v2
docker config rm app_config_v2
In this test, docker service rollback reached rollback_completed in 13 seconds, and both replicas returned v1 values again:
1
2
3
# HTTP responses after rollback
post-rollback#1: host=0d03ba0d8ce1 config=APP_MODE=canary-v1;TIMEOUT=30s secret=TEST_DUMMY_SECRET_V1_NOT_REAL
post-rollback#2: host=514979d56aca config=APP_MODE=canary-v1;TIMEOUT=30s secret=TEST_DUMMY_SECRET_V1_NOT_REAL
Once the service pointed back to v1, app_secret_v2 and app_config_v2 were no longer referenced and rm removed both without error.
Production pitfalls and checklist
Three operational traps to watch for
- Don’t delete the old version too early: If you remove
app_secret_v1immediately after rollout,docker service rollbackcan’t restore the previous spec. Retain at least one previous version until post-deploy checks pass.- Plan for
v1/v2credential overlap: Becausestart-firstruns old and new tasks simultaneously, rotating a database password or API key requires a grace window where both credentials remain valid until every task finishes switching.- Tighten permissions for non-root containers: Swarm mounts secrets as
root:rootwith mode0444by default. To restrict read access to a specific non-root user, pass--secret source=app_secret_v2,target=app_secret,uid=1000,gid=1000,mode=0400.
Before rolling out Swarm secret and config rotation in production, verify these four items:
- Keep the container
targetpath constant and put version or hash suffixes (_v2,_20260926) only on the source object names. - Read secrets from
/run/secrets/<target>(or via*_FILEenvironment variables) rather than injecting plaintext into.Config.Env. - Pair
--update-order start-firstwith--update-parallelism,--update-delay, and a containerHEALTHCHECKso old tasks drain only after new tasks are ready. - Separate cleanup of previous secret/config versions into a post-verification step so
docker service rollbackremains usable.