3

I am using docker-compose for deploying several containers on the same host. My images are built in Google Cloud Build and stored on gcr.io; I am not using docker-compose to build my images.

When I run docker-compose pull followed by docker-compose up -d, new containers are created for my images. However, if I run docker ps in another tab, I observe that my containers go offline for a few seconds while the new ones are being created.

Is there a way to tell docker-compose to recreate and then restart the containers without any downtime?

2 Answers 2

3

docker-compose up by itself does not support any way of deploying with zero downtime.

https://docs.docker.com/compose/reference/up/

You'll need to implement your own blue/green deployment or have a look at kubernetes's rolling update: https://kubernetes.io/docs/tutorials/kubernetes-basics/update/update-intro/

6

Here’s a really simple solution we found for zero downtime, blue–green style deploys with just docker-compose and nginx: https://engineering.tines.com/blog/simple-zero-downtime-deploys.

Turns out, a well-placed bash script is all you need here! This method saved us adding other dependencies to our stack. Here's the central piece:

reload_nginx() { docker exec nginx /usr/sbin/nginx -s reload } zero_downtime_deploy() { service_name=tines-app old_container_id=$(docker ps -f name=$service_name -q | tail -n1) # bring a new container online, running new code # (nginx continues routing to the old container only) docker-compose up -d --no-deps --scale $service_name=2 --no-recreate $service_name # wait for new container to be available new_container_id=$(docker ps -f name=$service_name -q | head -n1) new_container_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $new_container_id) curl --silent --include --retry-connrefused --retry 30 --retry-delay 1 --fail http://$new_container_ip:3000/ || exit 1 # start routing requests to the new container (as well as the old) reload_nginx # take the old container offline docker stop $old_container_id docker rm $old_container_id docker-compose up -d --no-deps --scale $service_name=1 --no-recreate $service_name # stop routing requests to the old container reload_nginx } 

Hope this can help others.

2
  • Hey where do you place this script? Commented Mar 13, 2023 at 18:04
  • While it sounds great, it's absolutely not clear for me, how should example docker-compose.yml and nginx configs look like. Would appreaciate more examples here. Commented Sep 14 at 18:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.