0
0
Dockerdevops~7 mins

Blue-green deployment with containers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Blue-green deployment helps you update your app without downtime by running two versions side by side and switching traffic between them smoothly.
When you want to update your web app without making users wait or see errors.
When you need to test a new version of your app in production safely before full release.
When you want to quickly roll back to the previous version if the new one has problems.
When you run multiple containers and want to avoid downtime during updates.
When you want to reduce risk by having two identical environments ready to serve traffic.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  blue:
    image: my-app:blue
    ports:
      - "8081:80"
  green:
    image: my-app:green
    ports:
      - "8082:80"
  proxy:
    image: nginx:1.23
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

This file defines three services:

  • blue: runs the current stable app version on port 8081.
  • green: runs the new app version on port 8082 for testing.
  • proxy: an Nginx server that listens on port 8080 and forwards traffic to either blue or green.

The nginx.conf file controls which app version receives user traffic, enabling smooth switching.

Commands
Start the blue, green, and proxy containers in the background so both app versions and the proxy run simultaneously.
Terminal
docker-compose up -d blue green proxy
Expected OutputExpected
Creating network "default" with the default driver Creating blue ... done Creating green ... done Creating proxy ... done
-d - Run containers in detached mode (in the background).
Check which app version the proxy is currently forwarding traffic to by sending a request to port 8080.
Terminal
curl http://localhost:8080
Expected OutputExpected
<html><body><h1>Welcome to Blue Version</h1></body></html>
Reload the Nginx proxy configuration to switch traffic from blue to green without downtime.
Terminal
docker-compose exec proxy nginx -s reload
Expected OutputExpected
No output (command runs silently)
Verify the proxy now forwards traffic to the green version after reload.
Terminal
curl http://localhost:8080
Expected OutputExpected
<html><body><h1>Welcome to Green Version</h1></body></html>
Stop the blue container after confirming green is working fine to free resources.
Terminal
docker-compose stop blue
Expected OutputExpected
Stopping blue ... done
Key Concept

If you remember nothing else from this pattern, remember: run two app versions side by side and switch user traffic instantly using a proxy.

Common Mistakes
Not running both blue and green containers at the same time.
You cannot switch traffic smoothly if the new version is not running alongside the old one.
Always start both versions before switching the proxy.
Forgetting to reload the proxy after changing its configuration.
The proxy keeps sending traffic to the old version until reloaded, causing no effect from the switch.
Run the proxy reload command after updating its config.
Stopping the old version before confirming the new version works correctly.
If the new version has issues, users will face downtime or errors without a quick rollback.
Test the new version fully before stopping the old one.
Summary
Use docker-compose to run blue and green app versions plus a proxy together.
Switch user traffic instantly by updating and reloading the proxy configuration.
Stop the old app version only after confirming the new one works well.