We can have a data volume in docker:
$ docker run -v /path/to/data/in/container --name test_container debian
$ docker inspect test_container
...
Mounts": [
{
"Name": "fac362...80535",
"Source": "/var/lib/docker/volumes/fac362...80535/_data",
"Destination": "/path/to/data/in/container",
"Driver": "local",
"Mode": "",
"RW": true
}
]
...
But if the data volume lives in /var/lib/docker/volumes/fac362...80535/_data
, is it any different from having the data in a folder mounted using -v /path/to/data/in/container:/home/user/a_good_place_to_have_data
?
The difference between host directory and a data volume is in that that Docker manages the latter by placing it into the
$DOCKER-DATA-DIR/volumes
directory and attaching a reference to it (names or randomly generated ids). That is you get a little bit of convenience.Both host directories and data volumes are directories on the host. Both are host dependent. You can't reference either of them in a
Dockerfile
; theVOLUME
directive creates a new nameless (with randomly generated id) volume every time you launch a new container and cannot reference an existing volume.*
$DOCKER-DATA-DIR
is/var/lib/docker
here unless you changed the defaults.It is because, as mentioned in "Mount a host directory as a data volume"
You can combine both approaches:
Although when using them it feels the same, with the only change of the location of the directory, there is a different.
Volumes vs Bind Mounts
Volumes advantages over bind mounts:
Volumes
Bind mounts
There is also
tmpfs mounts
.tmpfs mounts
Reference:
https://docs.docker.com/storage/