Skip to content

Instantly share code, notes, and snippets.

@mateusnava
Last active March 24, 2021 14:09
Show Gist options
  • Select an option

  • Save mateusnava/43527c844d544598be96e0b212e63d92 to your computer and use it in GitHub Desktop.

Select an option

Save mateusnava/43527c844d544598be96e0b212e63d92 to your computer and use it in GitHub Desktop.
Script for docker-compose deploy with zero downtime
#!/bin/bash
DOCKER_COMPOSE_NAME='web'
DOCKER_COMPOSE_FOLDER='/home/mateus/Projetos/api-agro1'
cd $DOCKER_COMPOSE_FOLDER
echo '==> Pulling...'
docker-compose pull
echo '==> Scaling...'
container_name=`docker-compose ps | grep -m 1 $DOCKER_COMPOSE_NAME | cut -d " " -f1`
docker-compose up -d --no-deps --scale $DOCKER_COMPOSE_NAME=2 --no-recreate
new_container_name=`docker-compose ps | grep $DOCKER_COMPOSE_NAME | tail -1 | cut -d " " -f1`
echo "New container is $new_container_name"
echo '==> Waiting for health...'
while true; do
if [[ `docker inspect --format='{{json .State.Health.Status}}' $new_container_name` = *"\"healthy\""* ]]; then
break
fi
echo "$(date) - still trying"
sleep 5
done
echo '==> New container is running!!!'
echo '==> Removing old container...'
sleep 1
docker rm --force $container_name
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment