- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating a MySQL Docker Container
One of the most important features of Docker Containerization is that it creates a bounded environment for running the Application with all the necessary dependencies and packages installed. Most applications require a backend database to store data points. Oracle provides Docker Images for running MySQL inside Containers and thus it becomes an excellent choice for testing your database applications. It provides lightweight MySQL Image instances with cleanup features once your testing is completed.
Docker allows you to download the Image containing the MySQL binaries and dependencies and creates a virtual filesystem. Note that if you start a Docker Container with the −−rm flag, after you stop the Container, the entire file system along with the Container Instance gets deleted and hence combining this flag with the run command would give you automatic cleanup feature for testing your applications.
In this article, we will see how to create MySQL Docker Containers for testing your application.
Creating the Docker Container for MySQL
The below command creates an instance for the MySQL Image with the latest version and along with it, the −−rm flag helps in eliminating the Container along with the file system once the Container is stopped.
sudo docker run −−rm −−name=mysql−test −e MYSQL_ALLOW_EMPTY_PASSWORD=yes −d mysql/mysql−server
In the above command, the −−rm flag instructs the Docker daemon to delete the Container file system after it’s stopped. The −−name flag provides the name to the Container. The “−e MYSQL_ALLOW_EMPTY_PASSWORD=yes” instructs the Container to create an empty root password.
Executing the MySQL Docker Container
The MySQL Client programs use the local socket connections i.e. they connect via the localhost. To create a local MySQL connection, you can use the following command.
sudo docker exec −it mysql−test mysql −uroot
The exec instruction tells the Docker daemon to execute the command specified inside the Docker Container. The −it flag instructs the Docker to open the Container in an interactive shell. The shell command that executes inside the Docker Container is “mysql −uroot”.
Client access using TCP/IP
After you execute the commands in the previous steps, Docker assigns a private IP address to the Docker Containers. It also creates a Network interface on your system. These can be used as endpoints to create a TCP/IP connection to the service.
You cannot access the MySQL service unless you create a TCP/IP connection between the two endpoints. To do so, you need to find out the IP addresses for the Virtual Network Interface in your system and the Docker Container instance. You can do so by using the commands below.
To find the IP address of the network interface in your system, execute −
ip addr show docker0
The IP address you get using the above command becomes one of the endpoints that your client application will connect to and you will use it to create users for your MySQL.
To find out the other endpoint, which is the IP address of your Docker Container Instance for MySQL, you can use this command −
sudo docker mysql−test
Using these two IP addresses, you can now move forward and create your TCP/IP connection and create a user to run your client application.
To make your local system as a root user without any password, you can use these commands.
Inside the terminal of your system, execute the mysql−test Docker Container to access the mysql shell.
sudo docker exec −it mysql−test mysql −uroot
Once you have access to the mysql shell, inside it execute this command to create your local system as the root user.
create user root@<virtual network interface IP> identified by '';
Note that the IP address used in the above command should be that of the virtual network interface in your local machine that we found in the earlier steps.
To grant all the access to the user created above, use this command.
grant all on *.* to root@<virtual network interface IP> with grant option;
To perform auto cleanup after you have performed all your testings, you can simply stop the Container and remove it using the stop and rm commands.
To conclude, in this article, we discussed how to create and launch a MySQL Docker Container and access it by creating a TCP/IP connection between the Virtual Network Interface on your Linux Machine and the Docker Container. We created the root user and granted all access to it.
- Mounting a volume inside docker container
- Running a Docker image as a container
- Installing Linux Packages Inside a Docker Container
- Working with Java inside Docker Container
- How to get a Docker Container IP address?
- How to backup and restore a Docker Container?
- How to run a command inside Docker Container?
- Building a full-fledged data science Docker Container
- Running Docker Container as a Non Root User
- Copying files from Docker container to Host
- Copy Files from Docker Container to Local Machine
- How do I get into a Docker container's shell?
- How to edit a file after I shell to a Docker container?
- How to copy files from host to Docker container?
- How do I run a command on an already existing Docker container?