📖 Scenario: You are managing a web application that needs to be updated without downtime. You will use blue-green deployment with Docker containers to switch between two versions smoothly.
🎯 Goal: Build a simple blue-green deployment setup using Docker containers. You will create two containers named blue and green, configure a variable to track the active container, switch the active container, and finally display which container is currently active.
📋 What You'll Learn
Create two Docker containers named blue and green running a simple web server
Create a variable called active_container to track which container is live
Write a command to switch the active_container from blue to green or vice versa
Print the name of the currently active container
💡 Why This Matters
🌍 Real World
Blue-green deployment helps update applications without downtime by switching traffic between two container versions.
💼 Career
Understanding container management and deployment strategies is essential for DevOps roles to ensure smooth application updates.
Progress0 / 4 steps
1
Create two Docker containers named blue and green
Write two docker run commands to start containers named blue and green using the nginx image in detached mode.
Docker
Hint
Use docker run -d --name container_name nginx to start each container.
2
Create a variable active_container to track the live container
Create a shell variable called active_container and set it to blue to indicate the blue container is currently active.
Docker
Hint
Use active_container=blue to set the variable.
3
Switch the active_container from blue to green
Write a shell command to change the value of active_container from blue to green if it is currently blue. Use an if statement to check the current value.
Docker
Hint
Use if [ "$active_container" = "blue" ]; then active_container=green; fi to switch.
4
Print the name of the currently active container
Write a echo command to display the text Active container is: followed by the value of active_container.
Docker
Hint
Use echo "Active container is: $active_container" to print the active container.
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
Step 1: Understand blue-green deployment concept
Blue-green deployment uses two identical environments (blue and green) to switch traffic smoothly.
Step 2: Identify the main goal
The main goal is to reduce downtime by switching user traffic from one environment to another without interruption.
Final Answer:
To reduce downtime by switching traffic between two identical environments -> Option A
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
Step 1: Identify command to create and start container
The docker run command creates and starts a container from an image.
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.
Final Answer:
docker run -d --name green_container myapp:latest -> Option B
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?
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
Step 1: Analyze the commands
docker stop blue_container stops the blue container. docker start green_container starts the green container.
Step 2: Understand traffic switch effect
Stopping blue and starting green means traffic moves from blue to green environment.
Final Answer:
The blue environment stops, and the green environment starts serving traffic -> Option C
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
Step 1: Check container states
Stopping blue_container and starting green_container should switch environments if containers run properly.
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.
Final Answer:
The load balancer or proxy is still routing traffic to the blue environment -> Option A
Quick Check:
Traffic routing depends on load balancer config [OK]
Hint: Check load balancer routes after container switch [OK]