Docker (c) - storage volume

Why storage volumes?

  • Docker image formed by a plurality of read-only overlay of, when starting container, Docker loads read-only image and adding a reader mirror stack top layer.
  • If you are running container modify an existing file, the file is copied from a read-only layer to layer read-write, read-only version of the file still exists, but has been hidden copy of the file read-write layer, namely " copy COW write " mechanism.

Problems

   United stored in the file system, easy access to the host;
 between container data sharing inconvenient;
 delete their data will be lost container

 

Storage volume volume

"Volume" is one or more of the containers "directory", such a joint may bypass the file system directory, and a directory on the host "binding (association)";

When similar to mount as host / data / web directory and container / container / data / web directory to establish a binding relationship, then the container process to write data to this directory, the directory is written directly on the host machine on, bypassing the container file system and the host file system to establish relationships that can be shared within the host database and the container contents, the contents of the container so that direct access to the host machine, the host can also access the contents of the container, the two are synchronized of. Even if the container is deleted, the data will not be lost.

 The maximum data volume is characteristic: it is independent of the life cycle of the container life cycle 

  • Copy volume is created at the completion of initialization of the container, the volume data provided by the base image will during this
  • The mind is independent of the volume of the container life cycle data persistence, and therefore neither deleted when you delete the volume of container, nor will it even if volumes do not referenced garbage collection operation
  • Providing separate volumes docker data management mechanism of the container

Docker volume type

Docker storage volume there are two types, each type there is a mount point in the vessel, but its location in the host are different

The first: A bind mount volume : path on the host computer needs to manually specify the path in the container also needs to specify, establish two paths known relationship.

The second: Docker manage volumes : only need to specify the mount point of the container, and that is bound under the host directory, create an empty directory by the vessel engine daemon itself, or use an existing directory, and storage establishment storage relationship, a great relief in this way coupling between the user when using the volume defect is that users can not use those specified directory, more suitable for temporary storage

Volumes used in the container

1.docker manage volumes

 In one run container terminal:

docker run -it --name vbox1 -v /data busybox

/ # touch /data/test

 View another terminal:

docker inspect vbox1 # View Container Details

"Mounts": [
{"Type": "volume",
"Name": "257ff9d7f0822606560fc21f8bd63b784cd2db72e410c8634ef361df3f5157b3",
"Source": "/var/lib/docker/volumes/257ff9d7f0822606560fc21f8bd63b784cd2db72e410c8634ef361df3f5157b3/_data",
"Destination": "/data",

# cd /var/lib/docker/volumes/257ff9d7f0822606560fc21f8bd63b784cd2db72e410c8634ef361df3f5157b3/_data
# ls
test

# Echo "hello"> a.txt # can also be seen in the container

2. Bind mount the volume

docker run -it --rm --name vbox2 -v /host/volumes/b2:/data busybox

docker inspect vbox2

"Mounts": [
{"Type": "bind",
"Source": "/host/volumes/b2",
"Destination": "/data",

In the host / / volumes / operator and containers do host b2 / data sync operation. The next time you start hanging container binding in this volume, the file still exists.

Use template to view golang

docker inspect -f {{.Mounts}} vbox2
[{bind /host/volumes/b2 /data1 true rprivate}]

docker inspect -f {{.NetworkSettings.Networks.bridge.IPAddress}} vbox2
172.17.0.5

 

A plurality of containers share the same storage volume

Docker two containers simultaneously allows directory associated with the same host, using the same shared storage volumes, data sharing between the container

docker run -it --rm --name vbox3 -v /host/volumes/b2:/data3 busybox

# Ls / data3

docker run -it --rm --name vbox2 -v /host/volumes/b2:/data2 busybox

# Ls / data2

 

--Volumes-from the copy storage volume

When multiple containers need to use the same into multiple volumes, where each write initialization have to use -v to specify, if you do not record this path, docker also supports copy other storage volume path

   The formulation of a container, do not perform any tasks, create, just specify its storage path, as the basis for container and other containers, to copy its storage volume settings when other containers start, but this point waste, but use joined container of basis, then, several containers already has a close relationship, such as nginx + tomcat, nginx container and container share a tomcat underlying network, there is a external interface, there is a loop interface, so 80 to nginx, to the inner loop tomcat, the request came in, nginx proxy transferred tomcat as a reflection on it, plus a mysql, you can achieve a lnmp architecture. Let them share the name space uts, net, ipc, you can also share storage volumes, ngInx deal with static, tomcat dynamic process, in the same directory, use the storage volume to solve this problem, this organization use to build applications.

  nginx     tomcat     mysql

          Share uts, net, ipc

          Share volume

       

docker run -it --rm --name vbox4 --volumes-from vbox2 busybox

\ # ls data1/
b.txt vbox2 vbox3

To develop the base image (online specialized production infrastructure, container, do not start, as long as you can create a)
# Docker RUN --name infracon -it --rm -v / the Data / Basic / Volume /: / the Data / Web / HTML busybox

# Docker run --name nginx --network container: infracon --volumes-from infracon -it busybox # join the network name of the space, while the volume copy

 

Guess you like

Origin www.cnblogs.com/xiaobaozi-95/p/12301121.html