Docker on Windows
上QQ阅读APP看书,第一时间看更新

Sharing data between the 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, including the container's ID, name, and the virtual IP address of the container in the Docker network.

I can use JSON formatting in the container inspect command, passing a query to extract just the volume information 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 : 65ab1b420a27bfd79d31d0d325622d0868e6b3f353c74ce3133888fafce972d9
Source : C:\ProgramData\docker\volumes\65ab1b42...\_data
Destination : c:\app\config
Driver : local
RW : TruePropagation :

Type : volume
Name : b1451fde3e222adbe7f0f058a461459e243ac15af8770a2f7a4aefa7516e0761
Source : C:\ProgramData\docker\volumes\b1451fde...\_data
Destination : c:\app\logs
Driver : local
RW : True

I've abbreviated the output, but in the Source field 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 that 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\b1451fde...\_data
Directory: C:\ProgramData\docker\volumes\b1451fde3e222adbe7f0f058a461459e243ac15af8770a2f7a4aefa7516e0761\_data

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 06/02/2019 13:33 19 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.