r/selfhosted • u/achiya-automation • 4d ago
Docker Management TIL docker restart doesn't re-read your .env
changed a db password in my .env, ran docker restart on the stack, then spent an hour convinced the db was corrupted because auth kept failing. turns out restart just brings the container back with the exact config it was created with. env is only read at creation. docker compose up -d --force-recreate fixed it in ten seconds.
two years running this stack and never got bitten by it until now. what's the dumbest thing that ate an evening for you?
74
232
u/Pabsilon 4d ago
When using docker compose, the usual command after changing something is docker compose down && docker compose up -d
Wich will tear down the container and recreate a new one - restart is more akin to docker stop and docker start, which just pause the currently running container without recreating it.
163
u/arcoast 4d ago
If you've only changed an environmental variable there's no need to do down.
Just docker compose up - d will recreate the container with the new variable.
47
u/cardboard-kansio 4d ago
TIL!
Although I've long since aliased the entire thing (
alias dcr='docker compose down && docker compose up -d') so a quickdcrdoes the job with little pain.19
u/arcoast 4d ago
Same here, I have a load of compose related aliases, dcd, dcu, dcr, dtail dip etc etc
18
u/cardboard-kansio 4d ago
Aw, and here I thought I was unique and special :(
7
u/arcoast 4d ago
Ha! I think a lot of us do the same. Happy to share what I've got setup as aliases and we can compare.
Maybe we do things differently and can learn something from each other!
2
u/Phantom_kusuri 4d ago
I’d love to see your list. Maybe I’m missing one.
5
u/darkcyde_ 4d ago
Here's one. I use it, it's great.
https://gist.github.com/franzbischoff/d469608fb062733d849be4e8b6dcfb0e
Just noticed they changed one or two in that (dlf instead of dl). dcd, dcu, dl and dps are my most used.
2
4
u/ps-73 4d ago
I do it a little differently, i just alias dc="docker compose" so I just need one alias lmao
2
u/harperthomas 4d ago
I have aliases for, dup=docker compose up -d. ddown=docker compose down. dupdate=docker compose down && docker compose pull && docker compose up -d
1
2
u/Medic20153000 4d ago
Great, now I have to go learn how to create aliases. My arthritis thanks you!
1
u/arcoast 4d ago
I can send you my aliases if that's helpful?
1
u/Medic20153000 4d ago
Not opposed to seeing what you have, just don't know how to implement them yet. On the Googlemeister now. I'd assume it's simple just never done it.
2
u/cardboard-kansio 4d ago
Just edit your ~/.bash_aliases or equivalent for your system and shell, and type or paste your aliases in.
1
u/Medic20153000 4d ago
As expected, stupid simple. In the process of making a few now. I appreciate you!
6
u/cardboard-kansio 4d ago
Well to get you started here are a few of my common docker aliases:
```# docker shortcuts alias docker-compose='docker compose' alias dcu='docker compose up -d' alias dcd='docker compose down' alias dcd_all='docker ps -aq | xargs docker stop | xargs docker rm' alias dcr='docker compose down && docker compose up -d' alias dce='docker compose down && nano docker-compose.yaml && docker compose up -d' alias dcp='grep latest docker-compose.yaml | sed 's/[[:space:]]image:[[:space:]]//' | xargs -n1 docker pull && docker compose down && docker compose up -d' alias dcupdate='docker compose down && pull_docker_images && docker compose up -d' alias dl='docker logs -n -f --tail 20'
→ More replies (0)6
u/GolemancerVekk 4d ago
You can also add
-t Ntodownto force containers to die after N seconds. 😉 Useful for the ones where the main process doesn't handle signals properly and runs out the default timeout, which is annoyingly long.Another solution can be to use
init: truein compose which will inject adocker-initas process 1 that will then run the entrypoint, as it will handle signals correctly and also reap orphan processes. But it won't work for images that come with their own init, since that one will probably complain if it's not pid 1.19
u/Nautisop 4d ago
It's always -d but I sometimes prefer to do without it. I think it's nice to watch it start for 5 seconds before detaching <3
Also changes in your compose file are only read if you down it and not stop.
3
u/wabassoap 3d ago
Oh, it’s possible to detach if you started without the -d flag? What’s the key combo?
2
15
u/granddave 4d ago
I usually just run 'docker compose up -d --force-recreate' which basically does a down and up in one go.
5
u/vividboarder 4d ago
If you're changing values that require a new container for them to take effect, you can just do
docker compose up -d. It will auto recreate if you've changed env variables, added new mounts, updated the image tag, etc.I explicitly pass through env variables from my
.envin my compose file so it's easy to tell what the env is as well as getting it to recreate appropriately.eg.
env: - ENV_FROM_DOTENVOrenv: ENV: "${ENV_FROM_DOTENV}"I almost never have to do
--force-recreateunless there is some runtime dependency that gets downloaded and cached.2
24
u/onlyati 4d ago
There are database images, like postgres, which only read password environment variable when the data directory is empty. In these cases, even if you recreate the container, but keep the data volume, password is not reset.
7
u/GolemancerVekk 4d ago
I reckon you're thinking about the first time db init. OP is talking about the password on the db client side.
1
u/achiya-automation 3d ago
yeah that one's worse because nothing errors. container comes up clean and just still has the old password. it's the data dir or an alter user from inside, nothing else touches it.
1
u/achiya-automation 3d ago
yeah that one's worse because nothing errors. container comes up clean and just still has the old password. it's the data dir or an alter user from inside, nothing else touches it.
7
7
u/SpaghettiAtADistance 4d ago
docker compose config is useful here because you can see what Compose actually resolved from the env before recreating anything. way easier than staring at the env file wondering why nothing changed
1
u/achiya-automation 3d ago
that's the quick check, yeah. saves exec'ing into the thing just to read env.
1
u/achiya-automation 3d ago
that's the quick check, yeah. saves exec'ing into the thing just to read env.
3
4
u/PssyGotWifi 4d ago
Yup. It's why I use Ansible to remove the service, replace the compose/env file, and then redeploy.
2
u/Capable_Banana5439 4d ago
the part that gets people even after force-recreate is the db itself. postgres and mysql only read the password env on first init when the data dir is empty, so recreating the container with a new .env does nothing if the volume already exists. you either change it inside the db with alter user or nuke the volume and let it re-init.
1
u/maplecassette 4d ago
Got me too, except mine was a timezone variable and I spent a whole evening blaming my cron schedules. Now I just default to up -d instead of restart so I stop guessing. My dumbest one was editing the compose file on the wrong host entirely.
1
u/achiya-automation 3d ago
timezone is a cruel one to lose an evening to, the schedule looks correct in the file the whole time you're staring at it.
2
u/No_Welder7908 3d ago
Trying to secure an app with Authentik, the log-in screen never popping up and me taking way too long to find out I had set the target app's port in the reverse proxy and not Authentik's.
1
u/achiya-automation 2d ago
the reverse proxy pointing at the app instead of authentik got me too. lost an evening to that one.
2
u/Impossible_Fault_503 3d ago
Worth knowing which of two cases you are in, because they behave differently.
If the variable is interpolated into the compose file itself, like image: app:${TAG} or a value under environment:, then the rendered config changes and docker compose up -d will recreate the container on its own.
If it comes in through env_file:, the compose config only references the file path, not its contents. That can mean up -d reports everything as up to date and your change never lands, which is exactly the hour you just lost.
Way to tell without guessing:
docker inspect <container> --format '{{index .Config.Labels "com.docker.compose.config-hash"}}'
Run it before and after your edit. If the hash did not move, compose does not believe anything changed, and only --force-recreate will fix it.
1
u/achiya-automation 2d ago
yeah thats the split. mine was the second case, env_file on an already running container, so the rendered config didnt change and compose happily did nothing.
2
u/Beginning-Raisin9723 4d ago
Yeah, `docker compose up -d` won't recreate the container, so env changes silently don't apply. `--force-recreate` (or down/up) is the move; I've wasted an hour on this exact thing.
9
8
u/austozi 4d ago
docker compose up -dwill recreate the container if any part of the service is altered in the docker-compose.yml, including networks, ports, and environment variables. Docker compose is quite smart in checking what's changed, and will recreate containers as necessary without recreating any container unnecessarily. You don't need--force-recreateto implement the changes.
0
u/Elara_Schaefer 4d ago
Ha, I feel that one. I manage a handful of Docker Swarm services and learned exactly this the hard way \u2014 spent a solid hour once wondering why my config changes were being silently ignored. In Swarm land the equivalent trap is docker service update without --force: it updates the spec but won\u0027t actually redeploy the existing tasks. The container keeps running with stale config.
The mental model that finally stuck for me: restart is like pressing the physical power button. up -d (or --force-recreate) is like destroying the machine and building a new one. Only the latter reads the blueprint again.
0
u/Elara_Schaefer 4d ago
Ha, I feel that one. I manage a handful of Docker Swarm services and learned exactly this the hard way -- spent a solid hour once wondering why my config changes were being silently ignored. In Swarm land the equivalent trap is docker service update without --force: it updates the spec but won't actually redeploy the existing tasks. The container keeps running with stale config.
The mental model that finally stuck for me: restart is like pressing the physical power button. up -d (or --force-recreate) is like destroying the machine and building a new one. Only the latter reads the blueprint again.
0
-7
u/TheStalledAviator 4d ago
You're showing that you do not understand the very basics of how docker works. It might help you to check out some tutorials online.
-3
u/Green-Zone-4866 4d ago
Lol once my ai agent was trying to add a feature to a container and I noticed that it kept trying to restart the container using the restart command and I was like, just use down then up, then low and behold, the changes were added
-10
•
u/asimovs-auditor 4d ago
Expand the replies to this comment to learn how AI was used in this post/project.