Welcome to Docker for Beginners. Today I am going to show you how to run a MySQL Server Container with persistent volume. In my previous tutorial, I run a MySQL Server container but without persistent volume. In a production machine, I would suggest you to use a persistent volume to store the MySQL data. This tutorial requires Docker to be installed on the host. I am using Ubuntu 18.04 as the host. Let’s get started.
Run MySQL Server Container with Persistent Volume
Try the following command on Terminal
docker run --name mysql01 --restart always -p 33065:3306 -v /home/dhani/Docker_Volumes/mysql01:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=12345 -e MYSQL_ROOT_HOST=% -d mysql
- mysql01 #This is how I name my new container. Change mysql01 with something else.
- 33065:3306 #I want to use the port 33060 in the host. You can also change this number
- /home/dhani/Docker_Volumes/mysql01 #This is the folder where I want to save the mysql data. The -v option will mount the /var/lib/mysql in the container to /mnt/mysql_data in the host.
- MYSQL_ROOT_PASSWORD #specify your MySQL root password
- MYSQL_ROOT_HOST #If you put %, this means the root user can access the server from any host.
Now check using docker ps command
You should see the new container listed there.
My Test Result
I deleted mysql01 container but I did not touch anything inside my /home/dhani/Docker_Volumes/mysql01 directory. And then I run another instance (say it mysql02) with the same command above and connect the instance to my volume /home/dhani/Docker_Volumes/mysql01.
Magically, my new MySQL Server instance (mysql02) can read the databases from the previous instance that I deleted. Which is good.