I'm playing with Docker in an EC2 (Amazon Linux2) environment. I noticed that the mount points were multiplying on the host OS. .. .. So I tried various things to deepen my understanding of Volume.
Excerpt from the description of dockerfile-VOLUME in the Docker reference
The> VOLUME instruction creates a mount point with the specified name and makes it an externally mountable volume from another host or container.
The docker run
command initializes the newly created volume as the location to store the data in the location specified from the base image.
After reading this, I understood that "the directory specified by VOLUME is created in the container", but ** "Make it an externally mountable volume from another host or container" ** ,
It was recognized that when the container was started (run), -v
was used to define a directory that could be linked to a directory on the host OS. (If you do not specify -v, it will not be persisted after all and the data will be discarded)
So, I verified the operation of Volume in various ways.
--Try starting the container from an image (CentOS) without VOLUME specification (-v, --mount not specified)
#Volume does not exist(Before starting the container)
[~]docker volume ls
DRIVER VOLUME NAME
#Start container from CentOS image
[~]docker run --name test_container -dt centos
#Volume does not exist(After starting the container)
docker volume ls
DRIVER VOLUME NAME
--Try starting the container from the image with VOLUME specified (-v, --mount not specified) Create an image with Volume specified based on the CentOS image using the docker file below and start it.
#dockerfile(Volume based on CentOS image=/test_Define volume)
ARG baseimagetag=latest
FROM centos:${baseimagetag} AS baseimage
VOLUME /test_volume
#docker image creation
[~]docker build -t centos_mod .
#Volume does not exist(Before starting the container)
[~]docker volume ls
DRIVER VOLUME NAME
#CentOS(Modification)Start container from image
[~]docker run --name test_container -dt centos_mod
#Volume is created(After starting the container)
docker volume ls
DRIVER VOLUME NAME
local ec1feead17bb87e24d3e2e6db716daf58d12747cbdd0b510fd30f507d7f8308b
#Check the mount point on the host OS of Volume
#Automatically/var/lib/docker/volumes/You can see that the mount point is created under it
[~]docker inspect ec1feead17bb87e24d3e2e6db716daf58d12747cbdd0b510fd30f507d7f8308b
[
{
"CreatedAt": "2020-07-10T08:30:00+09:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/ec1feead17bb87e24d3e2e6db716daf58d12747cbdd0b510fd30f507d7f8308b/_data",
"Name": "ec1feead17bb87e24d3e2e6db716daf58d12747cbdd0b510fd30f507d7f8308b",
"Options": null,
"Scope": "local"
}
]
#Container stop, delete
[~]docker stop test_container
[~]docker rm test_container
#Volume confirmed(After deleting the container)
#The mount point on the host OS exists
docker volume ls
DRIVER VOLUME NAME
local ec1feead17bb87e24d3e2e6db716daf58d12747cbdd0b510fd30f507d7f8308b
--Try starting the container from the image with VOLUME specified (specify the Volume name with -v)
#CentOS(Modification)Start container from image(Name the mount point of the host OS(host_mount)Specified by
[~]docker run --name test_container -v host_mount:/test_volume -dt centos_mod
baf6954e9dbce1dd9ad3ae5c8286e63c01fd39689d79053725ae1800b70ea752
#Volume is created(After starting the container)
[~]docker volume ls
DRIVER VOLUME NAME
local host_mount
#Check the mount point on the host OS of Volume
#Automatically/var/lib/docker/volumes/Under subordinate-Name specified by v/_You can see that the mount point is created in data
docker inspect host_mount
[
{
"CreatedAt": "2020-07-10T22:22:49+09:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/host_mount/_data",
"Name": "host_mount",
"Options": null,
"Scope": "local"
}
]
--Try starting the container from the image with VOLUME specified (specify the directory on the host OS with -v)
#CentOS(Modification)Start container from image(Directory of host OS mount points(/test_mount)Specified by
[~]docker run --name test_container -v /test_mount:/test_volume -dt centos_mod
e5e70d6235dbfe54950e2053f61236c1450d43c0b3aacfa38341e37cfacecaaa
#Check Volume(After starting the container)
#The mount point is not displayed with the following command
[~]docker volume ls
DRIVER VOLUME NAME
#Check the mount point from the container information
[~]docker inspect test_container
##Excerpt
"Mounts": [
{
"Type": "bind",
"Source": "/test_mount",
"Destination": "/test_volume",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
--When you start a container using a Docker image with VOLUME defined,
The directory specified by VOLUME is created in the container as a persistence area.
--If you do not specify the mount point (directory) on the host OS that corresponds to the directory specified by VOLUME when starting the Docker container (run), the Volume area of Docker on the host OS (/ var / lib / docker in the above verification) Mount points are automatically created in / volumes)
--When the mount point (name) on the host OS corresponding to the directory specified by VOLUME is specified at startup, the name / _data specified by the mount point in the Volume area of Docker on the host OS is the same as when not specified. Created
--The mount point displayed by docker volume ls
shows the directory created in the Docker Volume area (/ var / lib / docker / volumes) on the host OS.
Display the image information with docker inspect <image name>
and check if the directory is specified in the ** "Volumes": ** part.
Example) For MySQL container / Var / lib / mysql is defined in the container as a persistence area
"Volumes": {
"/var/lib/mysql": {}
},
Recommended Posts