Sharing files between the container and the host?

I see the confusion: I should have started with “that’s not possible!”. You can not mount a container folder to the host. You can only mount a host folder or volume into a container folder.

Furthermore, you can use docker cp {containername or id} {container path} {host path} to copy files from the container filesystem to your host, or you can use the files tab in container details view in Docker Desktop.

I am not sure what this sentence is supposed to mean, but it doesn’t mirror anything, it mounts the host path into a container path. And it is not even a volume in the sense of named volume (as in: not listed with docker volume ls. It’s a bind.

Here is an example.

Preparation on the Windows host:

# create folder
PS C:\Users\me> mkdir d:\bind-test

    Directory: D:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          20.07.2024    20:43                bind-test

Write a file in the container, access it on the host:

# list folder content -> empty
PS C:\Users\me> ls d:\bind-test

# start a container that binds the crated host folder into a container, and writes data in it
PS C:\Users\me> docker run --rm -v d:\bind-test:/test alpine sh -c 'date | tee /test/test.txt'
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
ec99f8b99825: Pull complete
Digest: sha256:b89d9c93e9ed3597455c90a0b88a8bbb5cb7188438f70953fede212a0c4394e0
Status: Downloaded newer image for alpine:latest
Sat Jul 20 18:48:21 UTC 2024

# list folder content -> not empty anymore
PS C:\Users\me> ls d:\bind-test

    Directory: D:\bind-test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20.07.2024    20:48             29 test.txt

# show Content of the file create inside the conatiner
PS C:\Users\me> cat D:\bind-test\test.txt
Sat Jul 20 18:48:21 UTC 2024

Create a file on Windows host, access it inside the container:

# create a file in to the Windows folder
PS C:\Users\me> date > d:\bind-test\test2.txt

# show Content of the file created on the host
PS C:\Users\me> cat D:\bind-test\test2.txt

Samstag, 20. Juli 2024 20:52:45

# show Content of the file from inside the container
PS C:\Users\me> docker run --rm -v d:\bind-test:/test alpine sh -c 'cat /test/test2.txt'

Samstag, 20. Juli 2024 20:52:45