April 27, 2025

Ultimate Seamless Blue‑Green Deployment with Docker Compose – A Powerful Zero‑Downtime Strategy 2025

Flat-style illustration of Blue-Green Deployment in DevOps showing two environments (blue and green) with traffic routing and a developer overseeing the deployment process.

Introduction

Blue-green deployment is a powerful deployment strategy that ensures zero-downtime software updates by switching traffic between two identical environments — one live (blue) and one idle (green). Docker Compose, a tool for defining and running multi-container Docker applications, can be a great way to manage blue-green deployments in a lightweight setup.

In this blog post, we’ll walk you through the process of implementing blue-green deployment using Docker Compose and further automating it with scripts. Whether you’re a DevOps engineer or a curious developer, this guide will help you build resilient deployments and reduce risk during production updates.


What is Blue-Green Deployment?

Blue-green deployment is a deployment technique where you maintain two separate environments:

  • Blue: The current live environment handling user traffic.
  • Green: The staging or idle environment where the new version of the application is deployed.

When the new version is verified to be working fine, the traffic is switched from Blue to Green, making Green the new live version. If something goes wrong, the switch can easily be reversed.

This technique minimizes downtime and eliminates risks associated with rolling back changes.


Why Docker Compose?

Docker Compose is ideal for small to medium-scale applications where Kubernetes may feel like overkill. With Docker Compose, you can define both your Blue and Green services along with a reverse proxy (like NGINX) in a single YAML file. It’s quick to get started and easy to automate.

Benefits:

  • Lightweight & fast
  • Easy to define multiple environments
  • Great for CI/CD pipelines
  • Can be integrated with scripts for full automation

Setting Up Blue-Green Deployment with Docker Compose

Project Structure

blue-green-deployment/
├── docker-compose.yml
├── nginx/
│   └── default.conf
├── app-blue/
│   └── index.html
├── app-green/
│   └── index.html
└── switch.sh

1. Define Services in docker-compose.yml

services:
  app-blue:
    image: nginx:alpine
    volumes:
      - ./app-blue:/usr/share/nginx/html
    networks:
      - appnet

  app-green:
    image: nginx:alpine
    volumes:
      - ./app-green:/usr/share/nginx/html
    networks:
      - appnet

  nginx:
    image: nginx:alpine
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    depends_on:
      - app-blue
      - app-green
    networks:
      - appnet

networks:
  appnet:

2. Create NGINX Config

Create nginx/default.conf:

server {
    listen 80;

    location / {
        proxy_pass http://app-blue; # or app-green
    }
}

3. Create Simple HTML Pages

In app-blue/index.html:

<h1>This is the BLUE version</h1>

In app-green/index.html:

<h1>This is the GREEN version</h1>

Automating Blue-Green Deployment Switch

We’ll create a simple switch.sh script that updates the NGINX config and restarts the NGINX service.

4. Bash Script: switch.sh

#!/bin/bash

NGINX_CONF="./nginx/default.conf"

if grep -q "app-blue" "$NGINX_CONF"; then
    NEW_TARGET="app-green"
    OLD_TARGET="app-blue"
else
    NEW_TARGET="app-blue"
    OLD_TARGET="app-green"
fi

# Health check
echo "Checking health of $NEW_TARGET..."

if curl -s --head http://localhost | grep "200 OK" > /dev/null; then
    echo "$NEW_TARGET is healthy. Switching traffic..."
else
    echo "$NEW_TARGET is not healthy. Aborting switch."
    exit 1
fi

# Update config
cat > "$NGINX_CONF" <<EOF
server {
    listen 80;

    location / {
        proxy_pass http://$NEW_TARGET;
    }
}
EOF

# Restart NGINX
docker-compose restart nginx

echo "Traffic successfully switched to $NEW_TARGET."

Make it executable:

chmod +x switch.sh

Run it:

./switch.sh

You can get this complete code from Github

Real-World Use Case Example

Let’s say you run an online store. You want to deploy a new feature but can’t afford downtime. Here’s how the blue-green deployment helps:

  1. Deploy the new version to Green.
  2. Test it on Green with real staging traffic or internal QA.
  3. Run the script to switch NGINX traffic from Blue to Green.
  4. If users report issues, run the script again to rollback instantly.

Best Practices for Blue-Green Deployment

  • Automate health checks to ensure target is healthy before switching.
  • Use CI/CD tools like GitHub Actions or GitLab CI to trigger deployments.
  • Monitor logs for real-time feedback.
  • Container versioning ensures you know what’s running.
  • Add downtime fallback pages if needed.

Expanding the Setup

  • DNS-Based Switching: Use external DNS services or Consul to point to Blue/Green.
  • Load Balancer Support: Use HAProxy or AWS ELB to route traffic.
  • Analytics Integration: Track user traffic and switch based on real-time metrics.

Conclusion

Blue-green deployment is a battle-tested way to achieve zero-downtime deployments. By combining it with Docker Compose and a little scripting, you get a lightweight yet powerful setup that can be extended with health checks, CI/CD pipelines, and version rollbacks.

This guide gives you a hands-on way to start building your own blue-green deployment system in minutes.

Want to automate this even more with GitHub Actions or integrate with cloud load balancers? Drop a comment below!

 

Amritpal

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 6+ years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

Amritpal

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 6+ years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by Amritpal →

Leave a Reply

Your email address will not be published. Required fields are marked *