Docker Community Forums

Share and learn in the Docker community.

Docker-compose MariaDB - can't login as root after DB init

I can create my MariaDB instance just fine using the docker CLI:

$ docker run --name rf-mariadb --rm --detach --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=pass123 mariadb:10.2
$ docker container cp dbdata/dump.sql rf_mariadb:/docker-entrypoint-initdb.d
$ docker container exec -it rf-mariadb bash
# mysql -uroot -p"pass123" < dbdata/dump.sql

However, when I convert this to what I think is the equivalent docker-compose, I can’t login to MariaDB:

# docker-compose.yml
# ------------------
version: "3.2"

services:
  rf-mariadb:
    container_name: rf-mariadb
    image: mariadb:10.2 
  restart: always
  volumes:
    - type: bind
      source: ./dbdata
      target: /docker-entrypoint-initdb.d
      read_only: true
  environment: 
    MYSQL_ROOT_PASSWORD: pass123
  ports:
    - "3306:3306"

volumes:
  dbdata:

The database dump works, as I can see it when logged in as a different user. However, logging in as root using the password set using MYSQL_ROOT_PASSWORD fails, and I don’t understand why:

$ docker-compose up
$ docker exec -it rf-mariadb bash
# mysql -uroot -p"pass123"
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)