Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a default database in mysql using dockerfile

Tags:

docker

mysql

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

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

So when I build an image from the docker file, the database should already be there.

like image 468
Mohammad Avatar asked Jul 10 '14 09:07

Mohammad


1 Answers

I was able to accomplish the goal of adding a database to the tutum-docker-mysql image by doing the following.

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

Inside of that .sh file I added a line, right below the two "mysql -uroot" lines that are already there. I simply added:

mysql -uroot -e "create database test;"

After that change to that .sh file, I simply built the 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 container id, that was returned from the "docker run" command above. Now that you have the password, you need the ip address. I get that by doing.

docker inspect 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896

And looking at the "Gateway" address. With the password and the ip address I was then able to do this mysql command from OUTSIDE of the container.

mysql -uadmin -pJcM5FCphMOp4 -h172.17.42.1 -P3307

Where the ip address and the password are the values I got from the previous two docker commands. After that command has run, I can then issue a "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 fought for a while trying it by modifying the Dockerfile. I think it might be possible, but after a while, I found the above solution a lot quicker and simpler.

like image 195
Cris Holdorph Avatar answered Sep 20 '22 23:09

Cris Holdorph