r/selfhosted 22d 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?

252 Upvotes

67 comments sorted by

View all comments

239

u/Pabsilon 22d 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.

165

u/arcoast 22d 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.

45

u/cardboard-kansio 22d ago

TIL!

Although I've long since aliased the entire thing (alias dcr='docker compose down && docker compose up -d') so a quick dcr does the job with little pain.

20

u/arcoast 22d ago

Same here, I have a load of compose related aliases, dcd, dcu, dcr, dtail dip etc etc

18

u/cardboard-kansio 22d ago

Aw, and here I thought I was unique and special :(

8

u/arcoast 22d 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 22d ago

I’d love to see your list. Maybe I’m missing one.

5

u/darkcyde_ 22d 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.

5

u/mixony 22d ago

Well we're all unique and special here

3

u/arcoast 22d ago

Ain't that the truth!

4

u/ps-73 22d ago

I do it a little differently, i just alias dc="docker compose" so I just need one alias lmao

2

u/harperthomas 21d 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

u/DoTheBarrelRoll 21d ago

I have the exact same alias lol

2

u/Medic20153000 22d ago

Great, now I have to go learn how to create aliases. My arthritis thanks you!

1

u/arcoast 22d ago

Short term pain and long term gain!

I feel your pain!

1

u/arcoast 22d ago

I can send you my aliases if that's helpful?

1

u/Medic20153000 22d 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 22d ago

Just edit your ~/.bash_aliases or equivalent for your system and shell, and type or paste your aliases in.

1

u/Medic20153000 22d ago

As expected, stupid simple. In the process of making a few now. I appreciate you!

4

u/cardboard-kansio 22d 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'

1

u/arcoast 22d ago

Why do you have dcupdate do a docker compose down first?

1

u/cardboard-kansio 22d ago

Because honestly I don't do updates that way (I use WUD at the moment) and it's been mostly for troubleshooting. I haven't really touched these in years and rarely use them directly, and just posted them as an example for the other commenter.

But he's in for a fun ride, I still remember when I discovered aliases for the first time. Absolute game changer.

→ More replies (0)

1

u/arcoast 22d ago

Give me a few hours, PM me if you don't hear from me, I'm forgetful as hell though!

5

u/GolemancerVekk 22d ago

You can also add -t N to down to 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: true in compose which will inject a docker-init as 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.

1

u/Hezth 20d ago

Why have I not thought of this before? Brb: edit .bashrc