Step 1: Pull the MySQL Docker Image
- Start by pulling the appropriate Docker image for MySQL. You can download a specific version or opt for the latest release as seen in the following command:
docker pull mysql/mysql-server:latest
If you want a particular version of MySQL, replace latest with the version number.
- Verify the image is now stored locally by listing the downloaded Docker images:
The output should include mysql/mysql-server among the listed images.
example Docker image on your system for deploying a MySQL Docker container
Step 2: Deploy the MySQL Container
- Once you have the image, move on to deploying a new MySQL container with:
docker run --name=[container_name] -d mysql/mysql-server:latest
Replace [container_name] with the name of your choice. If you do not provide a name, Docker generates a random one.
The -d option instructs Docker to run the container as a service in the background.
In the command above, we used the latest version tag. This may differ according to the image you downloaded.
2. Then, check to see if the MySQL container is running:
You should see the newly created container listed in the output. It includes container details, one being the status of this virtual environment. The status changes from health: starting to healthy, once the setup is complete.
Command to deploy a MySQL Docker container and verify MySQL container is running
Step 3: Connect to the MySQL Docker Container
- Before you can connect the MySQL server container with the host, you need to make sure the MySQL client package is installed:
apt-get install mysql-client
2. Then, start a MySQL client inside the container by typing:
docker exec -it [container_name] mysql -uroot -p
3. Provide the root password, when prompted. With that, you have connected the MySQL client to the server.
- Finally, change the server root password to protect your information:
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘[newpassword]’;
Replace [newpassword] with a strong password of your choice.