patterndockerMinor
How Docker volume works?
Viewed 0 times
dockerworksvolumehow
Problem
Somebody creates a php and Apache project, with docker, using volumes to persist the files that this project generated (plain text files saved in a folder).
This person built the image and ran the project normally, with the exception of one detail, (s)he didn't link the folder where (s)he saved the messages (www/site/messages) (s)he simply used the /data folder, in which the project had no connection. And it worked.
How does the volume really work? Because to me it doesn't make sense.
This is the project:
https://github.com/matheusbattisti/curso_docker/tree/main/2_volumes
This person built the image and ran the project normally, with the exception of one detail, (s)he didn't link the folder where (s)he saved the messages (www/site/messages) (s)he simply used the /data folder, in which the project had no connection. And it worked.
How does the volume really work? Because to me it doesn't make sense.
This is the project:
https://github.com/matheusbattisti/curso_docker/tree/main/2_volumes
Solution
Dockerfile's
On the host-side, the volumes are created with a very long ID-like name, these volumes are often referred to as unnamed/anonymous volumes.
Given this Dockerfile:
Build it:
Run it :
Inside the container, run
Running the container also creates a directory on the host-side.
While having the container running, execute
Back in the container, execute
This file is now available on the host machine, in one of the unnamed volumes.
Similarly, you can try to delete this file on the host and it will be deleted in the container as well.
Now run a new container, but specify a volume using
This adds a second volume and the whole system ends up having two unnamed volumes.
On the host-side, the new second volume is anonymous and resides together with the other volume in
It was stated earlier that the
Imagine I have a subfolder in my project directory
Both sides of the
We run this command:
Is the specification purely informational?
No, it does mount a volume, it's similar to
What are the (dis)advantages of (not) specifying volumes?
Why and in which cases should I use VOLUME and when shouldn't I?
It's probably a best practice to never use
The first reason we have already identified: We can not specify the host path.
The second reason is people might forget to use the
What about files that may optionally be mounted? Do they count as volumes in that case, too?
In this case you don't need volumes; mapping a single file does not make sense, I would use
VOLUME does not allow you to specify a host path.On the host-side, the volumes are created with a very long ID-like name, these volumes are often referred to as unnamed/anonymous volumes.
Given this Dockerfile:
FROM php7:latest
VOLUME /var/wwwBuild it:
docker build -t myTestRun it :
docker run --rm -it myTestInside the container, run
ls and you'll notice the directory exists; /var/wwwRunning the container also creates a directory on the host-side.
While having the container running, execute
docker volume ls on the host machine and you'll see something like thisDRIVER VOLUME NAME
local c984..e4fcBack in the container, execute
touch /var/www/myFileThis file is now available on the host machine, in one of the unnamed volumes.
ls /var/lib/docker/volumes/c984..e4fc/_dataSimilarly, you can try to delete this file on the host and it will be deleted in the container as well.
Now run a new container, but specify a volume using
-v:docker run --rm -it -v /myVolume myTestThis adds a second volume and the whole system ends up having two unnamed volumes.
On the host-side, the new second volume is anonymous and resides together with the other volume in
/var/lib/docker/volumes/.It was stated earlier that the
Dockerfile can not map to a host path which sort of pose a problem for us when trying to bring files in from the host to the container during runtime. A different -v syntax solves this problem.Imagine I have a subfolder in my project directory
./src that I wish to sync to /src inside the container. This command does the trick:docker run -it -v $(pwd)/src:/src myTestBoth sides of the
: character expects an absolute path. Left side being an absolute path on the host machine, right side being an absolute path inside the container.We run this command:
docker run -v $(pwd)/src:/src myTestIs the specification purely informational?
No, it does mount a volume, it's similar to
-v /var/www you just don't specify a mount point in the host machine, so docker will take care of it.What are the (dis)advantages of (not) specifying volumes?
Why and in which cases should I use VOLUME and when shouldn't I?
It's probably a best practice to never use
VOLUME.The first reason we have already identified: We can not specify the host path.
The second reason is people might forget to use the
--rm option when running the container, you will end up with a couple of unused volumes and it might be a daunting task to figure out which of all anonymous volumes are safe to removeWhat about files that may optionally be mounted? Do they count as volumes in that case, too?
In this case you don't need volumes; mapping a single file does not make sense, I would use
ADD or COPY inside the dockerfile.Code Snippets
FROM php7:latest
VOLUME /var/wwwdocker build -t myTestdocker run --rm -it myTestDRIVER VOLUME NAME
local c984..e4fcls /var/lib/docker/volumes/c984..e4fc/_dataContext
StackExchange DevOps Q#15745, answer score: 3
Revisions (0)
No revisions yet.