26

Docker run command has option to mount host directory into container

-v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. 
       If "host-dir" is missing, then docker creates a new volume.

And Dockerfile has VOLUME instruction

VOLUME ["/data"] - The VOLUME instruction will add one or more new volumes 
                   to any container created from the image.

From what I see, there is no way to specify host-dir or rw/ro status when using Dockerfile.

Is there any other use of VOLUME in docker file other than wanting to share it with some other container?

1 Answer 1

29

Dockerfiles are meant to be portable and shared. The host-dir volume is something 100% host dependent and will break on any other machine, which is a little bit off the Docker idea.

Because of this, it is only possible to use portable instructions within a Dockerfile. If you need a host-dir volume, you need to specify it at run-time.

A common usage of VOLUME from Dockerfile is to store configuration or website sources so that it can be updated later by another container.

4
  • 4
    And what about current directory with Dockerfile? It will always be portable, Commented Apr 23, 2015 at 4:53
  • Thanks for explaining why this is not offered. That makes sense now that I understand the design philosophy. Cannot say I like it, but I understand it now.
    – JA...
    Commented Jul 31, 2015 at 16:45
  • Great answer :). Also see the official Docker documentation at: Notes about specifiying volumes
    – We'll See
    Commented Dec 6, 2022 at 13:59
  • @creack Is not there any way? The Dockerfile is the single start point in my case for airflow. There is no necessity in portability as the image exists for the sole machine. This step is the most convenient for pip install -e command since there is no internet access after the container run & I need to run this command in each final container.
    – Vovin
    Commented Mar 27 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.