CodingLittle
CodingLittle

Reputation: 1857

Why is docker compose creating mysql container with wrong name?

I have managed to create a MySQL and PHP container and my scripts execute and all my tables are there.

However I have a database that I call "myDb" and a user that is called "someuser" and when the database is created for some reason the name of the database is "somedatabase"

my docker-compose.yaml file:

services:
  mysql:
    image: mysql:latest
    ports:
      - 3307:3306
    environment:
      MYSQL_DATABASE: myDb
      MYSQL_ROOT_PASSWORD: SomeRootPassword1!
      MYSQL_USER: someuser
      MYSQL_PASSWORD: Password1!
    volumes:
      - ./dbScript/winit_Script2.sql:/docker-entrypoint-initdb.d/winit_Script2.sql
      - db_data:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: dev_pma
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3307
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8183:80

volumes:
  db_data:

phpAdmin:

enter image description here

Mysqlworkbench:

enter image description here

What have I done wrong here?

A little edit after the comments:

It would seem that when having a volumes section you create volumes in docker and when you create a volume on a specific port once then it gets reused every time you do docker-compose up. This was the case for me.

More details in accepted answer.

Upvotes: 1

Views: 928

Answers (1)

Neo Anderson
Neo Anderson

Reputation: 6382

The mysql image does not initialize the database if the volume is not clean.

When you are stopping and starting the database from the same compose file, the volume is always the same, hence you want the data to be persisted even after an app restart.

To force the re-initialization of the data, you can delete that docker volume(only if you no longer need that database! this cannot be undone):

First, stop and delete the containers.

Then list and delete the volume that persists the database:

docker volume ls
DRIVER              VOLUME NAME
local               <your-deployment-name>_db_data


docker volume rm <your-deployment-name>_db_data

Then run again the docker-compose up command and you'll be able to find the myDb in phpMyAdmin instead of somedb


Edit:
Unless you change yourself the entrypoint and rebuild the image to force it initialize your DB according to the ENV you're passing, even if the volume is not clean, the only option that comes to my mind is to create the new DB manually. Here is the conditional that skips the re-initialization of the DB and here is the script that is invoked if the volume is clean.

Upvotes: 3

Related Questions