This article will explain the step-by-step procedure for using MySQL with Docker.
What are the Steps to Use MySQL With Docker?
To use MySQL with Docker, try out the below-mentioned steps:
- Pull MySQL image from Docker Hub
- View the downloaded image
- Build and run MySQL container
- View running container
- Access MySQL container
- Connect to MySQL database
- Execute MySQL commands
Step 1: Pull MySQL Image From Docker Hub
To pull MySQL from Docker Hub to the local system, write out the below-listed command in Windows PowerShell:
It can be observed that the MySQL image’s latest version has been downloaded.
Step 2: View the Downloaded Image
Next, list all the available images to ensure that the MySQL image has been pulled successfully:
The above output shows the latest version of MySQL image.
Step 3: Start MySQL Container
Then, create and execute the MySQL container via the “docker run -d –name <cont-name> -e MYSQL_ROOT_PASSWORD=<password> <image-name>” command:
Here:
- “–name” option sets the container name i.e., “mySql-cont”.
- “-d” flag is utilized to execute the container in background.
- “-e MYSQL_ROOT_PASSWORD” defines the root password to “mysql123”.
- “mysql:latest” is the Docker image to utilize:
The above-executed command has built and started a “mySql-cont” container running the latest version of MySQL in the background.
Step 4: View Running MySQL Container
To ensure that the MySQL container is running successfully, execute the following command:
The above output indicates that the MySQL container is running successfully i.e., “mySql-cont”.
Step 5: Access MySQL Container
Now, utilize the “docker exec -it” command along with the container name to open the Bash shell inside the running MySQL container:
The above-stated command has opened a Bash shell and now users can execute the command within the running MySQL container.
Step 6: Connect to MySQL Database
After that, connect to the MySQL database as the root user using the provided command and enter the password interactively:
It can be observed that the MySQL shell has been started.
Step 7: Execute MySQL Commands
Finally, run the MySQL commands in the MySQL container. For instance, execute the “SHOW DATABASES;” command to view all the existing databases:
The above output showed the available databases in the MySQL container.
To select a specific database, execute the “USE <database-name>;” command:
Moreover, to view the tables in the selected database, utilize the below-provided command:
In the above output, all the tables in the MySQL container can be seen. We have successfully used MySQL with Docker.
Conclusion
To use MySQL with Docker, first, pull the MySQL image from Docker Hub using the “docker pull mysql” command. Then, create and execute the MySQL container through the “docker run -d –name <cont-name> -e MYSQL_ROOT_PASSWORD=<password> <image-name>” command and view it. After that, access the MySQL container and connect to the MySQL database. Finally, run MySQL commands in it. This article has explained the procedure for using MySQL with Docker.