Let mysql sql automatically executed when the start docker

When you create a mysql container with a docker, sometimes we expect the container to start the database and tables have been built automatic initialization data has automatic entry, which means that we can start after the container is directly connected to the container database, which of the data.

In fact, the official mirror mysql is to support the ability to automatically execute when the container starts specified sql script or shell script, we take a look at mysql official mirror Dockerfile , as shown below:

Write pictures described here

Has been set up ENTRYPOINT, which calls /entrypoint.sh this script, we mysql: 8 to pull this image locally, and then start docker run up and see what's inside entrypoint.sh this script, there is a piece of content through all .sh .sql and fixed directory suffix from a file, and then executed, as shown below:

Write pictures described here

Clear principles, and now we have to practice it again:

When setting up disconf environment on a docker, need to build mysql database, and to be performed sequentially four sql database files are on the table, do the initial data, we have two approaches:

  1. Copy the file to the /docker-entrypoint-initdb.d four sql directory, so that when the container when run will automatically perform four sql, but from the point of view screenshot of the script, the execution order of multiple files is not specified, if the script is created late in the database to create a table of script execution, it will lead to the failure to build the table, so this way can not copy sql meet our needs (however, if the four files into one order sql able to meet the requirements);
  2. Do a sh file, in which according to our own need to perform sql, reads as follows:
#!/bin/bash
mysql -uroot -p$MYSQL_ROOT_PASSWORD <<EOF
source $WORK_PATH/$FILE_0;
source $WORK_PATH/$FILE_1;
source $WORK_PATH/$FILE_2; 
source $WORK_PATH/$FILE_3; 

See shell is very simple, and executes the specified login mysql sql file, MYSQL_ROOT_PASSWORD, WORK_PATH, FILE_0 these are environment variables.

Let's look at the corresponding Dockerfile how to write, as follows:

# Docker image of disconf mysql
# VERSION 0.0.1
# Author: bolingcavalry

#基础镜像使用daocloud.io/library/mysql:8
FROM daocloud.io/library/mysql:8

#作者
MAINTAINER BolingCavalry <[email protected]>

#定义工作目录
ENV WORK_PATH /usr/local/work

#定义会被容器自动执行的目录
ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d

#定义sql文件名
ENV FILE_0 0-init_table.sql
ENV FILE_1 1-init_data.sql
ENV FILE_2 20151225.sql
ENV FILE_3 20160701.sql

#定义shell文件名
ENV INSTALL_DATA_SHELL install_data.sh

#创建文件夹
RUN mkdir -p $WORK_PATH

#把数据库初始化数据的文件复制到工作目录下
COPY ./$FILE_0 $WORK_PATH/
COPY ./$FILE_1 $WORK_PATH/
COPY ./$FILE_2 $WORK_PATH/
COPY ./$FILE_3 $WORK_PATH/

#把要执行的shell文件放到/docker-entrypoint-initdb.d/目录下,容器会自动执行这个shell
COPY ./$INSTALL_DATA_SHELL $AUTO_RUN_DIR/

#给执行文件增加可执行权限
RUN chmod a+x $AUTO_RUN_DIR/$INSTALL_DATA_SHELL

0-init_table.sql, 1-init_data.sql, 20151225.sql, 20160701.sql four files that we want to execute sql, the file is copied to the mirror when the mirror structure docker;

Please complete clone of my github: [email protected]: zq2599 / docker_disconf.git, into the inside of the clone is complete mysql folder, execute the command line docker build -t disconf_mysql in this folder: 0.0.1 build Mirror ;

Then perform docker run -name mysqldisconf -e MYSQL_ROOT_PASSWORD = 123456 -idt disconf_mysql: 0.0.1 start a container, then perform docker logs -f mysqldisconf view container logs, red box below, you can see that we write sh file specified location It has been performed:

Write pictures described here

We went to look at the data in the database is not really there, execute docker exec -it mysqldisconf / bin / bash into the container;

After performing mysql -uroot -p123456 log mysql, FIG follows:

Write pictures described here

You can see, show databases, show tables, select * from app and other operations can prove sql automatically executed after the container has been created to achieve our goal.

I welcome the attention of the public number

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/bolingcavalry/p/11495509.html