In this post, We are deploying a container of MySQL that saves the data into a host folder. Here we will able to manage a persistent database with a shared volume.
Step 1: Search official MySQL Docker Container Image
Docker user search through subcommand and find available MySQL official images from remote or local registries. Below command filter only official image of MySQL database. we got two results one MySQL and another one MariaDB.
# docker search --filter is-official=true MySQL
Step 2:Download official MySQL Docker Container Image
After search 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 to 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 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 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 above command and after that access the mysql server through below command
# mysql -u root -p
Now you will be able to create users , databases , assigning privilege as on regular mysql server . Hope you enjoy container services.