Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created October 4, 2018 08:18
Show Gist options
  • Save tsh-code/473fc86582a63453139e535f2833bfab to your computer and use it in GitHub Desktop.
Save tsh-code/473fc86582a63453139e535f2833bfab to your computer and use it in GitHub Desktop.
#!/bin/bash
get_first_container_num() {
echo `docker inspect --format='{{.Name}}' $(docker ps -q) | grep "$1" | awk -F "_" '{print $NF}' | sort -r | head -1`
}
APP_FOLDER="dockerzerodowntime"
APP_NAME="api" # from docker-compose
APP_CONTAINER_NAME="$APP_FOLDER"_"$APP_NAME"
LB_NAME="lb" # from docker-compose
LB_CONTAINER_NAME="$APP_FOLDER"_"$LB_NAME"
APP_CONTAINER_NUM=`get_first_container_num $APP_CONTAINER_NAME`
LB_CONTAINER_NUM=`get_first_container_num $LB_CONTAINER_NAME`
docker-compose build $APP_NAME
docker-compose scale $APP_NAME=2
print "\nPreparing new container..."
sleep 5; # Allow container to fully start
printf "Draining traffic from old container"
docker exec -it "$LB_CONTAINER_NAME"_"$LB_CONTAINER_NUM" sh -c "echo set weight default_service/"$APP_CONTAINER_NAME"_"$APP_CONTAINER_NUM" 0 | socat stdio /var/run/haproxy.sock"
sleep 5; # Wait for connections to drain
printf "Stopping container: "
docker stop "$APP_CONTAINER_NAME"_"$APP_CONTAINER_NUM"
printf "Removing old container: "
docker rm "$APP_CONTAINER_NAME"_"$APP_CONTAINER_NUM"
printf "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment