Docker container does now create table on run, MariaDB

I not sure what im doing wrong here but my docker container mariadb does not create a table when it start.

what am i doing wrong?

docker-compose.yml

version: '3'
services:
  db:
    build: .
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: checkitDB
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
      MYSQL_ROOT_PASSWORD: rootpassword
    volumes:
      - ./initDB:/docker-entrypoint-initdb.d

initDB.sql

CREATE TABLE checkitDB.tasks (
    id VARCHAR(255) NOT NULL,
    title VARCHAR(32) NOT NULL,
    description TEXT NOT NULL,
    PRIMARY KEY (id)
);

>Solution :

There can be a couple of reasons for this.

Do you have volume mapping for the database as well? The directory ‘docker-entrypoint-initdb.d’ has ‘init’ in it to indicate that the scripts are only run on database initialization. So if you already have a database, the scripts aren’t run.

Another thing is that you map ./initDB but your file is called initDB.sql. If you just want to map the file, you should do

- ./initDB.sql:/docker-entrypoint-initdb.d/initDB.sql

Leave a Reply