How to attach a Docker volume to the container


Docker Volume: In this guide, we about see how to attach a persistent volume in a docker container. If we don’t have separate storage the data will not persist once the container is no longer up and running. To eliminate the ephemeral state of filesystem we need to use anyone of volume.

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.

Articles related to Docker Series

  1. How to install Docker on Red Hat Enterprise Linux and CentOS Linux 7
  2. Search for Docker images and launch a container
  3. Connect to Docker containers and expose the network
  4. How to manage Docker containers
  5. Managing Docker data persistently by attaching a volume

Step 1: Label with SELinux context for docker volume

In the latest version of RHEL/CentOS flavour, you can check for SELinux content by following below SELinux guide.

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
Manage data in Docker container
Create bind volume for docker container

Step 2: Mounting a Volume

Let’s download the MySQL docker image to launch a docker container

# docker pull mysql

To know about the available Environment Variables of MYSQL container image navigate to

Click Here to know the MySQL containers environment variables from Docker Hub

We are using with only three of environment variables for now.


Launch a container from MySQL image in detaches mode in the name of “mysql-pro-dbsrv” by mounting volume from source to destination using the “-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
Launch a container by attaching a volume
Launch a container by attaching a volume
  1. –name Name of the container
  2. -d Launching container in detach mode
  3. -v Bind mount a volume
  4. -e Set environment variables

In case, if you have already started the container and not included any environment variable the container will be in error state. By running log option on the container we will get to know the available variables as well.

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 MySQL file inside the container
List the MySQL file inside the container

List the Environment variables and do a grep

# env | grep MYSQL
Finding the assigned env variable in a container
Finding the assigned env variable in a container

Login to MYSQL Database with assigned environmental variables.

# mysql -u root -p
# exit
Verify MySQL login in Container
Verify MySQL login in Container

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/
List the MySQL files in bind mount locally from the Docker host
List the MySQL files in bind mount locally from the Docker host

Step 4: Removing MYSQL container and data

To remove a Docker volume or 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
Removing docker container and bind volume
Removing Docker container and bind volume

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 attaching a docker volume. By following let us cover with more docker topics till then subscribe with the newsletter to keep updated.