'Second mariadb+phpmyadmin Docker container access denied

I have working mariadb+phpmyadmin container on my local dev computer. I would like to create another mariadb+phpmyadmin container, but I'm getting error: [Warning] Access denied for user 'root'@'127.0.0.1' (using password: YES). I can't figure out where is a problem. I tried to add parameter: MYSQL_HOST: '%' and modify line: test: mysqladmin ping -h $$MYSQL_HOST -u $$MYSQL_USER --password=$$MYSQL_PASSWORD but still no success.

Working docker-compose.yml

version: '3.9'

networks:
  rosetta_net:
    driver: bridge

services:
  maria_db_service:
    image: mariadb:10.5.9
    container_name: 'rosetta-api-db'
    restart: always
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: 'root123'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'root'
      MYSQL_DATABASE: 'rosetta'
    volumes:
      - ./docker/db/mariadb/data:/var/lib/mysql
      - ./docker/db/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf
    networks:
      - rosetta_net
    healthcheck:
      test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
      interval: 5s
      retries: 5

  phpmyadmin_service:
    image: phpmyadmin/phpmyadmin:5.1
    container_name: 'rosetta-api-db-admin'
    ports:
      - '8081:80'
    environment:
      PMA_HOST: db_server
      MAX_EXECUTION_TIME: 3600
      UPLOAD_LIMIT: 128M
    depends_on:
      maria_db_service:
        condition: service_healthy
    volumes:
      - ./docker/db/phpmyadmin/sites-enabled:/etc/apache2/sites-enabled
      - db_rosetta_data:/var/www/html
    networks:
      - rosetta_net

volumes:
  db_rosetta_data:

Not working docker-compose.yml (it is very similar to previous one):

version: '3.9'

networks:
  parkovisko_net:
    driver: bridge

services:
  maria_db_service:
    image: mariadb:10.5.9
    container_name: 'parkovisko-api-db'
    restart: always
    ports:
      - '3307:3306'
    environment:
      MYSQL_ROOT_PASSWORD: 'root123'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'root'
      MYSQL_DATABASE: 'parkovisko'
    volumes:
      - ./docker/db/mariadb/data:/var/lib/mysql
      - ./docker/db/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf
    networks:
      - parkovisko_net
    healthcheck:
      test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
      interval: 5s
      retries: 5

  phpmyadmin_service:
    image: phpmyadmin/phpmyadmin:5.1
    container_name: 'parkovisko-api-db-admin'
    ports:
      - '8083:80'
    environment:
      PMA_HOST: db_server
      MAX_EXECUTION_TIME: 3600
      UPLOAD_LIMIT: 128M
    depends_on:
      maria_db_service:
        condition: service_healthy
    volumes:
      - ./docker/db/phpmyadmin/sites-enabled:/etc/apache2/sites-enabled
      - db_parkovisko_data:/var/www/html
    networks:
      - parkovisko_net

volumes:
  db_parkovisko_data:


Solution 1:[1]

The problem is that you are using the same volumes for both of the docker-compose files, so MYSQL cant initialize mounting the same volumes for 2 containers

There are multiple ways to fix this:

Change it to named volumes:

volumes:
 - mariadb-data:/var/lib/mysql

Copy the data to another folder with cp and then mount it again (assuming you want the same data on both containers).

The same applies to .cnf and phpmyadmin containers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paltaa