Configure Docker Container Replicas With Docker Compose

Deploying applications with Docker Compose means that some part of the application needs more resources than other parts.

With compose, we can deploy multiple containers that run the same image. We might deploy two frontend containers and one backend server (database) in frontend and backend applications design.

Deploy

The deploy statement allows us to specify replicas of the same image and have multiple containers running the same code.

In the following compose code, I deeply two frontend containers running nginx and one backend service that runs the database service.

services:
  app:
    image: nginx
    deploy:
      mode: replicated
      replicas: 2 
  backend:
    image: mysql:5.7
    volumes:
      - data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: set
      MYSQL_DATABASE: set
      MYSQL_USER: set
      MYSQL_PASSWORD: set
     
volumes: 
  data: {} 

The result is shown below. I have two containers running and listening on port 80 and one container running and listening on port 3306.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.