Friday, 10 July 2026

docker compose restart:always restart:unless stopped notes, robuts design

 The only difference is what happens when you manually stop a container:


Scenario restart: always restart: unless-stopped

Container crashes ✅ Restarts ✅ Restarts

VM reboots ✅ Restarts ✅ Restarts

You run docker stop ✅ Restarts anyway ❌ Stays stopped

You run docker compose down ❌ Stays down (compose down overrides) ❌ Stays down

The key difference:


always — if you manually docker stop certgen, it will restart itself immediately. You can't keep it stopped unless you remove the container or use docker compose down.

unless-stopped — if you manually docker stop certgen, it stays stopped. It remembers "the user stopped me" and won't restart until you explicitly start it again.

************************************************************************


1. __`depends_on` controls order__ — yes, the dependency starts first. In your case, MySQL starts before <your_service>. ✅


2. __No `depends_on`__ — Docker Compose starts services in __the order they appear in the file__ (top to bottom). So if you remove `depends_on`, <your_service>(listed first) would start before MySQL (listed second).


__But there's an important caveat:__


`depends_on` only waits for the container to __start__, not for the service inside to be __ready__. So even with `depends_on`, MySQL container starts before certgen, but MySQL might not be ready to accept connections yet (it takes 3-10 seconds to initialize).


That's why your `db.Connect()` retry logic (30 retries × 2s) is essential — it bridges the gap between "container started" and "MySQL ready to accept connections."


__Summary:__

No comments:

Post a Comment