[Docker] Input initial data to docker-entrypoint-initdb.d without mounting the directory [MySQL]

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

Text

What I wanted to do

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.

manner

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

Background when this was needed

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.

Contents that I was allowed to refer to

Recommended Posts

[Docker] Input initial data to docker-entrypoint-initdb.d without mounting the directory [MySQL]
Rails6: Input the initial data of ActionText using seed
Changed the initial validator message to Japanese without garbled characters
Input to the Java console
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
I was able to deploy the Docker + laravel + MySQL app to Heroku!
Initial data input with [Rails] seed_fu!
Update MySQL from 5.7 to 8.0 with Docker
After all I wanted to preview the contents of mysql with Docker ...