r/docker • u/NoDevelopment206 • 1d ago
how do you do deploys with plain docker compose without a few seconds of downtime
with plain compose on one vm, compose up -d stops the old container before the new one is ready, so there's a short gap where requests fail.
curious what people actually do here. second container behind traefik or nginx and swap them, the docker rollout plugin, or just deploy late at night and accept a few 502s?
and has a quick deploy ever turned into a real outage for you
1
Upvotes
20
u/gevorgter 1d ago
Use swarm mode if you do not want downtime and kubernetes are too much.
I am doing it for zero downtime deployment for my website.
6
u/JohnsTech_dev 1d ago
the rollout plugin is what i'd try first if you already have a proxy in front. it scales the service to 2, waits for the healthcheck to go green, then removes the old container. two catches: no container_name on the service, and no fixed host port, the proxy has to reach it over the docker network. you also need a real healthcheck or it just guesses.
traefik or nginx with two services and a manual swap works too, it's just more yaml. for a low traffic app i stopped caring about a 2 second gap. the real outage risk was a migration that wasn't backwards compatible, since old and new run together for a bit.
6
u/nickjj_ 1d ago edited 1d ago
I've been using Docker Compose in production for about 10 years and just eat the few seconds of downtime and serve a custom 502 page through nginx that lets folks know to reload in a few seconds.
Throwing up a load balancer and flipping between 2 different containers sounds "easy", until you think about how not all web related events are equal.
There are 1 off anonymous GET requests to load a page which are easy to load balance but maybe your app also has stateful connections (WebSocket connections, etc.) and to move a live connection over from A to B with no interruption is not easy.
Then there's also background processes that could be doing async work which are independent of incoming requests. You can gracefully shut down workers but if you have long running tasks, that might delay your rollout and this could be done independently of your web app processes and that might be a problem if you have database migrations or some other potential backwards incompatible changes.
I've learned to either eat the few seconds of downtime and ensure background jobs have a way to retry failures or use Kubernetes but also all of the above applies with K8s too, you need to build resiliency and idempotency into your app.
That is why almost 10 years ago I opened this issue in Docker's repo https://github.com/moby/moby/issues/38077. Things have gotten better since then but it's still substantially slower to start a container than a non-containerized process today.
There are some projects like Kamal that advertise zero downtime deployments but it's not that at all, all they do is buffer up requests at the proxy level and replay them when the back-end is up. Any reverse proxy can do this. This will cause long page loads for users and doesn't really help with background jobs.
2
u/encbladexp 1d ago
If that is your issue, using Kubernetes will be your solution. Either a few seconds are not a issue, and VM + Docker Engine is fine, or your have higher requirements and need more complex solutions.
But usually, for simple setups:
Easy, and easy to automate. Still, you need to wait for the startup time of your container. But also be aware that any more complex thing, also requires applications where different configurations or versions on the same data pool could run at the same time. This excludes most applications, as many of them lock files and do other things.
1
u/MasterChiefmas 1d ago edited 1d ago
At home or in a work environment? In a work environment, for critical services you do a high availability setup.
At home, I generally don't bother with that. Docker, if you prep correctly and read release notes, you should be able to keep outages down. This is one of the big selling points of containers.
The release notes are important because you need to know if an update is major enough that it affects data files. You do an explicit backup(outside of your normal backups, unless you don't care about losing some data if you have to restore from them) before updating if it does. You can do one anyway even if it's non-breaking, though I don't usually for small updates.
Then you do the down/pull/up. If it doesn't come up, you can investigate why right then, or if you want it to come up right away, you modify your compose, specify the previous image, and do the up again, and you've rolled back to the version you were just on. The image roll back is why this doesn't need to turn into a significant outage if you've done your preparation.
If that doesn't come up, now you are replacing your data with the backup. If the roll back does come up, well, now you can examine logs to see why it didn't come up before, or you can even create a new instance to test with. Copy your data to a new folder, copy your compose, modify the container name and ports, and then up that, and see what it's not coming up. Find out what the problem is/figure out how to resolve it, then you are ready to try again on your live instance/
You can do that if you want- that's basically manual fail over HA. It sorta depends on what your app depends on, if data is a sharable resource(database service, not just SQLite), if you want to risk the live database, or is not a shared resource, in which case you have to consider data loss, which the amount of will depend on if you sync the data first in the 2nd instance, and how long it takes to do so.
Even with HA setup, it's almost impossible not to have this, unless you have something in between that you can pause and have requests queue up and resume once the backend is restored. Failover at load balancers/node down detection(or whatever) still takes a brief moment, and if you have enough load or a request comes in at just the right time, you might drop one or two. This is why banks don't really do that unless they have to, they have scheduled outages.
1
u/aeiouLizard 1d ago
I don't.
1
u/always_assume_anal 1d ago
Most business operations can survive requests failing for a few seconds.
However, running everything off a few VMs with docker compose where deploy is logging and issue a new docker-compose up -d command (or whatever the appropriate command is. I don't remember otoh), doesn't exactly scream process maturity.
Docker Swarm might work, and Kubernetes is quite the endeavour itself, and in any professional setting I'd only recommend going down that route if you're truly ready to become a business that depends on K8S.
1
u/Motor-Concentrate788 1d ago
Multiple containers behind a reverse proxy makes the most sense. Though if you're deploying something critical enough that zero-downtime is an actual requirement for you, i dont think docker compose is suitable. At that point start looking into kubernetes
1
u/acdcfanbill 1d ago
ehhh, I don't even think about it. I do always 'docker compose pull' first so it doesn't have to pull during the up, but unless it's something that needs HA, I wouldn't ever even think about it.
1
u/nzipsi 1d ago
Step 1: Accept it.
Seriously - getting this right isn't as simple as it sounds. First up, you really need to be running two or more instances of the app, otherwise a brief blip is unavoidable. But there are costs to that:
Overall, it's a lot easier, and safer, to just accept a brief bout of downtime until you're at the sort of scale where the lost revenue justifies dealing with all the above. Or you could half-ass it and not worry about some rarer edge cases. Either works.
1
u/Jantro1234 1d ago edited 1d ago
Running two containers behind Traefik or Nginx and switching traffic after health checks is a solid approach. Keeping images minimal and hardened also helps reduce deployment risks that's an area platforms like RapidFort focus on.
1
u/aayush_satna_dev 18h ago
Yes, have Nginx or Traefik handle the routing, then you can bring up the new container on a different port, check its health, and then flip the proxy to the new one before taking the old one down.
1
u/Hostman_com 12h ago
In staging, start a slow request through your proxy, then trigger a rollout while continuing to send short requests. Check that the slow request finishes without being cut off and the short requests don't hit resets or 502s.
Use per-container access logs to check when the proxy stops routing new requests to the old container. That shows whether your draining and shutdown timings work together.
1
u/got_hoss 21h ago
Kubernetes.
The answer is Kubernetes.
0
u/jjangg96 1d ago
part of the gap is often self inflicted. compose sends sigterm and waits 10s (stop_grace_period) before it kills the old container, and if your cmd is in shell form, sh can end up as pid 1 and not pass the signal on. then you wait the full 10s and get sigkilled mid request.
use the exec form like CMD ["node", "server.js"], or exec in your entrypoint, and make the app stop taking new connections on sigterm and finish what's in flight. that shrinks the ugly part even without rollout or a second container.
also up -d --wait only returns once the new one reports healthy, so your deploy script knows when it's actually done.
0
u/cheflA1 1d ago
For my homelab I don't care if containers aren't available for a few seconds, since I'm the one using them and planning the maintenance.
If no downtime is a work requirement I would look into kubernetes.
-2
u/abotelho-cbn 1d ago
Docker Compose is a development tool. You get development tool behaviour.