Search code examples
dockermagentodocker-compose

Multiple Docker Containers with Same Images


I am trying to create multiple containers with my existing Magento project.

Task # 1) I have successfully moved the existing project to Docker by following markshust/docker-magento. Everything working perfectly fine as expected. But this is only for moving single Magento instance to Docker

Task # 2) I am trying to similarly create multiple instances of the same existing Magento project on Docker

I tried to create 2 different YMLs with different --project-name (to differentiate between containers). Followed same steps as for Task # 1 and updated external ports in the second YML. But unfortunately I am not able to run any of the containers now. None working!

When I am trying to access first Magento container e.g. https://example.com:444/ it add errors in system.log and strangely, the same error is being logged inside my second docker container's system.log

I suspect that volumes are creating problems in my case (as volumes path are same in both YMLs) but I am not able to figure out the exact problem here. Adding both YMLs below

docker-compose.yml (Placed at docker root directory)

version: "3"

services:
  app:
    image: markoshust/magento-nginx:1.18-5
    ports:
      - "81:8000"
      - "444:8443"
    depends_on:
      - "db"
    volumes: &appvolumes
      - ~/.composer:/var/www/.composer:cached
      - ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
      - ~/.ssh/known_hosts:/var/www/.ssh/known_hosts:cached
      - appdata:/var/www/html
      - sockdata:/sock
      - ssldata:/etc/nginx/certs
    networks:
      - customNetwork

  phpfpm:
    image: markoshust/magento-php:7.4-fpm-11
    volumes: *appvolumes
    env_file: env/phpfpm.env
    networks:
      - customNetwork

  db:
    image: mariadb:10.4
    restart: on-failure
    command: --max_allowed_packet=256M
    ports:
      - "3307:3306"
    env_file: env/db.env
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - customNetwork

  redis:
    image: redis:5.0-alpine
    ports:
      - "6379:6379"
    networks:
      - customNetwork

  elasticsearch:
    image: markoshust/magento-elasticsearch:7.9.3-1
    ports:
      - "9201:9200"
      - "9301:9300"
    environment:
      - "discovery.type=single-node"
      ## Set custom heap size to avoid memory errors
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
      ## Avoid test failures due to small disks
      ## More info at https://github.com/markshust/docker-magento/issues/488
      - "cluster.routing.allocation.disk.threshold_enabled=false"
      - "index.blocks.read_only_allow_delete"
    networks:
      - customNetwork

volumes:
  appdata:
  dbdata:
  sockdata:
  ssldata:

networks:
  customNetwork:

docker-compose-second.yml (Placed at docker root directory)

version: "3"

services:
  app:
    image: markoshust/magento-nginx:1.18-5
    ports:
      - "82:8000"
      - "445:8443"
    depends_on:
      - "db"
    volumes: &appvolumes
      - ~/.composer:/var/www/.composer:cached
      - ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
      - ~/.ssh/known_hosts:/var/www/.ssh/known_hosts:cached
      - appdata:/var/www/html
      - sockdata:/sock
      - ssldata:/etc/nginx/certs
    networks:
      - customNetworkM2

  phpfpm:
    image: markoshust/magento-php:7.4-fpm-11
    volumes: *appvolumes
    env_file: env/phpfpm.env
    networks:
      - customNetworkM2

  db:
    image: mariadb:10.4
    restart: on-failure
    command: --max_allowed_packet=256M
    ports:
      - "3308:3306"
    env_file: env/db.env
    networks:
      - customNetworkM2

  redis:
    image: redis:5.0-alpine
    ports:
      - "6381:6379"
    networks:
      - customNetworkM2

  elasticsearch:
    image: markoshust/magento-elasticsearch:7.9.3-1
    ports:
      - "9202:9200"
      - "9302:9300"
    environment:
      - "discovery.type=single-node"
      ## Set custom heap size to avoid memory errors
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
      ## Avoid test failures due to small disks
      ## More info at https://github.com/markshust/docker-magento/issues/488
      - "cluster.routing.allocation.disk.threshold_enabled=false"
      - "index.blocks.read_only_allow_delete"
    networks:
      - customNetworkM2

volumes:
  appdata:
  dbdata:
  rabbitmqdata:
  sockdata:
  ssldata:

networks:
  customNetworkM2:

db.env (Placed at [docker root]/env directory)

MYSQL_HOST=db
MYSQL_ROOT_PASSWORD=magento
MYSQL_DATABASE=magento
MYSQL_USER=root
MYSQL_PASSWORD=magento

MYSQL_INTEGRATION_ROOT_PASSWORD=magento
MYSQL_INTEGRATION_DATABASE=magento_integration_tests
MYSQL_INTEGRATION_USER=root
MYSQL_INTEGRATION_PASSWORD=magento
MYSQL_INTEGRATION_HOST=db

I am new to Docker. Need some help! P.S. I am using docker & docker-compose on Ubuntu 18.04 and Magento 2.4.3-P1


Solution

  • Problem has been Solved. Follow below instructions:

    Create file docker-compose-instance1.yml

    version: '3'
    
    services:
      magento:
        image: folio3ecommerce/magento-php-apache:2.4.5
        restart: on-failure
        ports:
          - "${APP_PORT}:80"
        env_file: env/phpfpm.env
        links:
          - db
          - redis
          - elasticsearch
        depends_on:
          - db
        networks:
          - customNetwork
        
      db:
        image: mariadb:10.4
        restart: on-failure
        command: --max_allowed_packet=256M
        ports:
          - "${DB_PORT}:3306"
        env_file: env/db.env
        volumes:
          - dbdata:/var/lib/mysql
        networks:
          - customNetwork
    
      redis:
        image: redis:5.0-alpine
        restart: on-failure
        ports:
          - "${REDIS_PORT}:6379"
        networks:
          - customNetwork
    
      elasticsearch:
        image: elasticsearch:7.16.2
        restart: on-failure
        ports:
          - "${ES_PORT}:9200"
          - "${ES_SSL_PORT}:9300"
        volumes:
          - elasticsearchdata:/var/lib/elasticsearch
        environment:
          - "discovery.type=single-node"
          ## Set custom heap size to avoid memory errors
          - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
          ## Avoid test failures due to small disks
          ## More info at https://github.com/markshust/docker-magento/issues/488
          - "cluster.routing.allocation.disk.threshold_enabled=false"
          - "index.blocks.read_only_allow_delete"
        networks:
          - customNetwork
    
    volumes:
      appdata:
      dbdata:
      sockdata:
      ssldata:
      redisdata:
      elasticsearchdata:
    
    networks:
      customNetwork:
    

    Execute following command to create containers for instance1 via above YML file

    docker-compose -f docker-compose-instance1.yml -p "instance1" up -d --build
    

    Similarly, you can create multiple instances with docker-compose-instance2.yml, docker-compose-instance3.yml, etc

    You can use following image from Docker Hub https://hub.docker.com/r/folio3ecommerce/magento-php-apache/tags

    This image provides Magento Instances with Sample Data (v2.4.3, v2.4.4, v2.4.5)

    I hope this helps!