Skip to content

Instantly share code, notes, and snippets.

@dre1080
Created October 29, 2018 14:09
Show Gist options
  • Save dre1080/3f2275fb22d6ceb0ff988f77826e2913 to your computer and use it in GitHub Desktop.
Save dre1080/3f2275fb22d6ceb0ff988f77826e2913 to your computer and use it in GitHub Desktop.
Zero Downtime Docker Compose Deploys
#!/usr/bin/env bash
CONTAINER_NAME="$1"
PROJECT_NAME="app"
# lets find the first container
FIRST_NUM=`docker ps | awk '{print $NF}' | grep app_$CONTAINER_NAME | awk -F "_" '{print $NF}' | sort | head -1`
NUM_OF_CONTAINERS=1
MAX_NUM_OF_CONTAINERS=2
# if empty, first run
if [ -z "$FIRST_NUM" ]; then
docker-compose -f docker-compose.prod.yml -p $PROJECT_NAME up --build -d
else
# pull new images
docker-compose -f docker-compose.prod.yml -p $PROJECT_NAME pull
# scale & build new container
docker-compose -f docker-compose.prod.yml -p $PROJECT_NAME up \
-d \
--build \
--no-deps \
--scale $CONTAINER_NAME=$MAX_NUM_OF_CONTAINERS \
$CONTAINER_NAME
# waiting for new containers
sleep 90
# removing old containers
for ((i=$FIRST_NUM;i<$NUM_OF_CONTAINERS+$FIRST_NUM;i++))
do
docker stop "${PROJECT_NAME}_${CONTAINER_NAME}_$i"
docker rm "${PROJECT_NAME}_${CONTAINER_NAME}_$i"
done
docker-compose -f docker-compose.prod.yml -p $PROJECT_NAME up \
-d \
--no-deps \
--scale $CONTAINER_NAME=$NUM_OF_CONTAINERS \
$CONTAINER_NAME
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment