我正在学习docker,我对数据卷实际存在的地方感到困惑。
我正在使用Windows的Docker Desktop。(Windows 10)
在文档中,他们说在对象上运行docker inspect会给你源代码:https://docs.docker.com/engine/tutorials/dockervolumes/#locating-a-volume
$ docker inspect web
"Mounts": [
{
"Name": "fac362...80535",
"Source": "/var/lib/docker/volumes/fac362...80535/_data",
"Destination": "/webapp",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
然而,我没有看到这一点,我得到了以下:
$ docker inspect blog_postgres-data
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/blog_postgres-data/_data",
"Name": "blog_postgres-data",
"Options": {},
"Scope": "local"
}
]
有人能帮帮我吗?我只想知道我的数据量在哪里是在主机上吗?如果是这样,我怎么能得到它的路径?
挂载任何基于NTFS的目录都不能达到我的目的(MongoDB -据我所知,至少Redis和CouchDB也是如此):NTFS权限不允许对容器中运行的此类db进行必要的访问。下面是HyperV上命名卷的设置。
下面的方法在服务中启动ssh服务器,使用docker-compse进行设置,以便自动启动并在主机和容器之间使用公钥加密进行授权。这样可以通过scp或sftp上传/下载数据。
完整的船坞式结构。下面是一个webapp + mongodb的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
更新:请注意,现在你也可以只使用docker cp,所以上面概述的sshd容器可能不再需要了,除非你需要远程访问运行在Windows主机下的容器中的文件系统。