Configs
Containers need configuration files: an nginx.conf, a prometheus.yml, a logging config. Baking them into the image means a rebuild for every tweak; bind-mounting them from the host means the file must exist on every node (Volumes in a Swarm). Swarm offers a third way: configs, small files stored in the swarm itself and delivered into the tasks that ask for them, on whatever node they run.
How configs work
- You create a config once:
docker config create nginx_conf_v1 ./nginx.conf. The managers store it in the Raft log, replicated to every manager. - A service lists the configs it needs. When a task starts, its node receives the config from a manager and mounts it as a file in the container.
- Configs are immutable: you can't edit one, only create a new one and switch the service to it.
- Size limit: 500 KB per config. They are for configuration, not for data.
- Configs are not encrypted at rest in a special way and show up in
docker config inspectin clear text. Anything secret belongs in a secret.
$ docker config create nginx_conf_v1 ./nginx.conf
k2v8x0r4s7m1q9w3e5t6y8u0i
$ docker config ls
ID NAME CREATED UPDATED
k2v8x0r4s7m1q9w3e5t6y8u0i nginx_conf_v1 10 seconds ago 10 seconds ago
$ docker service create --name web --config source=nginx_conf_v1,target=/etc/nginx/nginx.conf,mode=0440 nginx:1.27-alpine(sample output.) Without target, a Linux container gets the config at /<config-name> (here /nginx_conf_v1). Files are owned by root with mode 0444 unless you set uid, gid and mode.
Configs in a stack file
A stack file declares configs at the top level and attaches them to services. file: reads the content from a file next to the stack file when you run docker stack deploy.
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
web:
image: nginx:1.27-alpine
configs:
- source: nginx_conf
target: /etc/nginx/nginx.conf
mode: 0440
- source: site
target: /etc/nginx/conf.d/default.conf
- motd
configs:
nginx_conf:
file: ./nginx.conf
site:
file: ./site.conf
motd:
external: trueversion: "3.13"
services:
web:
configs:
- source: nginx_conf
target: /etc/nginx/nginx.conf
mode: 288
- source: site
target: /etc/nginx/conf.d/default.conf
- source: motd
image: nginx:1.27-alpine
configs:
motd:
name: motd
external: true
nginx_conf:
file: /home/ada/demo/nginx.conf
site:
file: /home/ada/demo/site.conf
Relative paths like ./nginx.conf are resolved against the stack file's folder, so docker stack config prints the absolute path on the machine that runs it (here the example folder /home/ada/demo). Note also that stack config doesn't check that the files exist; docker stack deploy reads them and fails if they are missing. The motd config is external: it was created earlier with docker config create and the stack only refers to it.
In the stack shop, the config nginx_conf is created as shop_nginx_conf. The mode: 0440 was written in octal and printed back as decimal 288: YAML 1.1 reads 0440 as an octal number. Quote it ("0440") or write the decimal value if you want the file to read clearly.
Updating a config: rotate by name
Because a config can't change, updating one means: create a new config with a new name, point the service at it, and remove the old one once nothing uses it. Swarm treats the switch as a service update, so it rolls out with the service's normal update policy.
$ docker config create nginx_conf_v2 ./nginx.conf
$ docker service update \
--config-rm nginx_conf_v1 \
--config-add source=nginx_conf_v2,target=/etc/nginx/nginx.conf \
web
$ docker config rm nginx_conf_v1In a stack file, the clean way is a name: that contains a version variable. Change the variable, redeploy, and the stack creates a new config and rolls the service:
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
web:
image: nginx:1.27-alpine
configs:
- source: nginx_conf
target: /etc/nginx/nginx.conf
configs:
nginx_conf:
name: nginx_conf_${NGINX_CONF_VERSION:-v1}
file: /srv/shop/nginx.confversion: "3.13"
services:
web:
configs:
- source: nginx_conf
target: /etc/nginx/nginx.conf
image: nginx:1.27-alpine
configs:
nginx_conf:
name: nginx_conf_v1
file: /srv/shop/nginx.conf
The service still refers to the config by its key (nginx_conf); only the swarm object's name changes. Without a new name, docker stack deploy would try to update the existing config and fail with an error saying only labels can be updated. A popular variant uses a checksum of the file as the version, so the name changes exactly when the content does:
import hashlib
def config_name(base, content):
return f"{base}_{hashlib.sha256(content.encode()).hexdigest()[:8]}"
v1 = "worker_processes auto;\nevents { worker_connections 1024; }\n"
v2 = "worker_processes auto;\nevents { worker_connections 4096; }\n"
deployed = set()
for content in (v1, v1, v2):
name = config_name("nginx_conf", content)
action = "unchanged, no rollout" if name in deployed else "new config -> rolling update"
deployed.add(name)
print(f"{name}: {action}")nginx_conf_54c1c472: new config -> rolling update
nginx_conf_54c1c472: unchanged, no rollout
nginx_conf_29eb9593: new config -> rolling update
A CI job computes the hash, exports it as NGINX_CONF_VERSION and runs docker stack deploy. Old configs stay behind until you remove them; docker config rm refuses while a service still uses one, so cleanup is safe.
Templated configs
With template_driver: golang, Swarm renders the file as a Go template when the task starts, filling in placeholders such as {{ .Service.Name }}, {{ .Task.Slot }} and {{ .Node.Hostname }}, and even secrets ({{ secret "db_password" }}) and environment variables ({{ env "LOG_LEVEL" }}). Each task gets its own rendered copy:
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
agent:
image: ghcr.io/acme/agent:1.8
configs:
- source: agent_conf
target: /etc/agent.yml
deploy:
mode: global
configs:
agent_conf:
file: /srv/shop/agent.yml.tmpl
template_driver: golangversion: "3.13"
services:
agent:
configs:
- source: agent_conf
target: /etc/agent.yml
deploy:
mode: global
image: ghcr.io/acme/agent:1.8
configs:
agent_conf:
file: /srv/shop/agent.yml.tmpl
template_driver: golang
# /srv/shop/agent.yml.tmpl
node: {{ .Node.Hostname }}
service: {{ .Service.Name }}
task_slot: {{ .Task.Slot }}template = "node: {node}\nservice: {service}\ntask_slot: {slot}"
# Global services have no slot numbers: Swarm fills .Task.Slot with the node's ID instead.
node_ids = {"node-1": "7xk2m9p4q1", "node-2": "c3v8b5n0r6", "node-3": "w1e4t7y2u9"}
for node, node_id in node_ids.items():
print(f"--- /etc/agent.yml in the task on {node}")
print(template.format(node=node, service="shop_agent", slot=node_id))--- /etc/agent.yml in the task on node-1
node: node-1
service: shop_agent
task_slot: 7xk2m9p4q1
--- /etc/agent.yml in the task on node-2
node: node-2
service: shop_agent
task_slot: c3v8b5n0r6
--- /etc/agent.yml in the task on node-3
node: node-3
service: shop_agent
task_slot: w1e4t7y2u9
Configs vs the alternatives
| Need | Use |
|---|---|
| a few settings as key/value | environment: in the stack file |
| a whole config file, not secret | a config |
| passwords, keys, certificates | a secret |
| large or frequently changing data | a volume, or bake it into the image |
Note: Compose (single host) supports
configs:as well, implemented with bind mounts, plus inlinecontent:andenvironment:sources that Swarm stacks do not accept (Compose configs & secrets). Kubernetes' equivalent is the ConfigMap.
Learn more: Secrets · Updating Stacks
Test yourself
You need to change the content of config nginx_conf_v1 used by a running service. What do you do?
docker config update nginx_conf_v1- Edit the file inside the container
- Create
nginx_conf_v2and update the service to use it instead - Restart the managers
Where does a config without a target appear in a Linux container?
/run/configs/<name>/<config-name>/etc/<config-name>- Nowhere, target is required
Why should a database password not be stored as a config?
- Configs can't hold strings
- Configs are readable in clear text through
docker config inspect; secrets are encrypted and mounted in memory - Configs only work on managers
- Configs are limited to 10 bytes