Custom MySQL Docker Instance
Custom MySQL Docker Instance
Explore a custom MySQL Docker instance.
Join the DZone community and get the full member experience.Join For Free
Hi folks, have you ever been in a situation where you were 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 quite a tedious task to run a normal MySQL container and then source the DB script for the database every time a new container comes up.
Sometimes it was quite annoying because the dump takes 10-15 minutes to get populated. Another issue was that if you think that you can dump the file in a running container and the Docker commit will do the trick, it won't!
Docker commit will take a snapshot of the state, not the data persisted. So today, I'm going to share a solution for this problem. What about creating a custom MySQL image with data preloaded? Let's look at how we can get it done.
Things You'll Need
For this solution to work, you need Docker installed, Dockerfile that has the configuration for the above solution, and your 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 do the database dump. Just use the
--databases flag for
mysqldump command and only dump specific databases. So now let's look at the Dockerfile.
FROM mysql:5.7.24 as builder RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/usr/local/bin/docker-entrypoint.sh"] ENV MYSQL_ROOT_PASSWORD=root COPY all_db.sql /docker-entrypoint-initdb.d/ RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db"] FROM mysql:5.7.24 COPY --from=builder /initialized-db /var/lib/mysql
If you look at the Dockerfile, it looks confusing, but believe me, it's pretty straightforward.
So the concept used for this approach is using prebuilt docker images, which means making an image first and then using it afterward 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 a docker build process, which populates our base image with the 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, and 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 cases, we require a mysql-client to access the MySQL instance.
You can pull this image from Docker hub and see it in action. The password for user
docker pull shuhamknoldus/mysql-custom-instance
That's all for this article. In the next one, we'll 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.
This article was first published on the Knoldus blog.
Published at DZone with permission of shubham verma . See the original article here.
Opinions expressed by DZone contributors are their own.