首页 > 解决方案 > Docker for Windows: Accessing named volume mounts

问题描述

Please note that this is not a duplicate of Locating data volumes in Docker Desktop (Windows) as back in 2017 the inner workings of docker on windows were quite different - e.g. docker volume inspect output is quite different nowadays.

I have trouble accessing data mounted to containers in docker for windows via named volume mounts.

docker inspect [vol-id]

[
{
    "CreatedAt": "2019-04-02T11:58:14Z",
    "Driver": "local",
    "Labels": {
        "com.docker.compose.project": "foo",
        "com.docker.compose.version": "1.24.0",
        "com.docker.compose.volume": "mongodata-foo"
    },
    "Mountpoint": "/var/lib/docker/volumes/foo_mongodata-foo/_data",
    "Name": "foo_mongodata-payoff",
    "Options": null,
    "Scope": "local"
}
]

--> Mountpoint is inside the HyperV VM used in Docker. How to get access to that data? Is there an easily manageable way to to this?

Note: I don't have C:\ProgramData\Docker\Volumes as described here. Instead, what was created with Docker Desktop 2.0.0.3, Engine 18.03.3, was C:\ProgramData\DockerDesktop. That does not contain any Volumes as far as I can tell.

Background: I need named mounts with default location inside HyperV, as mounting it manually via docker run -v or specifying the driver device location looks to be unsupported by mongodb (I have exactly the same behavior as described there. It looks like mongodb is incompatible with NTFS-originating volume mounts.

标签: dockerhyper-vdocker-volumedocker-for-windowsdocker-desktop

解决方案


使用内置 docker cp 的方法

用于docker cp [containername]:[path] [host-path]例如复制数据 - 反转参数以复制数据 - 它就像 scp 一样工作。要获得对数据的 shell 访问,您只需附加到正在运行的容器即可。

亲:在 docker compose 中不需要额外的东西

缺点:没有与 WinSCP 之类的文件浏览器 GUI 集成(据我所知)。每次在主机和容器之间更新文件时,都需要进行基于终端的复制。

使用 dockerized ssh 服务器的方法

pro:可以与任何可以通过 ssh/sftp 通信的工具集成

缺点:需要额外的设置

以下方法在服务中启动 ssh 服务器,使用 docker-compse 进行设置,使其自动启动并在主机和容器之间使用公钥加密进行授权。这样,可以通过 scp 或 sftp 上传/下载数据。

下面是 node.js (keystone) + mongodb 应用程序的完整 docker-compose.yml,以及一些关于如何使用 ssh 服务的文档:

version: '3'
services:
  foo:
    build: .
    image: localhost.localdomain/${repository_name}:${tag}
    container_name: ${container_name}
    ports:
      - "3333:3333"
    links:
      - mongodb-foo
    depends_on:
      - mongodb-foo
      - sshd
    volumes:
      - "${host_log_directory}:/var/log/app"

  mongodb-foo:
    container_name: mongodb-${repository_name}
    image: "mongo:3.4-jessie"
    volumes:
      - mongodata-foo:/data/db
    expose:
      - '27017'

  #since mongo data on Windows only works within HyperV virtual disk (as of 2019-4-3), the following allows upload/download of mongo data
  #setup: you need to copy your ~/.ssh/id_rsa.pub into $DOCKER_DATA_DIR/.ssh/id_rsa.pub, then run this service again
  #download (all mongo data): scp -r -P 2222 user@localhost:/data/mongodb [target-dir within /c/]
  #upload (all mongo data): scp -r -P 2222 [source-dir within /c/] user@localhost:/data/mongodb
  sshd:
    image: maltyxx/sshd
    volumes:
        - mongodata-foo:/data/mongodb
        - $DOCKER_DATA_DIR/.ssh/id_rsa.pub:/home/user/.ssh/keys/id_rsa.pub:ro
    ports:
        - "2222:22"
    command: user::1001

#please note: using a named volume like this for mongo is necessary on Windows rather than mounting an NTFS directory.
#mongodb (and probably most other databases) are not compatible with windows native data directories due ot permissions issues.
#this means that there is no direct access to this data, it needs to be dumped elsewhere if you want to reimport something.
#it will however be persisted as long as you don't delete the HyperV virtual drive that docker host is using.
#on Linux and Docker for Mac it is not an issue, named volumes are directly accessible from host.
volumes:
  mongodata-foo:

注意:对于一个完整的示例,在任何 docker-compose 调用之前,需要运行以下脚本:

#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset

working_directory="$(pwd)"
host_repo_dir="${working_directory}"
repository_name="$(basename ${working_directory})"
branch_name="$(git rev-parse --abbrev-ref HEAD)"
container_name="${repository_name}-${branch_name}"
host_log_directory="${DOCKER_DATA_DIR}/log/${repository_name}"
tag="${branch_name}"

export host_repo_dir
export repository_name
export container_name
export tag
export host_log_directory

推荐阅读