In this guide, we about see how to attach a persistent volume in a docker container. If we don’t have a separate storage the data will not persist once the container is no longer up and running.
For demonstration purpose we are going to use with an MYSQL container its data will be saved under /var/lib/mysql inside the docker container, so our volume should be bind mounted under the same location from our docker host to the container with required proper SELinux contexts.
- How to install Docker on Red Hat Enterprise Linux and CentOS Linux 7
- Search for Docker images and launch a container
- Connect to Docker containers and expose the network
- How to manage Docker containers
- Managing Docker data persistently by attaching a volume
Step 1: Label with SELinux context for docker volume
Create a directory to save the data of the container and it should be labelled with “svirt_sandbox_file_t” Moreover it needs to be owned by user and group 27:27 recursively.
# mkdir /mysql_container # chown -R 27:27 /mysql_container # chcon -t svirt_sandbox_file_t /mysql_container
Step 2: Mounting a Volume
Let’s download MySQL docker image to launch a docker container
# docker pull mysql
To know about the available Environment Variables of MYSQL container image navigate to
We are using with only below three of environment variables for now.
MYSQL_ROOT_USER MYSQL_ROOT_PASSWORD MYSQL_DATABASE
Launch a container from MySQL image in detach mode in the name of “mysql-pro-dbsrv” by mounting volume from source to destination using “-v” option. By following we can assign to a number of variables which going to be used inside the container. To assign variables to use with “-e” the option.
# docker run --name mysql-pro-dbsrv -d -v /mysql_container:/var/lib/mysql/ -e MYSQL_ROOT_USER=root -e MYSQL_ROOT_PASSWORD=password123 mysql # docker ps
- –name Name of the container
- -d Launching container in detach mode
- -v Bind mount a volume
- -e Set environment variables
Step 3: Verifying environment variable and MYSQL DB
Once container up and running access its shell to verify DB files.
# docker exec -it mysql-pro-dbsrv /bin/bash # ls -lthr /var/lib/mysql/
List the Environment variables and do a grep
# env | grep MYSQL
Login to MYSQL Database with assigned environmental variables.
# mysql -u root -p # exit
Now, let us check MYSQL related files from docker host. The database files are saved under below location which mounted inside a Docker container.
# ls -lthr /mysql_container/
Step 4: Removing MYSQL container and data
To remove a Docker persistent storage we need to follow the above steps in reverse order.
But before starting with removing first we need to stop the container, and then remove the container, by following remove all files from bind source and finally remove the MySQL image.
# docker stop mysql-pro-dbsrv # docker rm mysql-pro-dbsrv # rm -rfv /var/local/mysql # docker rmi mysql
That’s it we have completed with mounting a volume to save data persistently in Docker containers.
In this short article, we have seen how to save Docker data persistently by binding a volume. By following let us cover with more docker topics till then subscribe with the newsletter to keep updated.