All That you need to know about Docker Volumes.

Bind Volumes: How Map folders in your Docker host to your container

First lets create folder in our docker host

root@master:~# mkdir mysql
root@master:~# ls
index.html  mysql  snap

Map the new folder to the folder in the container

root@master:~# docker run -d -v /mnt/mysql:/var/lib/mysql  --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

Now, Lets enter to the container and create new database.

root@master:~# docker exec -it some-mysql bash
root@43bbea76beca:/#  mysql -u root -pmy-secret-pw
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
    
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database docker_db;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| docker_db          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit
root@43bbea76beca:/# exit
exit

Now lets delete the container.

root@master:~# docker rm -f some-mysql

Make sure that you have some data in our new folder in the docker host.

root@master:~# ls /mnt/mysql/
 auto.cnf        ca-key.pem        docker_db            ibdata1      '#innodb_temp'        private_key.pem   sys
 binlog.000001   ca.pem           '#ib_16384_0.dblwr'   ib_logfile0   mysql                public_key.pem    undo_001
 binlog.000002   client-cert.pem  '#ib_16384_1.dblwr'   ib_logfile1   mysql.ibd            server-cert.pem   undo_002
 binlog.index    client-key.pem    ib_buffer_pool       ibtmp1        performance_schema   server-key.pem

Lets restore our information in a new brand MySQL container, basically we using the same command as before.

root@master:~# docker run -d -v /mnt/mysql:/var/lib/mysql  --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

Lets enter to our new container and check that we can see our new docker_db database.

root@master:~# docker exec  -ti some-mysql bash
root@814f0801e5d4:/# mysql -u root -pmy-secret-pw
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| docker_db          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

Normal Volume – Manage by docker

How to create a Normal Volume:

  • Important to know Normal volume created in the Docker Root Directory
root@master:~# docker volume create mysql_volume
mysql_volume

How to find Normal volume:

Long way….

root@master:~# docker info | grep -i root
WARNING: No swap limit support
 Docker Root Dir: /var/lib/docker
root@master:~# cd /var/lib/docker/
root@master:/var/lib/docker# ls
builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
root@master:/var/lib/docker# ls volumes/
mysql_volume

Short way…

root@master:/var/lib/docker# docker volume ls
DRIVER              VOLUME NAME
local               mysql_volume

Lets create new MySQL container:

root@master:/var/lib/docker# docker run -d -v mysql_volume:/var/lib/mysql --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

Now we can see all the data in our new docker volume.

root@master:/var/lib/docker/volumes/mysql_volume/_data# ls
 auto.cnf        ca-key.pem       '#ib_16384_0.dblwr'   ib_logfile0     mysql                public_key.pem    undo_001
 binlog.000001   ca.pem           '#ib_16384_1.dblwr'   ib_logfile1     mysql.ibd            server-cert.pem   undo_002
 binlog.000002   client-cert.pem   ib_buffer_pool       ibtmp1          performance_schema   server-key.pem
 binlog.index    client-key.pem    ibdata1             '#innodb_temp'   private_key.pem      sys

Anonymous Volumes: Be Careful when you use them (Not Recommended)

Lets create MySQL container this time with the tag -v we don’t specify any mapping to the Docker Host.

root@master:~# docker run -d -v /var/lib/mysql  --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

To find where docker create the Anonymous volume we use this command

root@master:~# docker inspect some-mysql

Under mounts we can find what the name of the volume

In this output we can see the mapping and the location of the volume to the container folder.

  • Volume name “060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b”,
  • Volume location “/var/lib/docker/volumes/”
"Mounts": [
            {
                "Type": "volume",
                "Name": "060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b",
                "Source": "/var/lib/docker/volumes/060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

If we will go the Volume Location folder we can find all MySQL data in the container in our docker root directory.

root@master:~# cd /var/lib/docker/volumes/060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b/_data
root@master:/var/lib/docker/volumes/060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b/_data# ls
 auto.cnf        ca-key.pem       '#ib_16384_0.dblwr'   ib_logfile0     mysql                public_key.pem    undo_001
 binlog.000001   ca.pem           '#ib_16384_1.dblwr'   ib_logfile1     mysql.ibd            server-cert.pem   undo_002
 binlog.000002   client-cert.pem   ib_buffer_pool       ibtmp1          performance_schema   server-key.pem
 binlog.index    client-key.pem    ibdata1             '#innodb_temp'   private_key.pem      sys

Important to know.

  • The Anonymous Volumes can be removed when you delete the container if you using this flag -v.
root@master:# docker rm -fv some-mysql

root@master:~# cd /var/lib/docker/volumes/060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b/_data
-bash: cd: /var/lib/docker/volumes/060239d0072093a4a2e984467751e163f197a0e40b57299a62ef17e83798815b/_data: No such file or directory

Remove Dangling volumes (volumes that are not in use with any container)

root@master:~# docker volume rm $(docker volume ls -f=dangling=true -q)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.