Docker study notes - Docker Volume

Volume can separate the container and the data generated by the container , so that when you delete the container, the related data will not be affected.

Note: Containers are not meant for persistence.

Creation of Volume


Volumes can be created in two ways:
- Specify VOLUME /some/dir in the Dockerfile
- Execute the Docker run -v /some/dir command to specify

Docker will create a directory on the host, under /var/lib/docker by default, and then mount it to the specified path (/some/dir), when the container using the Volume is deleted, the Volume itself does not will be affected and can be kept forever. Our operations on the /var/lib/docker directory on the host are synchronized to the container that mounts this directory.

We can also use the -v parameter to mount the specified host directory. E.g:

$ docker run -v /opt/data:/data ubuntu

This command will mount the host's /opt/data directory to the /data directory in the container, and any files in the /opt/data directory will appear in the container. This can be used to implement file sharing between the host and the container.

When the specified directory in the container does not exist, it will be automatically created. When it already exists, the files in the directory will not be synchronized to the Volume on the host, and then the data in the Volume will be copied to the container.

Deletion of Volume


We can delete the Volume while deleting the container.

$ docker rm -v my_container

However, if other containers still mount the Volume, it will not be deleted.
Therefore, since we did not add the -v parameter when deleting some containers, some zombie directories and files appeared in the /var/lib/docker/vfs/dir directory of the host.

data sharing


We can use the --volumes-from parameter when docker run to access volumes from one container to another container.

$ docker run -it --name newcontainer --volumes-from container-test ubuntu /bin/bash

At this point, it doesn't matter whether the container container-test is running or not.

data container


Data containers are typically used to persist databases and data files.

$ docker run --name dbdata mysql echo "data only"

Created a data container named dbdata and stopped after running echo. The data container does not need to be run, as long as it is created.

$ docker run -d --volumes-from dbdata --name db1 mysql

Start a database service container and connect to the dbdata data container.

Note:
- The data container does not need to run, it is purely a waste of resources-
You don't need to use a small image for the data container, just use the database image itself

data backup


If we want to backup MySQL database, we need to backup /var/lib/mysql folder in the data container.

 $ docker run --rm --volumes-from dbdata -v $(pwd):/backup ubuntu tar zcvf /backup/mysql.tar.gz /var/lib/mysql

At this point, a msql.tar.gz file will be generated in the current directory.

 


References:
Docker introductory practice - in-depth understanding of Docker Volume

 

 

http://blog.csdn.net/wangtaoking1/article/details/46005741

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326983028&siteId=291194637