Why
A short nginx config usually sits in a separate file next to compose.yaml and gets bind-mounted into the container:
services:
web:
image: nginx:1.29-alpine
volumes:
- ./site.conf:/etc/nginx/conf.d/default.conf:ro
Compose doesn’t read that file; it only passes it into the container. Variables from .env don’t work inside it:
nothing substitutes server_name ${DOMAIN};, and nginx itself doesn’t expand environment variables in its config. You
end up editing the domain by hand on each server, and that local edit then blocks git pull.
Text in content: is part of the Compose file itself. Compose expands variables in it the same way it does in image:
or ports: (Required Compose variables): one config in git for every server, with each server’s domain in .env. The
nginx image also has its own /etc/nginx/templates/ mechanism, whereas content works with any program.
How to write it
Put the config text in the top-level configs: section at the end of the file, and point the service at it with a
target path inside the container:
services:
web:
image: nginx:1.29-alpine
configs:
- source: site # name from the configs section
target: /etc/nginx/conf.d/default.conf # destination path in the container
configs:
site:
content: |
server {
listen 80;
server_name ${DOMAIN};
root /usr/share/nginx/html;
}
With DOMAIN=example.org in .env, the container gets /etc/nginx/conf.d/default.conf containing
server_name example.org;.
Compose copies this file into the container on creation — no bind mount, read-only (-r--r--r--, owned by root). Using
file: ./site.conf instead of content goes back to mounting a single file with all its pitfalls
(Single-file mounts), and variables aren’t substituted there.
Escape $ with a double dollar sign
Nginx has its own $ variables: $uri, $host, $remote_addr. Compose treats every $ in the Compose file as its
own, including inside content: it replaces $uri with an empty string and warns The "uri" variable is not set.
Double the sign so it reaches nginx. Compose turns $$ into a single $ and leaves it alone
(The $ sign):
configs:
site:
content: |
server {
listen 80;
server_name ${DOMAIN};
root /usr/share/nginx/html;
location / {
try_files $$uri $$uri/ /index.html;
}
}
The container gets try_files $uri $uri/ /index.html;, while ${DOMAIN} above it still expands from .env.
Recreate after editing
up -d recreates a container only when the service definition changed (The compose config hash). The content text
isn’t part of that definition: edit the config, run up -d, and Compose prints Running while the old copy stays in
the container. Recreate it explicitly:
docker compose up -d --force-recreate web
If something on the service itself also changed — the image, an environment variable — the container gets recreated and picks up the new text without the flag.
If it doesn’t work
You edited content, up -d prints Running, yet nginx serves the old config
- Cause
- The
contenttext isn't part of the service definition thatup -dchecks when deciding whether to recreate the container. Compose sees nothing to change, so the old copy stays inside. - Fix
docker compose up -d --force-recreate web.
The config has a blank space where $uri was, and startup warns The "uri" variable is not set
- Cause
- Compose treated
$urias its own variable and substituted an empty string. - Fix
- Write nginx variables with two dollar signs:
$$uri,$$host. A single$ends up in the container.
How to check
Inspect what landed in the container and test it with nginx:
docker compose exec web cat /etc/nginx/conf.d/default.conf
docker compose exec web nginx -t
# nginx: configuration file /etc/nginx/nginx.conf test is successful