Dockerfile vs. docker-compose VOLUME

DockerDocker Compose

Docker Problem Overview


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/...

Docker Solutions


Solution 1 - Docker

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFred J.View Question on Stackoverflow
Solution 1 - DockeraugurarView Answer on Stackoverflow