Blue green deployment script with docker compose

Note Statistics

Note Statistics

  • Viewed 1598 times
  • Bookmarked 1 times
Thu, 09/17/2020 - 16:34

Use this script to deploy docker containers defined in a docker-compose file with zero downtime.

Prerequisites

  • Your containers are defined without a port
  • You are using a smart proxy running in a container like Traefik or nginx-proxy to listen to new and removed containers and automatically registers/unregister them

Script

Create a file named deploy.sh with the content below

#!/bin/sh
if [ "$(docker ps -f name=blue -q)" ]
then
    ENV="green"
    OLD="blue"
else
    ENV="blue"
    OLD="green"
fi

echo "Starting "$ENV" container"
docker-compose --project-name=$ENV  up -d --build --force-recreate

echo "Waiting..."
sleep 5s

echo "Stopping "$OLD" container"
docker-compose --project-name=$OLD down
docker system prune -f
docker volume prune -f

Running the script

  1. Make this file executable with chmod +x deploy.sh
  2. execute with ./deploy.sh
Authored by