r/docker • • 6d 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

15 Upvotes

37 comments sorted by

24

u/gevorgter 6d 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.

5

u/NoDevelopment206 6d ago

swarm mode for a single site is a fair middle ground. feels like people skip it and jump straight to kubernetes

7

u/DuckDatum 6d ago

Don’t really need swarm either, do you? You can just host two instances behind a traffic router. Update one at a time.

3

u/cpuguy83 4d ago

And that's what swarm mode does.

1

u/DuckDatum 4d ago

Yeah, it’s the basic premise. I’m just pointing out that you don’t need a car to walk to your neighbors house.

2

u/gevorgter 4d ago

to tell you the truth, your way sounds more like driving a car to your neighbor's house.

You can't have 2 instances running in parallel without some special configuration. If your instance handles port 80 then second instance needs to be on different port. If you move different instance to another machine then they both need to talk to your DB server hence you need some kind of private network configured between your instance nodes, your router and your DB. All that is handled automatically by docker and swarm mode.

Of course you can run two instances on the same machine but each in it's own container in docker but then all you have left is turn the swarm mode on and no need to manually take one instance out of traffic router to update. Swarm handles all that.

1

u/DuckDatum 3d ago

What’s stopping you from having two separate machines host their own instance, both running off the same boot script and containing the same authentication details to the backend db? Presumably you’d have to require transactional execution by that point, but that’s not a deal breaker is it?

You need a traffic router in front, which sits behind your dns and picks a machine to distribute traffic to. Caddy works for that. So now we’re talking about three machines and some DNS records.

I don’t see why you’d need your two instances to be networked together. They just throw an error if a transaction fails, so coordination is not needed at the application layer.

And by driving a car to your neighbors house, I mean that to say: cars are a lot more fancy. My point was that you don’t need swarm, take a scooter if you want.

1

u/gevorgter 3d ago

What about database. Your app needs it, you will need a private network to access database

1

u/DuckDatum 3d ago

You needed that before too, didn’t you?

1

u/gevorgter 2d ago

No, my project is relatively small so db and web app live on the same server (vps). But business using them during working hours. So I can't really kick everyone one out for update. So I am using swarm to do "no downtime" update.

→ More replies

2

u/Strange_Ordinary6984 5d ago

Classic green blue pattern. Start one up, get a 200 health check back, spin the old one down.

2

u/mcassil 5d ago

Eu faço exatamente isso

6

u/JohnsTech_dev 6d 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.

2

u/NoDevelopment206 6d ago

good tip about the healthcheck. i was going to skip it, so thanks. does the old container get the full stop grace period before it is removed or does it cut off in flight requests?

1

u/JohnsTech_dev 6d ago

it does a normal docker stop on the old one, so you get whatever stop_grace_period is set to (10s by default) and then a SIGKILL. whether in flight requests survive depends on your app actually handling SIGTERM and finishing what its doing before exiting. a lot of node apps just die instantly on it.

the other gap is the proxy, it can still send a request to the old container for a moment after stop starts. if that matters set a longer grace period and make the app fail its healthcheck first when it gets SIGTERM so traefik drains it

9

u/nickjj_ 5d ago edited 5d 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.

1

u/encbladexp 6d 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:

git remote update
git rebase origin/main
docker compose pull
docker compose up -d

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/NoDevelopment206 6d ago

fair point on the data side, i hadnt thought about old and new versions sharing the same files. for a small single vm setup do you just accept the short gap and keep the pull and up flow?

2

u/encbladexp 6d ago

I would consider this a non-issue, even for prod, except somebody starts paying me for HA or FT. And most environments don't need HA or FT, DR is good enough and required anyways.

1

u/MasterChiefmas 6d ago edited 6d ago

and has a quick deploy ever turned into a real outage for you

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/

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?

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.

there's a short gap where requests fail.

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 6d ago

I don't.

1

u/always_assume_anal 6d 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 6d 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 5d 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 5d 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:

  • New version includes DB migrations? Are they backwards-compatible? I hope so, otherwise you'll get 500s at best, and data corruption at worst.
  • Have you properly configured connection draining and so on, so existing connections finish but new connections are rejected?
  • Is your LB/RP aware of your ZDT deployments, and able to avoid sending new requests to the old version while it's draining?
  • Will your LB/RP avoid splitting traffic from a given user across old and new? Otherwise, this could easily introduce weird bugs that are impossible to reproduce.

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 5d ago edited 5d 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 5d 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 5d 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/HeiiHallo 4d ago

Check out haloy which offers similar config options and provides zero-downtime

1

u/artsyfartsy2022 4d ago edited 4d ago

Running two containers behind Traefik or Nginx with health checks before switching traffic is usually the cleanest option. It avoids relying on late-night deploys and makes rollbacks much easier too.

1

u/Ruslanty 1d ago

If you run a queue worker in the same Compose project, I'd test that separately.Start a job that runs for 30 seconds, deploy while it's running, and check whether it finishes or gets picked up again. You can have zero 502s and still interrupt a job halfway through

1

u/got_hoss 5d ago

Kubernetes.

The answer is Kubernetes.

0

u/jjangg96 6d 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 6d 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.

-3

u/abotelho-cbn 6d ago

Docker Compose is a development tool. You get development tool behaviour.