31

I am having it difficulty in understanding added advantage of VOLUME(https://docs.docker.com/engine/reference/builder/#volume) .

In Dockerfile one can have mkdir to create a directory. Once the directory is created we can have handle to it. why specify a VOLUME (mount)and assign to that directory? What advantage VOLUME mount gives? I am trying to understand here without VOLUME what will we miss.

to me its looks like a redundant function , however I might be wrong.

T D
  • 1,080
  • 2
  • 13
  • 20
  • Do you know what a [Docker volume](https://docs.docker.com/engine/tutorials/dockervolumes/) is? They're *very* different from a directory inside a container. – Philip Kendall May 15 '17 at 13:40
  • 2
    Volumes are very interesting and there's no doubt of their usefulness over binding a folder. But I also wonder why this Dockerfile VOLUME option. It creates an anonymous volume, and so what ? If you run a new container it will create a new volume and won't reuse the previously created one. If you want to reuse it, you have to docker inspect the container, get the volume id and reuse it in your run command... better create the named volume in the run command the first time. So what are we missing ? Note that I only ask about VOLUME in Dockerfile vs --mount in run command. – Nicolas Massart Oct 30 '18 at 17:11
  • 3
    I'm also highly in doubt that the VOLUME command makes that much sense. I guess people use it to document the interface - but actually they miss the technical consequences, which sometimes are limiting. In fact, when using the VOLUME statement, you will never be able to add files in the space in your dockerfile. You always need to use the external volume - which is for most use cases the desired outcome. However, when extending a Docker-image, the VOLUME command limits which files you can add into the image. All files added to the volume path will be not accessible after starting the image. – user2078148 Jul 26 '19 at 12:47
  • Some people therefore rewrite docker images: https://github.com/goldmann/docker-squash – user2078148 Jul 26 '19 at 12:50
  • 2
    I think that this is a really good question which I have been searching for the answer to for a while. The value of volumes in general is clear but you can use them irrespective of whether the image declares one or not so what is the point of the VOLUME keyword? I tried to ask it more directly [here](https://stackoverflow.com/questions/63845709/what-is-the-actual-advantage-of-declaring-a-volume-in-a-dockerfile) but it was closed as opinion-based. – nickform Sep 11 '20 at 12:13
  • It was worth me posting after all - although my question was closed another SO user was kind enough to add a comment pointing me to https://stackoverflow.com/questions/52570093/what-is-the-practical-purpose-of-volume-in-dockerfile which I think will be of interest to other visitors here. – nickform Sep 11 '20 at 13:51
  • Possible duplicate: https://stackoverflow.com/q/41935435/596285 – BMitch Jan 17 '21 at 19:30
  • 1
    Does this answer your question? [Understanding "VOLUME" instruction in DockerFile](https://stackoverflow.com/questions/41935435/understanding-volume-instruction-in-dockerfile) – Lerner Zhang Feb 09 '23 at 13:55

3 Answers3

12

The VOLUME command will specify a mount point in the container. This mount point will be mapped to a location on the host that is either specified when the container is created or (when not specified) chosen automatically from a directory created in /var/lib/docker/volumes.

If the directory chosen as the mount point contains any files then these files will be copied into this volume. The advantage over mkdir is that it will persist the files to a location on the host machine after the container is terminated.

It appears some people have questioned why you would use the VOLUME command since it creates an anonymous volume. Anonymous volumes don't have much use any more and are basically an artifact of the early days of Docker before volumes could be named. You would normally specify the volume name when creating the container:

docker container run -v my-volume:/data image_tag

In this example, /data is the mount point in the container and my-volume is the volume on the local host. If my-volume does not exist when this command is run then it is created on the local host.

Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
rmullig2
  • 121
  • 1
  • 2
2

A volume is very helpful when we don't want to lose data once the container is deleted. A configuration file, a database data, etc. Hence, you are able to use the same volume with a complete new docker container. If you only create a directory within the Dockefile, the data inside the folder will be deleted toghether with the container.

kimy82
  • 4,069
  • 1
  • 22
  • 25
  • 8
    This still doesn't explain what exactly the `VOLUME` command does. You can mount a volume to the container and use it without specifying it in the Dockerfile. – mj3c Oct 27 '20 at 14:28
  • 1
    It's possible to mount host directory using `--volume` or `--mount` flags with `docker run`. But the OP asks about `VOLUME` instuction within Dockerfile. What is charm of it? – Alexander Feb 16 '21 at 16:44
1

The mkdir in the Dockerfile will make a directory within that container only. A volume can be on the host machine or a different container, and so remains in existence when the container is killed.

Colm Prunty
  • 1,595
  • 1
  • 11
  • 29
  • 4
    It's possible to mount host directory using `--volume` or `--mount` flags with `docker run`. But the OP asks about `VOLUME` instuction within Dockerfile. What is charm of it? – Alexander Feb 16 '21 at 16:43