Container orchestration in production in Docker - Commands & Configuration
version: '3.9' services: web: image: nginx:1.23 ports: - "8080:80" deploy: replicas: 3 restart_policy: condition: on-failure app: image: my-app:latest depends_on: - db deploy: replicas: 2 restart_policy: condition: on-failure db: image: postgres:15 environment: POSTGRES_USER: user POSTGRES_PASSWORD: password volumes: - db-data:/var/lib/postgresql/data volumes: db-data: {}
This docker-compose.yml file defines three services: web, app, and db.
web runs Nginx and exposes port 8080 on the host mapped to port 80 in the container. It runs 3 copies (replicas) for load balancing and restarts if it fails.
app runs your application image, depends on the database, runs 2 replicas, and restarts on failure.
db runs PostgreSQL with environment variables for user and password, and stores data persistently in a named volume db-data.
This setup helps run multiple containers together with automatic restarts and scaling.
docker stack deploy -c docker-compose.yml myapp-c - Specifies the compose file to use for deploymentdocker service ls
docker service ps myapp_web
docker service update --image nginx:1.24 myapp_web
--image - Specifies the new image version to update the service withIf you remember nothing else from this pattern, remember: container orchestration automates running, scaling, and recovering multiple containers together to keep your app reliable in production.
Practice
Solution
Step 1: Understand container orchestration role
Container orchestration automates managing many containers, including deployment and scaling.Step 2: Compare options
Only To manage and automate deployment, scaling, and operation of multiple containers describes managing and automating container groups, which is orchestration's goal.Final Answer:
To manage and automate deployment, scaling, and operation of multiple containers -> Option AQuick Check:
Orchestration = Manage & automate containers [OK]
- Confusing orchestration with coding inside containers
- Thinking orchestration is about creating images manually
- Believing orchestration only monitors single container stats
Solution
Step 1: Identify command for creating services
The commanddocker service createis used to start a new service in Docker Swarm.Step 2: Eliminate other commands
docker container runruns containers but not as a managed service;docker swarm initinitializes the swarm;docker network createcreates networks.Final Answer:
docker service create -> Option AQuick Check:
Service creation = docker service create [OK]
- Using 'docker container run' to create services
- Confusing swarm initialization with service creation
- Mixing network commands with service commands
docker service ls in a Docker Swarm cluster?Solution
Step 1: Understand the command purpose
docker service lsshows all services in the swarm, including how many replicas each has.Step 2: Compare with other options
It lists services and replicas, not individual containers (<code>docker ps</code>), images (<code>docker images</code>), or networks (<code>docker network ls</code>).Final Answer:
List of all services running in the swarm with their replicas -> Option BQuick Check:
docker service ls = list swarm services [OK]
- Thinking it lists individual containers
- Confusing services with images or networks
- Expecting detailed container logs from this command
docker service scale myservice=5 but only 3 containers start. What is the likely cause?Solution
Step 1: Analyze the scaling command
The command syntax is correct and service name is assumed valid since some containers started.Step 2: Identify resource constraints
If only 3 replicas run instead of 5, likely the swarm nodes lack enough CPU/memory to start all replicas.Final Answer:
Not enough resources on swarm nodes to run 5 replicas -> Option DQuick Check:
Scaling fails due to resource limits [OK]
- Assuming command syntax is wrong
- Thinking service name is invalid if some replicas run
- Ignoring resource limits on nodes
Solution
Step 1: Understand high availability in swarm
Running multiple replicas spreads tasks across nodes, so if one node fails, others keep running the service.Step 2: Evaluate options
Single replica or running containers manually does not provide failover. Disabling swarm removes orchestration benefits.Final Answer:
Set the service replicas to more than one so tasks run on multiple nodes -> Option CQuick Check:
Multiple replicas = high availability [OK]
- Running only one replica expecting failover
- Manually running containers without orchestration
- Disabling swarm mode to fix node failures
Ready to prove it?
Pass the test and this pattern moves unknown
