Creating a default database in mysql using dockerfile

Im using this tutum-docker-mysql dockerfile to get up and work with the docker image with mysql installed in it, the docker file creates a user with root name and no password, I need to add something like this to the docker file:

RUN mysql -uroot -p"" && mysql create database test; 

Therefore, when I create an image from a docker file, the database should already be there.

+6
source share
2 answers

I was able to complete the task of adding a database to the tutum-docker-mysql image by following these steps.

 git clone https://github.com/tutumcloud/tutum-docker-mysql.git cd tutum-docker-mysql vi create_mysql_admin_user.sh 

Inside this .sh file, I added a line located directly beneath the two "mysql -uroot" lines that already exist. I just added:

 mysql -uroot -e "create database test;" 

After this change in this .sh file, I just built a docker image.

 docker build -t tutum/mysql . 

After the docker image was built, I could run it with something like:

 docker run -d -p 3307:3306 tutum/mysql 

Once the container is running, you need to know the password to use and the IP address of the container. To get the password, you simply do

 docker logs 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896 

But, of course, use the identifier of the container that was returned from the docker run command above. Now that you have a password, you need an IP address. I understand what I'm doing.

 docker inspect 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896 

And look at the address "Gateway". With the password and ip address, I was able to execute this mysql command from the OUTSIDE container.

 mysql -uadmin -pJcM5FCphMOp4 -h172.17.42.1 -P3307 

Where the IP address and password are the values ​​I got from the two previous docker commands. After running this command, I can then run the "show databases" command with the following results.

 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> 

I struggled for a while trying it by modifying the Docker file. I think this is possible, but after a while I found the solution above is much faster and easier.

+11
source

You can pass the en_CREATE_DB env parameter when the container starts. Example from tutum README.md

docker run -d -p 3306:3306 -e ON_CREATE_DB="newdatabase" tutum/mysql

+3
source

Source: https://habr.com/ru/post/972018/


All Articles