68

This experiment tries to build a container using this Docker file:

FROM lambdalinux/baseimage-amzn:2016.09-000
COPY ./bundle /opt/bundle/
VOLUME /bundle

Then inside the container, create a /opt/bundle/file.txt and put some text in it. But that file did not show up in the bundle directory on the host as I expected after reading Should I include my code with COPY/ADD or a volume last paragraph:

There may be cases where you’ll want to use both. You can have the image include the code using a COPY, and use a volume in your Compose file to include the code from the host during development. The volume overrides the directory contents of the image.

Doesn't Dockerfile VOLUME do the same as docker-compose.yml VOLUME? If so, how can this be done so that changes in the host directory is reflected inside the container directory in this case?

I also created a file on the host bundle/play.txt but that did not show up inside the container /opt/bundle/...

1 Answer 1

86

A VOLUME instruction in the dockerfile creates a mount point but initially only maps it to Docker's internal data directory.

In order to map the volume to the host filesystem, you need to specify which path on the host should be mapped to the volume. You can do this in the docker-compose file using the volumes parameter. (Note: you can create volumes using docker-compose without declaring them in the Dockerfile.)

Note that when mapping a directory from the host to a container, the directory contents on the host will replace the contents in the container, not vice versa.

4
  • 5
    Can I savely omit the dockerfile volume declaration, when I only use the compose.yml volume definition?
    – velop
    Apr 20, 2017 at 8:36
  • 20
    @velop Yes, you can. In fact, it might be preferable in some cases -- see this article.
    – augurar
    Apr 20, 2017 at 16:11
  • 15
    What is the reason to put volume declaration in the dockerfile in the first place?
    – Gherman
    Oct 16, 2019 at 14:41
  • 8
    @Gherman because Dockerfile pre-dates docker-compose.yml and can be used without docker-compose. Jun 18, 2020 at 20:45

Your Answer

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

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