Last Updated on 7 hours by Sachin G
In this post, we are deploying a Docker container of MySQL that saves the data into a host folder. Here we will be able to manage a persistent database with a shared volume.
Step 1: Search official MySQL Docker Container Image
Docker user searches through subcommands and finds available MySQL official images from remote or local registries. The below command filters only the official image of the MySQL database. We got two results, one MySQL and another one MariaDB.
# docker search --filter is-official=true MySQL
Step 2: Download the official MySQL Docker Container Image
After searching for the image, you can use docker pull to download it. When using the pull subcommand, Docker fetches the image and saves it locally.
# docker pull mysql
After pulling the image docker stores images locally and you can list them through below command .
# docker images
Step 3: Preparing Persistent Storage for MySQL Container
4. Creating a directory on-base host Operating system. Setting up default ownership and group ownership uid and gid of MySQL user, which is default 27, because in running container processes, much be writable files to the directory.
# mkdir /var/mysqldb # chown -R 27:27 /var/dbfiles
Step 4 : Temporary Run MySQL container for Extra Variable
Before running or deploying this downloaded container image. I am going to run this container image for testing purposes, after starting the container with detaching mode option MySQL container stopped or existed because the Mysql container always needs some extra parameter. so after this we can see logs of that container image, the logs will show the MySQL container needs some extra parameter. I showed in the below screenshot. Below commands, I had used to run and check logs.
# docker run -d --name continer_name MySQL # docker ps -a # docker logs continer_name # docker rm continer_name
Step 5: Deploying a Container with Persistent Storage
This command mounts the host /var/mysqldb directory in the container /var/lib/mysql/data directory. The /var/lib/mysql/data is the directory where the MySQL database stores the data.
Syntax : # docker run -d --name [container_name] -v host_folder:container_mysql_database_storefolder -e MYSQL_ROOT_PASSWORD=password image_name:tag
-e MYSQL_ROOT_PASSWORD : variable is having the MySQL server root password, so it should be a complex password.
Verify that the /var/mysqldb directory contains mysql data of container.
Step 6: Access MySQL Container & MySQL server
Now access the shell of running mysql container through below command
# docker exec -it [container_name] /bin/bash
Go into the container through the above command, and after that access the MySQL server through the below command
# mysql -u root -p
Now you will be able to create users, databases, and assign privileges as on a regular MySQL server. Hope you enjoy container services.
I’m Sachin Gupta — a freelance IT support specialist and founder of Tech Transit. I’m certified in Linux, Ansible, OpenShift (Red Hat), cPanel, and ITIL, with over 15 years of hands-on experience. I create beginner-friendly Linux tutorials, help with Ansible automation, and offer IT support on platforms like Upwork, Freelancer, and PeoplePerHour. Follow Tech Transit for practical tips, hosting guides, and real-world Linux expertise!