Bird
Raised Fist0
Dockerdevops~7 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main goal of a blue-green deployment when using containers?
easy
A. To reduce downtime by switching traffic between two identical environments
B. To increase the number of containers running simultaneously
C. To create backups of container images automatically
D. To monitor container resource usage in real-time

Solution

  1. Step 1: Understand blue-green deployment concept

    Blue-green deployment uses two identical environments (blue and green) to switch traffic smoothly.
  2. Step 2: Identify the main goal

    The main goal is to reduce downtime by switching user traffic from one environment to another without interruption.
  3. Final Answer:

    To reduce downtime by switching traffic between two identical environments -> Option A
  4. Quick Check:

    Blue-green deployment = reduce downtime [OK]
Hint: Blue-green means two environments for zero downtime [OK]
Common Mistakes:
  • Confusing blue-green with scaling containers
  • Thinking it creates backups automatically
  • Assuming it monitors resource usage
2. Which Docker command is used to create a new container from an image for the green environment in a blue-green deployment?
easy
A. docker stop green_container
B. docker run -d --name green_container myapp:latest
C. docker push green_container myapp:latest
D. docker build -t green_container myapp/

Solution

  1. Step 1: Identify command to create and start container

    The docker run command creates and starts a container from an image.
  2. Step 2: Match command to green environment container creation

    docker run -d --name green_container myapp:latest runs the container detached with the name green_container.
  3. Final Answer:

    docker run -d --name green_container myapp:latest -> Option B
  4. Quick Check:

    Run command creates container [OK]
Hint: Use 'docker run' to start new containers [OK]
Common Mistakes:
  • Using 'docker build' which builds images, not containers
  • Using 'docker push' which uploads images to registry
  • Using 'docker stop' which stops containers
3. Given the commands below, what will be the result after switching traffic from blue to green?
docker stop blue_container
docker start green_container
medium
A. The green environment stops, and the blue environment starts
B. Both blue and green environments run simultaneously
C. The blue environment stops, and the green environment starts serving traffic
D. No change occurs because containers are not restarted

Solution

  1. Step 1: Analyze the commands

    docker stop blue_container stops the blue container. docker start green_container starts the green container.
  2. Step 2: Understand traffic switch effect

    Stopping blue and starting green means traffic moves from blue to green environment.
  3. Final Answer:

    The blue environment stops, and the green environment starts serving traffic -> Option C
  4. Quick Check:

    Stop blue + start green = traffic switch [OK]
Hint: Stopping blue and starting green switches traffic [OK]
Common Mistakes:
  • Assuming both run simultaneously after stop command
  • Confusing start and stop commands
  • Thinking no change happens without redeploy
4. You tried to switch traffic by running docker stop blue_container and docker start green_container, but users still reach the blue environment. What is the most likely cause?
medium
A. The load balancer or proxy is still routing traffic to the blue environment
B. The green container failed to start due to image errors
C. Docker daemon is not running on the host machine
D. The blue container was never running initially

Solution

  1. Step 1: Check container states

    Stopping blue_container and starting green_container should switch environments if containers run properly.
  2. Step 2: Identify traffic routing cause

    If users still reach blue, likely the load balancer or proxy still routes traffic to blue environment IP or port.
  3. Final Answer:

    The load balancer or proxy is still routing traffic to the blue environment -> Option A
  4. Quick Check:

    Traffic routing depends on load balancer config [OK]
Hint: Check load balancer routes after container switch [OK]
Common Mistakes:
  • Assuming container start/stop alone switches traffic
  • Ignoring load balancer or proxy configuration
  • Thinking Docker daemon status causes routing issues
5. You want to automate blue-green deployment using Docker Compose. Which approach correctly switches traffic between blue and green services?
hard
A. Run both blue and green services with the same name and let Docker handle traffic automatically
B. Stop Docker Compose and restart it with a new image tag without changing service names
C. Build images with different tags but deploy only the blue service always
D. Use two Compose files with different service names and update the reverse proxy to point to the active service

Solution

  1. Step 1: Understand Docker Compose blue-green setup

    Using two Compose files or service names (blue and green) allows running both environments side by side.
  2. Step 2: Switch traffic by updating reverse proxy

    Updating the reverse proxy or load balancer to point to the active service switches traffic without downtime.
  3. Final Answer:

    Use two Compose files with different service names and update the reverse proxy to point to the active service -> Option D
  4. Quick Check:

    Two services + proxy update = blue-green switch [OK]
Hint: Switch proxy target between blue and green services [OK]
Common Mistakes:
  • Using same service names causing conflicts
  • Not updating proxy or load balancer
  • Deploying only one service without switching
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard