How to run mysql scripts in docker image?
Comment exécuter des scripts MySQL dans une image Docker ?
420-5A4-MT · Module 3 · Techniques DockerIn my Dockerfile
but when I build that image I have problem like:
Le conteneur ne roule pas encore
You cannot run those mysql commands, because the container is not running yet, so neither is MySQL.
What you can do is copy your .sql files to /docker-entrypoint-initdb.d. MySQL will run all .sql, .sql.gz and .sh located in this directory after initialization, which would result in the following Dockerfile:
More information can be found on the dockerhub page of mysql.
Pas d'import au moment du build
You can not import database at build time, as every RUN command run in separate shell plus MySQL process also not ready to accept the connection.
You can take benefits from docker entrypoint that does these out of the box.
So when the container started it will run the above script in alphabetical order.
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
Also you use MYSQL_USER to create user.