简体   繁体   中英

Custom mysql alpine image not loading init.sql from docker-entrypoint-initdb.d mounted as a volume

I am trying to create a customer mysql image from base image as alpine. I have installed the necessary packages for mysql via apk through dockerfile. My requirement is i want to run a fresh instance of mysql_alpine and create and modify tables in the db. My data files are located in /var/lib/mysql/ mounted as a volume and my init.sql is mounted as a volume into /docker-entrypoint-initdb.d/. Following is my dockerfile

FROM alpine

RUN apk update && \
    apk --no-cache add mysql mysql-client && \
    addgroup mysql mysql && \
    mkdir docker-entrypoint-initdb.d && \
    mkdir run/mysqld && \
    chown -R mysql:mysql /run/mysqld && \
    chown -R mysql:mysql /docker-entrypoint-initdb.d && \
    chown -R mysql:mysql /var/lib/mysql && \
    chmod -R +x /docker-entrypoint-initdb.d/ && \
    mysql_install_db --user=mysql

 ENV "MYSQL_ROOT_PASSWORD=  " \
     "MYSQL_DATABASE=mysql" \
     "MYSQL_USER=test" \
     "MYSQL_PASSWORD=mypassword"

 VOLUME ~/mysql/init/:/docker-entrypoint-initdb.d/ && \
        ~/mysql/db/:/var/lib/mysql/

 CMD /usr/bin/mysqld --user=mysql

After i build the container when i run the conatiner with the command:

docker run -d -p 3307:3306 --name mysql_alpine mysql_alpine

only the db starts but there is no init.sql file inside docker-entrypoint-intidb.d/ folder and so, the file never got loaded when initializing the db

My init.sql file which is not getting mounted into docker-entrypoint-initdb.d folder inside the container:

CREATE DATABASE IF NOT EXISTS mysql;

USE mysql;

CREATE USER 'test'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL ON my_db.* TO 'testdata'@'localhost';
FLUSH PRIVILEGES;

First of all, you need to fix your volume section, as :

 VOLUME ~/mysql/init/:/docker-entrypoint-initdb.d/ && \
        ~/mysql/db/:/var/lib/mysql/

is not correct, check the documentation , instead you should use COPY instead:

 COPY mysql/init/ /docker-entrypoint-initdb.d/
 COPY mysql/db/ /var/lib/mysql/

And you need to make sure that mysql directory is on the same level as your Dockerfile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM