适用于Windows的Docker:访问命名的卷挂载

时间:2019-04-02 12:22:31

标签: docker hyper-v docker-volume docker-for-windows docker-desktop

请注意,这不是Locating data volumes in Docker Desktop (Windows)的副本,不是,因为早在2017年,Windows上docker的内部工作方式就大不相同-例如如今,码头工人卷检查输出完全不同。

我无法通过命名的卷安装访问docker中用于Windows的容器中安装的数据。

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位于Docker中使用的HyperV VM中。如何获得该数据?有一种容易管理的方法吗?

注意:我没有here所述的C:\ProgramData\Docker\Volumes。相反,使用Docker Desktop 2.0.0.3 Engine 18.03.3创建的是C:\ProgramData\DockerDesktop。据我所知,该卷不包含任何卷。

背景:需要将其命名为在HyperV内部具有默认位置的安装,因为通过docker run -v手动安装或指定驱动程序设备的位置看起来是{{ 3}}(我的行为与那里描述的完全相同。看起来mongodb与源自NTFS的卷挂载不兼容。

1 个答案:

答案 0 :(得分:0)

以下方法将启动服务中的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