1

The use case is that I want to download and image that contains python code files. Assume the image does not have any text editor installed. So i want to mount a drive on host, so that files in the container show up in this host mount and i can use different editors installed on my host to update the code. Saving the changes are to be reflected in the image.

if i run the following >

docker run -v /host/empty/dir:/container/folder/with/code/files -it myimage

the /host/empty/dir is still empty, and browsing the container dir also shows it as empty. What I want is the file contents of /container/folder/with/code/files to show up in /host/empty/dir

Imti
  • 13
  • 4
  • Can you get the original source code of the application, make your changes, make `pytest` pass, and then `docker build` a new image? That'd be the more typical Docker path. – David Maze Feb 26 '19 at 22:54

2 Answers2

0

Your /host/empty/dir is always empty because the volume binding replaces (overrides) the container folder with your empty host folder. But you can not do the opposite, that is, you take a container folder to replace your host folder.

However, there is a workaround by manually copying the files from your container folder to your host folder. before using them as you have suggested.

For exemple :

  1. run your docker image with a volume maaping between you host folder and a temp folder : docker run -v /host/empty/dir:/some-temp-folder -it myimage
  2. copy your /container/folder/with/code/files content into /some-temp-folder to fill you host folder with you container folder
  3. run you container with a volum mapping on /host/empty/dir but now this folder is no longer empty : run -v /host/empty/dir:/container/folder/with/code/files -it myimage

Note that steps 1 & 2 may be replaced by : Copying files from Docker container to host

Sébastien Helbert
  • 2,185
  • 13
  • 22
0

Sébastien Helbert answer is correct. But there is still a way to do this in 2 steps.

First run the container to extract the files:

docker run --rm -it myimage

In another terminal, type this command to copy what you want from the container.

docker cp <container_id>:/container/folder/with/code/files /host/empty/dir

Now stop the container. It will be deleted (--rm) when stopped. Now if you run your original command, it will work as expected.

docker run -v /host/empty/dir:/container/folder/with/code/files -it myimage

There is another way to access the files from within the container without copying it but it's very cumbersome.

Paulo Pedroso
  • 3,555
  • 2
  • 29
  • 34