- Docker on Windows
- Elton Stoneman
- 269字
- 2021-07-02 19:53:21
Sharing data between container and host with volumes
Container volumes are stored on the host, so you can access them directly from the machine running Docker - but they'll be in a nested directory somewhere in Docker's program data directory. The docker container inspect command tells you the physical location for a container's volumes, along with a lot more information - I've used it previously to fetch the container's IP address.
I can use explicit JSON formatting in the container inspect command, and extract just the volume information which is in the Mounts field. This command pipes the Docker output into a PowerShell cmdlet to show the JSON in a friendly format:
> docker container inspect --format '{{ json .Mounts }}' source | ConvertFrom-Json
Type : volume
Name : 3514e9620e667028b7e3ca8bc42f3615ea94108e2c08875d50c102c9da7cbc06
Source : C:\ProgramData\Docker\volumes\3514e96...\_data
Destination : c:\app\config
Driver : local
RW : True
Type : volume
Name : a342dc516e19fe2b84d7514067d48c17e5324bbda5f3e97962b1ad8fa4043247
Source : C:\ProgramData\Docker\volumes\a342dc5...\_data
Destination : c:\app\logs
Driver : local
RW : True
I've abbreviated the output, but in the source file you can see the full path where the volume data is stored on the host. I can access the container's files directly from the host, using the source directory. When I run this command on my Windows machine, I'll see the file created inside the container volume:
> ls C:\ProgramData\Docker\volumes\a342dc5...\_data
Directory: C:\ProgramData\Docker\volumes\a342dc5...\_data
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 22/06/2017 08:13 28 log-1.txt
Accessing the files on the host is possible this way, but it's awkward to use the nested directory location with the volume ID. Instead, you can mount a volume from a specific location on the host when you create a container.