yobasystems / alpine-mariadb

MariaDB running on Alpine Linux [Docker]
https://hub.docker.com/r/yobasystems/alpine-mariadb/
242 stars 72 forks source link

Running a сustom script when creating a database #67

Open weblabio opened 1 year ago

weblabio commented 1 year ago

Hi! I need to execute my script during the first database initialization, for example, I want to create additional users and give them rights. I faced the difficulty of customizing my script.

A. use /docker-entrypoint-initdb.d/* I looked at run.sh and it became clear that I can only place .sql and .sql.gz files in this directory. But I need to pass environment variables to this file. Is there any way I can do this? Not resorting to parsing via build, but only mounting the directory using volumes in my docker-compose.yml? Also, I can't run sh scripts in this directory.

B. use /scripts/pre-exec.d/*sh Here I can already place my script through volumes, in which environment variables are available, but the problem is that this script is called every time, but need to do it on first start. In run.sh uses the condition if [ -d /var/lib/mysql/mysql ] which is no longer relevant at the time of script execution.

At the moment, I found the following solutions to my problem:

  1. Enable execution of .sh scripts in the /docker-entrypoint-initdb.d directory
  2. Add parsing .sql files to replace ${VAR} to environment variables
  3. Add to my sh script the creation of a directory/file (flag) to check if the container is running for the first time.

The third option suits me best, but looks like a crutch (it is necessary to ensure that in each script there is such a check).

Use the first two options, I can only if I completely replace the run.sh script when building the image, which is a problem, since this script can be updated.

I can do a PR and modify run.sh, but first I want to hear your opinion. The first option seems to me more suitable (and is easier to implement), since plain sql is not enough.