The dynamic data handled by the container can be placed in the read / write layer of the running container, but it has some disadvantages. For example, when a container is deleted, the data in the container also disappears. It cannot be shared between containers. Write performance is also poor compared to writing to data on the host.
Created under / var / lib / docker / volumes. It will be automatically mounted in the container. The created volume can be named and managed, and can be mounted on multiple containers. In addition, common files can be read and written by multiple containers. volume does not disappear when the container is deleted, and does not disappear until it is explicitly deleted. Since moving files is not recommended, this is a feature provided for managing files on the container.
-#Docker to avoid environmental differences-Start a virtual machine with the machine command and work in it.
% docker-machine create vol-test
-#Ssh connection to the created host
% docker-machine ssh vol-test
-#docker volume create new volume name
$ docker volume create my-vol
-#Check for existing volume
$ docker volume ls
-#In the area on the host machine managed by Docker, "my-A volume called "vol" is created.
DRIVER VOLUME NAME
local my-vol
-#docker volume inspect volume name
$ docker valume inspect my-vol
-#docker volume rm volume name
$ docker valume rm my-vol
-# docker run -itd --name container name-v volume name:Mount point container source
$ docker run -itd —-name mount-c1 -v vol1:/app nginx:latest
Specify the volume name and the path to mount on the container separated by colons in the argument of -v. This time, the volume name is ** "vol1" **, and since it does not exist, it will be created as a new volume. The mount point in the container will be ** "/ app" **. You can see that "vol1" is created by the docker volume ls command. You can check the mounted volume by checking the details of the container with the docker inspect command.
$ docker exec -it mount-c1 /bin/bash
# df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 18714000 189496 17535376 2% /
tmpfs 65536 0 65536 0% /dev
tmpfs 504516 0 504516 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/sda1 18714000 189496 17535376 2% /app
tmpfs 504516 0 504516 0% /proc/asound
tmpfs 504516 0 504516 0% /proc/acpi
tmpfs 504516 0 504516 0% /proc/scsi
tmpfs 504516 0 504516 0% /sys/firmware
You can see that "/ app" is mounted.
-#Go to app
# cd app
-#Creating a "hogehoge" file
# touch hogehoge
# ls
hogehoge
-# docker run -itd --name container name--mount source=volume name,target=Mount point container source
$ docker run -itd —-name mount-c2 —-mount source=vol1,target=/app nginx:latest
When mounting with --mount, specify the mount source volume in the source argument and the mount destination in the target argument. The operation is the same as -v.
$ docker exec -it mount-c2 /bin/bash
# ls -l /app
You can see the Hogehoge file. By mounting the same volume on multiple containers in this way, files can be shared. However, this method can only share containers running within the same host, not between different hosts.
-# docker run -itd --name container name--mount source=volume name,destination=Mount point container source
$ docker run -itd —-name mount-c3 —-mount source=copy-vol,destination=/etc/nginx nginx
A volume called "copy-vol" was created and mounted in "/ etc / nginx". The configuration file and directory of "etc / nginx" are copied in volume.
$ docker volume inspect copy-vol
You can see the mount point by looking at the details of Volume. When a new volume is created and mounted, the files that already existed are copied and now exist on the volume. Another mounting pattern is to mount an existing volume. The files on the container are never copied. To the last, the existing volume is mounted at the specified mount point, and the existing files that existed on the container are hidden by the mount volume and become invisible.
You can set whether to make it read / write or read-only for each container. How to mount as read-only using the mount flag.
-# docker run -itd --name container name--mount source=volume name,destination=Mount point,readonly container source
$ docker run -itd —-name mount-c4 -—mount source=copy-vol,destination=/etc/nginx,readonly nginx
Specify "read only" separated by "," commas.
$ docker inspect mount-c4
You can see that "RW": false in "Mounts".
-# docker run -itd --name container name-v volume name:Mount point:ro container source
$ docker run -itd —-name mount-c5 -v copy-vol:/etc/nginx:ro nginx
It can be set to read-only by setting volume name: mount point: ro.
Recommended Posts