Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running a сustom script when creating a database #67

Open
weblabio opened this issue Feb 25, 2023 · 0 comments
Open

Running a сustom script when creating a database #67

weblabio opened this issue Feb 25, 2023 · 0 comments

Comments

@weblabio
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant