Why the container sees the old file
A file mount is tied to the file itself — its inode — not to its name. There are two ways to change a file:
| Method | What happens | Does the container see the change? |
|---|---|---|
write into the same file: cp new file, cat new > file, >> |
same inode, new contents | yes |
create a new one and swap it in: git pull, git checkout, sed -i, mv new file, saving in many editors |
a new inode under the old name; the old one is deleted, but the container keeps holding it | no |
What to do about it
- Mount configs that change through git as a directory (
./conf.d:/etc/nginx/conf.d:ro): a directory sees new files in it. That’s how Traefik readsdynamic/. - If a file is mounted on its own anyway, run
docker compose restart <service>aftergit pull.nginx -s reloadwon’t help: nginx rereads the same old file. - A script that regularly updates such a file writes to a temp file, checks it and copies it over —
cp, notmv:
tmp=$(mktemp); trap 'rm -f "$tmp"' EXIT
generate > "$tmp" # build the new contents
[ -f "$out" ] && cp "$out" "$out.prev" # a copy for rollback
cp "$tmp" "$out" # cp, not mv: same inode — the container sees it
if docker exec nginx nginx -t >/dev/null 2>&1; then
docker exec nginx nginx -s reload
else
cp "$out.prev" "$out" # the config failed the check — put the old one back
exit 1
fi
A directory or a file
| How to mount | When |
|---|---|
| a directory | anything that changes: configs from git, files that scripts update |
a single file, :ro |
the file hardly ever changes (nginx.conf, php.ini), and a restart after an edit is no problem |
| a single file, updated in place | generated lists (IP addresses, blocklists) when a restart is unwelcome |
If it doesn’t work
Updated nginx.conf with git pull, ran nginx -s reload — nothing changed
- Cause
git pullreplaced the file with a new one, but the container holds the old one.reloadrereads that same old file.- Fix
docker compose restart nginx.
A script updates an address list, reload succeeds, but nginx has the old list
- Cause
- The script writes a new file and runs
mv: a different file now sits under the old name, and the container doesn’t see it. - Fix
- Replace
mvwith copying over —cp, as in the script above.
The file nginx reads hasn’t changed in months, while another one grows next to it
- Cause
- The script writes to the wrong path.
- Fix
- Compare the path in the script with the volume path in compose. To make the script work from any directory, start it with
cd "$(dirname "$0")".
Edited a mounted file in vim right on the server — the container has the old version
- Cause
- vim saves through a new file with
backupcopy=noand, whenever it can, withauto— Vim's default. - Fix
- A restart fixes it now. To keep it from happening again — edit through git and mount a directory; in vim itself —
:set backupcopy=yes.
How to check
Compare the contents on the host and in the container:
md5sum ./nginx.conf
docker exec nginx md5sum /etc/nginx/nginx.conf # should match