Custom MySQL Docker Instance

Reading Time: 2 minutes

Hi folks, I am back with another blog, have you ever been in a situation where you required to set up or mimic the current MySQL instance of any environment. Well recently I have been in such a situation and my use case was to test the overall services so I decided to use minikube for service deployment as a whole, now when I had to deploy a MySQL instance, it was a quite tedious task to run a normal MySQL container and then source the db script for database every time a new container comes up.

Sometimes it was quite annoying as dump takes 10 -15 mins to get populated, another issue was that if you think that you can dump the file in running container and the docker commit would do the trick, it won’t !

As docker commit would take a snapshot of the state not the data persisted. So today I am going to share a solution with you for this problem, what about creating a custom MySQL image with data preloaded, sounds good right now let’s look at it how we can get it done.

Things you will need

For this solution to work you just need, docker installed, Dockerfile that have the configuration for the above solution, you database dump in the same directory where Dockerfile is, let’s call it all_db.sql we will be using this name throughout the blog.

Some points to consider

The thing to consider is how you take database dump, just use the --databases flag for mysqldump command and only dump specific databases. So now let’s look at the docker file.

Dockerfile

If you look at the Dockerfile, it looks confusing but believe me its pretty straight forward, so let’s look at it.

So the concept used for this approach is using a prebuilt docker images, which means making an image first and then use it afterwards as a base image. Similarly we used the MySQL’s base image and aliased it as builder, then we copied our database dump to the appropriate data directory for MySQL i.e; /docker-entrypoint-initdb.d/ and used

RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db"]

so that it runs as docker build process which populates our base image with dump, and lastly we copy the /initialized-db from our base image to /var/lib/mysql to our final image and voila we are done once we build the image using

docker build -t mysql-custom-instance .

we get our custom MySQL docker image, you can try running it

docker run -d -p 3307:3306 mysql-custom-instance

or on the host network

docker run -d --network host mysql-custom-instance

In both case we require mysql-client to access the MySQL instance

you can pull this image from docker hub and see it in action, the password for user root is root

docker pull shuhamknoldus/mysql-custom-instance

So that was all for this blog, in next blog we shall look at how we can do the same thing for dynamoDB, please feel free to drop any questions or suggestions, and like if it solved your problem.

Happy coding 🙂

References

https://dev.mysql.com/doc/refman/5.7/en/mysqldump-sql-format.html
https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization.html

Github https://github.com/shubhamknoldus
Twitter https://twitter.com/shubhamv306

Knoldus-blog-footer-image

Written by 

Shubham Verma is a software consultant. He likes to explore new technologies and trends in the IT world. Shubham is familiar with programming languages such as Java, Scala, C, C++, HTML, Javascript and he is currently working on reactive technologies like Scala, Akka , spark and Kafka. His hobbies includes playing computer games and watching hollywood movies.