Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile vs. docker-compose VOLUME

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

like image 202
Fred J. Avatar asked Nov 12 '16 20:11

Fred J.


People also ask

Should I use Dockerfile or Docker compose?

The key difference between the Dockerfile and docker-compose is that the Dockerfile describes how to build Docker images, while docker-compose is used to run Docker containers.

Does Docker compose create volume?

We can also create a volume with Docker compose service or also specify existing volumes. For example, the following screenshot shows a 'docker-compose' file that creates a docker-compose service with a volume. As a result of the above command, a volume with the name 'myvolume' gets created.

Does Docker compose replace Dockerfile?

No, Dockerfile instructs Docker how to build your image(s). Docker Compose instructs Docker how to run your image(s). Thx, so I have to make a dockerfile just to have the copy command ?

Can Dockerfile work without Docker compose?

The answer is neither. Docker Compose (herein referred to as compose) will use the Dockerfile if you add the build command to your project's docker-compose.


1 Answers

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.

like image 192
augurar Avatar answered Nov 25 '22 11:11

augurar