Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File in docker-entrypoint-initdb.d never get executed when using docker compose

I'm using Docker Toolbox on Windows 10

I can access the php part succesfully via http://192.168.99.100:8000, I have been working around with the mariadb part but still having several problems

I have an sql file as /mariadb/initdb/abc.sql so I should be copied into /docker-entrypoint-initdb.d, after the container is created I use docker-compose exec mariadb to access the container, there is the file as /docker-entrypoint-initdb.d/abc.sql but the file never get executed, I also have tested to import the sql file to the container manually, it was succesful so the sql file is valid

I don't quite understand about the data folder mapping, and what to do to get the folder sync with the container, I always get the warning when recreate the container using docker-compose up -d

WARNING: Service "mariadb" is using volume "/var/lib/mysql" from the previous container. Host mapping "/.../mariadb/data" has no effect. Remove the existing containers (with docker-compose rm mariadb) to use the Recreating db ... done

Questions

  1. How to get the sql file in /docker-entrypoint-initdb.d to be executed ?
  2. What is the right way to map the data folder with the mariadb container ?

Please guide Thanks

This is my docker-compose.yml

version: "3.2"
services:
    php:
        image: php:7.1-apache
        container_name: web
        restart: always
        volumes:
            - /.../php:/var/www/html
        ports:
            - "8000:80"
    mariadb:
        image: mariadb:latest
        container_name: db
        restart: always
        environment:
            - MYSQL_ROOT_PASSWORD=12345
        volumes:
            - /.../mariadb/initdb:/docker-entrypoint-initdb.d
            - /.../mariadb/data:/var/lib/mysql
        ports:
            - "3306:3306"
like image 993
Artisan Avatar asked Oct 03 '17 02:10

Artisan


2 Answers

For me the issue was the fact that Docker didn't clean up my mounted volumes from previous runs.

Doing a:

docker volume ls

Will list any volumes, and if previous exist, then run 'rm' command on the volume to remove it.

As stated on docker mysql docks, scripts in the '/docker-entrypoint-initdb.d' folder is only evalutated the first time the container runs, and if a previous volume remains, it won't run the scripts.

As for the mapping, you simply need to mount your script folder to the '/docker-entrypoint-initdb.d' folder in the image:

volumes:
   - ./db/:/docker-entrypoint-initdb.d

I have a single script file in a folder named db, relative to my docker-compose file.

like image 57
radrocket81 Avatar answered Oct 19 '22 05:10

radrocket81


In your Docker file for creating mariaDB, at the end add the abc.sql file to your docker entry point like so:

COPY abc.sql /docker-entrypoint-initdb.d/

Remove the - /.../mariadb/initdb:/docker-entrypoint-initdb.d mapping as any file copied into the entry point will be executed.

Note: Windows containers do not execute anything in docker-entrypoint-initdb.d/

like image 44
Kevin Kiwango Avatar answered Oct 19 '22 05:10

Kevin Kiwango