1

What is the use of "VOLUME" or "RUN mkdir /m"?

Even if I do not specify any of these instructions in the Dockerfile, then also "docker run -v ${PWD}/m:/m" works.

Pankaj Jangid
  • 524
  • 3
  • 18
  • 1
    Possible duplicate of [what does VOLUME inside Dockerfile do](https://stackoverflow.com/questions/52862705/what-does-volume-inside-dockerfile-do) – David Maze Nov 27 '18 at 17:02
  • 1
    TL;DR: you probably want to `RUN mkdir` in case the `-v` option wasn’t provided. You probably don’t want an explicit `VOLUME` unless you’re _very_ clear about what it does. – David Maze Nov 27 '18 at 17:10
  • @DavidMaze, Even if I build without `RUN mkdir` and without `VOLUME`, the external mount is still working. That is if I supply `-v` option. – Pankaj Jangid Nov 28 '18 at 03:37

1 Answers1

1

Inside a Dockerfile, VOLUME marks a directory as a mount point for an external volume. Even if the docker run command doesn't mount an existing folder into that mount point, docker will create a named volume to hold the data.

RUN mkdir /m does what mkdir does on any Unix system. It makes a directory named m at the root of the filesystem.

docker run -v ... binds a host directory to a volume inside a container. It will work whether or not the mount point was declared as a volume in a Dockerfile, and it will also create the directory if it doesn't exist. So neither VOLUME or RUN mkdir are specifically necessary before using that command, though they may be helpful to communicate the intent to the user.

mkasberg
  • 16,022
  • 3
  • 42
  • 46
  • Directories that are created inside a container using the command "mkdir" gets removed when the container is removed. Is it true that volumes are not removed? – Pankaj Jangid Dec 03 '18 at 18:40
  • 1
    There are a lot of intricacies around how volumes work. But in general, if you mount a host directory as a volume, then any data would persist on the host. If you don't mount a host directory, docker will create a volume for you. If you then remove the container, the docker volume would be orphaned (but still possible to reuse until you remove it). Reading about the `docker volume` command will go through some of these ideas in more depth. – mkasberg Dec 03 '18 at 18:49