TL;DR
--In Docker
, mounting a directory will overwrite the information in the container, but if you mount a file with bind
, it will not be overwritten.
--In the MySQL
official image, the .sh
file placed in docker-entrypoint-initdb.d
is executed when the container starts.
--Therefore, by mounting the directory containing DDL
in an arbitrary path and mounting the input script in docker-entrypoint-initdb.d
with bind
, docker-entrypoint-initdb.d
Initial data can be input without mounting DDL
on
I wanted to execute the initialization DDL
when the container was started, without overwriting the docker-entrypoint-initdb.d
in the container, and with the same usability as mounting a directory.
As mentioned at the beginning, the docker-entrypoint-initdb.d
in the container will not be overwritten if the file is mounted.
Also, in the MySQL
official image, the .sh
file placed in docker-entrypoint-initdb.d
will be executed when the container starts.
Therefore, you can mount the directory containing DDL
as an arbitrary directory and input it with the shell script mounted on docker-entrypoint-initdb.d
.
The following is an example of a submission script.
Input script
#!/bin/sh
#---
#Of MySql container/docker-entrypoint-initdb.By mounting it as a file on d
#Of that container/tmp/init.Put the delta DDL mounted on d into MySQL
#
# /tmp/init.An error will occur if the file does not exist in d
#---
ls -1 /tmp/init.d/*.sql | while read file
do
mysql -uroot -proot < $file
done
Since the series of executions is in alphabetical order by file name, you can control the order by adjusting the file name at the time of mounting.
For example, you can make it run last by prefixing the file name to mount with zzz-
.
docker-compose.yml
#Abbreviation (* Image, not moved and tried)
volumes:
- type: bind #Mount the input shell, zzz to make the execution order last-The prefix is specified
source: ./init.sh
target: /docker-entrypoint-initdb.d/zzz-init.sh
- ./init.d:/tmp/init.d/ #Align DDL with input script/tmp/init.Mount on d
This is because I had to use an image in the format that contains the initial data in docker-entrypoint-initdb.d
, and I couldn't mount the directory here.
If you do it by placing the files in docker-entrypoint-initdb.d
obediently, you need to mount the DDL
files individually, so consider how to mount them collectively in a directory and come up with this method. I did.
.sh
and .sql
in the chapter of ʻInitializing a fresh instance`.Recommended Posts