skip to Main Content

I have a maria-db database docker-compose.yml:

version: '3.8'

services:
  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_DATABASE: "wordpress3,wordpress4"
      MYSQL_USER: mohsin
      MYSQL_PASSWORD: 12345678
    volumes:
      - mariadb_data:/var/lib/mysql
    networks:
      - wordpress_network

networks:
  wordpress_network:
    external: true
    name: wordpress_network

volumes:
  mariadb_data:

I am running two wordpress docker-compose files (for testing purposes). I want to connect both of them to the mariadb docker-compose. Both files have similar config:

version: '3.8'

services:
  wordpress:
    image: wordpress:latest
    container_name: wordpress3
    restart: always
    environment:
      WORDPRESS_DB_HOST: mariadb
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: admin
      WORDPRESS_DB_NAME: wordpress3
    ports:
      - "8083:80"
    depends_on:
      - mariadb
    networks:
      - wordpress_network
  mariadb:
    extends:
      file: ../mariadb/docker-compose.yml
      service: mariadb

networks:
  wordpress_network:
    name: wordpress_network
    external: true

When I run the first wordpress docker-compose, it automatically runs mariadb and connects to it. At the same time, the other instance throws an error saying mariadb already in use. I want it to recognize that mariadb is already running, and connect to the shared instance automatically?

Two instances access the same db service type of scenario is required (condition: auto runs db service if down)

Tried using extend and .env but doesn’t work.

2

Answers


  1. You need to make sure that the containers you want to talk to each other are on the same network.

    for example:

    # first/docker-compose.yml
    version: '2'
    services:
      first:
        ...
        networks:
          - some-net
    networks:
      some-net:
        driver: bridge
    
    
    # second/docker-compose.yml
    version: '2'
    services:
      second:
        ...
        networks:
          - first_some-net
    networks:
      first_some-net:
        external: true
    

    Note: Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in, in this case a prefix first_ was added

    Login or Signup to reply.
  2. It is not logically correct to create two containers for one db, for maria db you have already created a container in your first file, as your two docker services are running in a single network, you can just connect to it. mariadb already in use this means that this container name has already taken in your network, so network has this container already, you can change container name, you don’t have to create two containers if you want them to be indentical, just use one! .

    So the conclusion is

    1. if you want to use two docker services in one host, and share single database in between, (And if it is, then your db access credentials should be the same, which is not in your case) use bridge network, and do not create tow identical containers for single service!.
    2. if you want to create two different databases with the same configuration just rename the container name in your second compose file, like mariadb_new.
    3. if you want to use the same db between two docker services which are located in two separate servers, then do not overwrite networks, just get access to them with public IP and served port.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search