Canary deployment pattern in Docker - Commands & Configuration
version: '3.8' services: app: image: my-app:stable ports: - "80:80" app_canary: image: my-app:canary ports: - "8080:80" deploy: replicas: 1 labels: - "traefik.http.routers.app_canary.rule=PathPrefix(`/`) && Headers(`X-Canary`, `true`)"
This file defines two services: app runs the stable version on port 80, and app_canary runs the new canary version on port 8080.
The canary service is set to one replica to limit exposure.
Labels can be used by a reverse proxy like Traefik to route only users with a special header to the canary version.
docker-compose up -d
-d - Run containers in detached mode (in the background)docker ps
curl -H "X-Canary: true" http://localhost:8080
curl http://localhost:80
If you remember nothing else from this pattern, remember: deploy the new version to a small user group first, then gradually shift all traffic if it works well.
Practice
What is the main goal of a canary deployment in Docker?
Solution
Step 1: Understand canary deployment purpose
Canary deployment updates only a few containers initially to test changes safely.Step 2: Compare options
Only To update a small number of containers first to reduce risk describes updating a small number of containers to reduce risk, which matches the canary deployment goal.Final Answer:
To update a small number of containers first to reduce risk -> Option AQuick Check:
Canary deployment = update few containers first [OK]
- Thinking canary updates all containers simultaneously
- Confusing canary with blue-green deployment
- Assuming canary deletes old containers immediately
Which Docker command option controls how many containers update at the same time during a canary deployment?
docker service update --help
Solution
Step 1: Identify update control options
Docker's--update-parallelismcontrols how many containers update simultaneously.Step 2: Differentiate from other options
--update-delaycontrols wait time between updates,--replicassets container count, and--rollbackreverts updates.Final Answer:
--update-parallelism -> Option CQuick Check:
Parallelism = containers updated at once [OK]
- Confusing delay with parallelism
- Using replicas to control update speed
- Thinking rollback controls update count
Given the command:
docker service update --image myapp:v2 --update-parallelism 2 --update-delay 10s myservice
What happens during this update?
Solution
Step 1: Analyze update-parallelism
It sets 2 containers to update at the same time.Step 2: Analyze update-delay
It waits 10 seconds between each batch of updates.Final Answer:
Two containers update to v2, then wait 10 seconds before next two -> Option AQuick Check:
Parallelism=2 + Delay=10s means batch update with wait [OK]
- Assuming all containers update instantly
- Confusing delay as total update time
- Thinking syntax is incorrect
What is wrong with this command for canary deployment?
docker service update --image myapp:v2 --update-parallelism 0 --update-delay 5s myservice
Solution
Step 1: Check update-parallelism value
Parallelism controls how many containers update at once and must be at least 1.Step 2: Validate other options
Delay of 5s is valid; image syntax and option order are correct.Final Answer:
Parallelism cannot be zero; it must be at least 1 -> Option DQuick Check:
Parallelism=0 invalid, must be ≥1 [OK]
- Setting parallelism to zero to pause updates
- Thinking delay must be ≥10s
- Misordering command options
You want to deploy a new version of your app with canary deployment using Docker Swarm. You have 10 replicas. You want to update 3 containers at a time, wait 15 seconds between batches. Which command correctly implements this?
Solution
Step 1: Identify correct canary options
--update-parallelism 3and--update-delay 15scorrectly set batch size and delay; no other flags needed.Step 2: Verify update options
These match exactly in docker service update --image myapp:v2 --update-parallelism 3 --update-delay 15s myservice.Step 3: Check options in each command
--rollback-conditionand--rollback-on-failureare invalid;--rollbackforces rollback instead of update.Final Answer:
docker service update --image myapp:v2 --update-parallelism 3 --update-delay 15s myservice -> Option BQuick Check:
Parallelism + delay with valid flags only [OK]
- Adding invalid rollback flags
- Missing parallelism or delay options
- Assuming rollback must be specified
Ready to prove it?
Pass the test and this pattern moves unknown
