0
0
Dockerdevops~5 mins

Container orchestration in production in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing many containers manually is hard and error-prone. Container orchestration automates running, scaling, and managing containers in production so your apps stay reliable and efficient.
When you want to run multiple containers that work together as one app and keep them running smoothly.
When you need to update your app without downtime by replacing containers one by one.
When your app needs to handle more users by automatically adding more containers.
When you want to recover quickly if a container crashes by restarting it automatically.
When you want to manage container networking and storage easily across many servers.
Config File - docker-compose.yml
docker-compose.yml
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.

Commands
This command deploys the app stack defined in the docker-compose.yml file to Docker Swarm mode, creating and managing all services together.
Terminal
docker stack deploy -c docker-compose.yml myapp
Expected OutputExpected
Creating network myapp_default Creating service myapp_web Creating service myapp_app Creating service myapp_db
-c - Specifies the compose file to use for deployment
Lists all running services in the Docker Swarm to verify that the app services are up and running.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS xk3j4k5l6m7n myapp_web replicated 3/3 nginx:1.23 *:8080->80/tcp p9q8r7s6t5u4 myapp_app replicated 2/2 my-app:latest z1y2x3w4v5u6 myapp_db replicated 1/1 postgres:15
Shows detailed status of all tasks (containers) running for the web service to check if all replicas are healthy.
Terminal
docker service ps myapp_web
Expected OutputExpected
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS abc123def456 myapp_web.1 nginx:1.23 node1 Running Running 2 minutes ago def456ghi789 myapp_web.2 nginx:1.23 node2 Running Running 2 minutes ago ghi789jkl012 myapp_web.3 nginx:1.23 node3 Running Running 2 minutes ago
Updates the web service to use a newer Nginx image version 1.24, rolling out the update without downtime.
Terminal
docker service update --image nginx:1.24 myapp_web
Expected OutputExpected
myapp_web overall progress: 3 out of 3 tasks 1/3: running [==================================================>] 2/3: running [==================================================>] 3/3: running [==================================================>] verify: Service converged
--image - Specifies the new image version to update the service with
Key Concept

If 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.

Common Mistakes
Running multiple containers manually without orchestration tools.
It becomes hard to manage, scale, and recover containers, leading to downtime and errors.
Use orchestration tools like Docker Swarm with docker-compose files to manage containers as a group.
Not specifying restart policies in the compose file.
Containers won't restart automatically if they crash, causing service outages.
Add restart_policy under deploy to ensure containers restart on failure.
Updating container images by stopping and starting containers manually.
This causes downtime and can lead to inconsistent app states.
Use docker service update to roll out updates smoothly without downtime.
Summary
Use a docker-compose.yml file to define multiple services with replicas and restart policies.
Deploy the stack with docker stack deploy to run all services together in Docker Swarm mode.
Check service status with docker service ls and docker service ps to ensure containers run correctly.
Update services smoothly using docker service update with the new image version.