Skip to content

Instantly share code, notes, and snippets.

@gamma
Last active August 7, 2017 04:40
Show Gist options
  • Save gamma/55e1669aa7f4a529bc52d6aa104290e2 to your computer and use it in GitHub Desktop.
Save gamma/55e1669aa7f4a529bc52d6aa104290e2 to your computer and use it in GitHub Desktop.
Rolling Update with docker-compose and old Swarm Image. Expects docker-compose.yml side-by-side
#!/bin/bash
# Use the command line arguments as input. Each argument is a service.
SERVICE_LIST=`echo "$@" | tr ' ' '\n'`
if [ -z "${SERVICE_LIST[@]}" ]; then
SERVICE_LIST=`docker-compose config --services`
fi
# Check for all services in the compose file, scale them to "two"
echo "+++++++++++++++++++++++++"
echo "Scaling the containers up"
printf "%s\0" "${SERVICE_LIST[@]}" | xargs -ISERVICE echo "--scale SERVICE=2" | xargs -0 echo | tr '\n' ' ' | xargs docker-compose up -d --no-recreate
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
for i in {10..0} ; do
echo -ne "Waiting for all containers to have started. Actually just ${i}s.\r "
sleep 1
done
echo "Done waiting. "
# walk the services, find the lowest number and stop that container
for SERVICE in $SERVICE_LIST ; do
echo "-------------------`echo ${SERVICE} | sed 's/./-/g'`-"
echo "Cleaning Service: '${SERVICE}'"
NUMBER=`docker-compose ps ${SERVICE} | grep ${SERVICE} | awk '{print $1}' | awk -F_ '{print $3}' | sort | head -n1`
docker-compose ps $SERVICE | awk '{print $1}' | grep "${SERVICE}_${NUMBER:-_}" | xargs docker stop | xargs docker rm -f
done
# walk the services, scale them back to one
echo "+++++++++++++++++++++++++++"
echo "Scaling the containers down"
printf "%s\0" "${SERVICE_LIST[@]}" | xargs -ISERVICE echo "--scale SERVICE=1" | xargs -0 echo | tr '\n' ' ' | xargs docker-compose up -d --no-recreate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment