Is there a way to override the host's folder with the container's folder using volumes in Docker?

Viewed 156

I'm fairly new to using Docker and Docker Compose (using Docker Compose for this particular problem). Here is what I know so far about the problem I am facing: When using volumes when there are contents available in the host folder as well as the container's folder, the files inside the container's folder are hidden and the host's files are then made available to the container.

I want to use it the other way round. I would like to make available the container's files (that were copied into the image in the Dockerfile) to the host folder.

Is there a way to do that?

Here are a bunch of screenshots of my Dockerfile and Docker Compose to show my setup.

Dockerfile Screenshot

DockerCompose Screenshot

Thanks in advance! :)

1 Answers

I've come across the same thing many times and the way I go about it is as follows.

As the host volume will always take priority over the container filesystem, you have to copy the files out of the container to the host first, then volume mount them back - this way you get what was there originally, and also what might change in the future (by the container).

The following is all pseudo code, but should hopefully simulate the concept:

First run the main container:

docker run --rm -d --name my-container registry/image-name

Then copy the files you want from it to the local filesystem

docker cp my-container:/files/i/want ./files

Then stop the original container

docker stop my-container

Then mount them back into the container on the next run

docker run --rm -d --name my-container -v ./files:/files/i/want registry/image-name


Obviously you've mentioned compose there also, so just reflect the volume mapping into the compose format - the copy stuff will need to be done via standard docker however in line with the above.

Note: I wrote the above commands blind, but will check them over at lunch and correct any mistypes - but the concept is correct

Related