Data sharing is a key feature of volume. Today, let's take a look at sharing data between containers and host s, and between containers through volume.
1, The container shares data with the host
The bind mount and docker manage volume introduced in the previous article can share data between the container and the host, but in different ways. Before the container is started, bind mount specifies the host data directory where the volume is located and mounts it into the container. After the container is started, we write data to this directory, and the container can also use these data, The docker manage volume is different. It can only determine the directory of the host where the volume is located when the container is started. Therefore, the docker cp command is needed here. It can copy data between the container and the host.
$ sudo docker run -d -p 80:80 -v /usr/local/apache2/htdocs httpd 0320b31996408b61a1bc363f999509f3bfdc17ca292dd08bd5f7496edb7c8947 $ sudo docker cp ~/htdocs/index.html 0320b31996:/usr/local/apache2/htdocs $ curl http://127.0.0.1:80 <h1>update page</h1>
2, Sharing data between containers
1,bind mount
The first method is bind mount. The data to be shared is mounted to multiple containers through bind mount. For example, we start three httpd containers and let them mount the same htdocs.
$ sudo docker run --name web1 -d -p 80 -v ~/htdocs:/usr/local/apache2/htdocs httpd f5a911434445f431d511b5292112fe0f9b9b44b868f98561feba895107e0cb40 $ sudo docker run --name web2 -d -p 80 -v ~/htdocs:/usr/local/apache2/htdocs httpd 61e43e584c33efdab7ef5c1a72a5a4e9a0d40731d860e5fd9fb23de0a7c767df $ sudo docker run --name web3 -d -p 80 -v ~/htdocs:/usr/local/apache2/htdocs httpd 6494f68481a0f1971fdc33c13b969706c69b8ecaedc39aa707b6d35d32b6fba0 $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6494f68481a0 httpd "httpd-foreground" 11 seconds ago Up 9 seconds 0.0.0.0:49155->80/tcp, :::49155->80/tcp web3 61e43e584c33 httpd "httpd-foreground" 19 seconds ago Up 17 seconds 0.0.0.0:49154->80/tcp, :::49154->80/tcp web2 f5a911434445 httpd "httpd-foreground" 28 seconds ago Up 27 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp web1 $ curl http://127.0.0.1:49155 <h1>hello docker</h1> $ curl http://127.0.0.1:49154 <h1>hello docker</h1> $ curl http://127.0.0.1:49153 <h1>hello docker</h1>
2,volume container
Volume container is a container dedicated to providing volume for other containers. The volume it provides can be bind mount or docker managed volume. First, we create a volume container:
$ sudo docker create --name vc_data -v ~/htdocs:/usr/local/apache2/htdocs -v /other/useful/tools busybox 178dd66f492dbe0485816a9f6f9ecde97c03e322841e6b38f4a2c7c439c6f020
We use the other tool, volume bind, to mount the other page statically. Note that we use the docker create command because the volume container only provides data and does not need to be running.
You can see these two volume s through docker inspect:
$ sudo docker inspect vc_data [ ... "Mounts": [ { "Type": "bind", "Source": "/home/yangye/htdocs", "Destination": "/usr/local/apache2/htdocs", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "volume", "Name": "e3ba7240ef18694660ed3da4e6976fa35bb5c2d3c26f91593ea30d26289e0fd6", "Source": "/var/lib/docker/volumes/e3ba7240ef18694660ed3da4e6976fa35bb5c2d3c26f91593ea30d26289e0fd6/_data", "Destination": "/other/useful/tools", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ... ]
Then we start the container through -- volumes_from uses the VC just created_ data:
$ sudo docker run --name web1 -d -p 80 --volumes-from vc_data httpd 14131cc7057528ac892b2661ed46e543bb30b65af789d83f0770dab4ebb06a33 $ sudo docker run --name web2 -d -p 80 --volumes-from vc_data httpd 320a11bcb64ee032c65b65ee744a7afd6ebc7a24330e68c688a4b9c4b5e5eb22 $ sudo docker run --name web3 -d -p 80 --volumes-from vc_data httpd f4c4bed873f3ad76b37136fc93dd915c6e4915897e7628f3b1ca1d5fc30e920a
Let's take web1 as an example to see if its volume is correct:
$ sudo docker inspect web1 [ ... "Mounts": [ { "Type": "bind", "Source": "/home/yangye/htdocs", "Destination": "/usr/local/apache2/htdocs", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "volume", "Name": "e3ba7240ef18694660ed3da4e6976fa35bb5c2d3c26f91593ea30d26289e0fd6", "Source": "/var/lib/docker/volumes/e3ba7240ef18694660ed3da4e6976fa35bb5c2d3c26f91593ea30d26289e0fd6/_data", "Destination": "/other/useful/tools", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ...
It can be seen that web1 uses vc_data volume, let me verify the effect of data sharing:
$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4c4bed873f3 httpd "httpd-foreground" 10 minutes ago Up 10 minutes 0.0.0.0:49158->80/tcp, :::49158->80/tcp web3 320a11bcb64e httpd "httpd-foreground" 10 minutes ago Up 10 minutes 0.0.0.0:49157->80/tcp, :::49157->80/tcp web2 14131cc70575 httpd "httpd-foreground" 10 minutes ago Up 10 minutes 0.0.0.0:49156->80/tcp, :::49156->80/tcp web1 $ curl http://127.0.0.1:49158 <h1>hello docker</h1> $ curl http://127.0.0.1:49157 <h1>hello docker</h1> $ curl http://127.0.0.1:49156 <h1>hello docker</h1>
All three containers share volume_ Compared with bind mount, volume container has the following characteristics:
1) It is not necessary to specify the host path for each container. All paths are defined in the volume container. The container only needs to be associated with the volume container to realize the decoupling between the container and the host;
2) The mount point s of containers using volume container are consistent, which is conducive to the standardization and standardization of configuration. Of course, there are some limitations, which should be considered comprehensively when using.