Could some one help me to understand the difference between:
VOLUME
command in Dockerfile
(image building layer)
and
-v
parameter when issuing docker run
-v/xyz/bla
command (container building layer).
-v
parameter is for me clear, it simply exposes a directory from the host to the container and vice versa, but how does VOLUME
in Dockerfile
behave differently?
In a nutshell
The
VOLUME [PATH]
instruction inside a Dockerfile is equivalent toDetailed explanation
Using volumes in Docker is primarily less a matter of speed than a matter of data persistance independet from a container's life cycle. Mounting volumes from a faster disk will obviously improve performance, but Docker's default behavior for
VOLUME
is to create a named volume on the host system with little to no speed improvements compared to the container's writable layer.While this is partly true,
-v
can also be used to mount named volumes into your Docker container instead of a directory. This little detail is important in order to understand whatVOLUME
does. An example:Here a volume named
my_volume
was created. It behaves as would expect from a 'normal' mount. Any changes to[PATH]
inside the container will be persisted in this volume. The difference is that Docker manages the volume's location, so that you don't need to worry (it is/var/lib/docker/volumes/my_volume/_data
in case you're interested). Why would you want this? You could have a test database. While you don't need direct access to the files, you might want to save the current state to mount it into other database containers.The
VOLUME [PATH]
instruction basically saves the above instructions into the image's metainformation. So everytime you start a container from this image, Docker knows that you want to persist[PATH]
in a volume and takes care of that.The
-v
parameter andVOLUME
keyword are almost the same. You can use-v
to have the same behavior asVOLUME
.Same as
But also -v have more uses, one of them is where map to the volume:
So the question is: what is the use of
VOLUME
in aDockerfile
?The container filesystem is made of layers so writing there, is slower and limited (because the fixed number of layers) than the plain filesystem.
You declare
VOLUME
in yourDockerfile
to denote where your container will write application data. For example a database container, its data will go in a volume regardless what you put in yourdocker run
.If you create a docker container for JBoss and you want to use fast filesystem access with
libaio
yo need to declare the data directory as aVOLUME
or JBoss will crash on startup.In summary
VOLUME
declares a volume regardless what you do indocker run
. In fact indocker run
you cannot undo aVOLUME
declaration made inDockerfile
.Regards