简体   繁体   中英

Docker mount volume to reflect container files in host

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM