在 Windows 上使用 docker compose 进行卷绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41334021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Volume binding using docker compose on Windows
提问by 9997
I recently upgraded my Docker Toolbox on Windows 10, and now my volume mounts no longer work. I've tried everything. Here is the current mount path:
我最近在 Windows 10 上升级了我的 Docker 工具箱,现在我的卷挂载不再有效。我什么都试过了。这是当前的挂载路径:
volumes:
- C:\Users\Joey\Desktop\backend:/var/www/html
I receive an invalid bind mount error.
我收到无效的绑定安装错误。
回答by friism
I think you have to set COMPOSE_CONVERT_WINDOWS_PATHS=1
, see here.
我想你必须设置COMPOSE_CONVERT_WINDOWS_PATHS=1
,看这里。
Docker Machine should do it automatically: https://github.com/docker/machine/pull/3830
Docker Machine 应该自动执行:https: //github.com/docker/machine/pull/3830
回答by Amit Jain
回答by Top-Master
It seems you are using an absolute path located inside C:\Users
dir, that didn't work for me too, if you are using Docker-Toolbox
see below.
看来您使用的是位于C:\Users
dir内的绝对路径,如果您使用,Docker-Toolbox
请参见下文,这对我也不起作用。
Overview
概述
Forwarding the ./
relative path in volumes
section will automatically get resolved by docker-compose
to the directory containing docker-compose.yml
file (for example, if your project is in %UserProfile%/my-project
then ./:/var/www/html
gets /c/Users/my-name/my-project:/var/www/html
).
转发部分中的./
相对路径volumes
将自动解析docker-compose
到包含docker-compose.yml
文件的目录(例如,如果您的项目在,%UserProfile%/my-project
则./:/var/www/html
获取/c/Users/my-name/my-project:/var/www/html
)。
The problem is that currently (using DockerToolbox-19.03.1
) only the /c/Users
directory gets shared with the Virtual-Machine (toolbox puts docker
itself in the VM, which means it has no access to your file system, except mounted shared-directories).
问题是目前(使用DockerToolbox-19.03.1
)只有/c/Users
目录与虚拟机共享(工具箱将docker
自己放在虚拟机中,这意味着它无法访问您的文件系统,除了挂载的共享目录)。
Conclusion
结论
So, basically placing your project there (C:\Users\YOUR_USER_NAME
) should make ./
work.
But not even that workedfor me, and we ended up with below _prepare.sh
script:
所以,基本上把你的项目放在那里 ( C:\Users\YOUR_USER_NAME
) 应该可以./
工作。但即使这样对我也不起作用,我们最终得到了以下_prepare.sh
脚本:
#!/bin/bash
VBoxManage='/c/Program Files/Oracle/VirtualBox/VBoxManage'
# Defines variables for later use.
ROOT=$(dirname volumes:
- /data/YOUR_USERNAME/projects/my_project/jssecacerts:/usr/lib/jvm/java-1.8-openjdk/jre/lib/security/jssecacerts/
)
ROOT=$(cd "$ROOT"; pwd)
MACHINE=default
PROJECT_KEY=shared-${ROOT##*/}
# Prepares machine (without calling "docker-machine stop" command).
#
if [ $(docker-machine status $MACHINE 2> /dev/null) = 'Running' ]; then
echo Unmounting volume: $ROOT
docker-compose down
docker-machine ssh $MACHINE <<< '
sudo umount "'$ROOT'";
'
"$VBoxManage" sharedfolder remove $MACHINE --name "$PROJECT_KEY" -transient > /dev/null 2>&1
else
docker-machine start $MACHINE
fi
eval $(docker-machine env $MACHINE)
set -euxo pipefail
"$VBoxManage" sharedfolder add $MACHINE --name "$PROJECT_KEY" --hostpath "$ROOT" -automount -transient
docker-machine ssh $MACHINE <<< '
echo Mounting volume: '$ROOT';
sudo mkdir -p "'$ROOT'";
sudo mount -t vboxsf -o uid=1000,gid=50 "'$PROJECT_KEY'" "'$ROOT'";
'
docker-compose up -d
docker-machine ssh $MACHINE
bash
Usage:
用法:
- Place a copy of it beside each project's
docker-compose.yml
file. - Run it each time the system is turned on (simply double-click it or its shortcut).
- Done! relative paths should now work even if your project is in another drive (far away and outside of
C:\Users
dir).
- 将它的副本放在每个项目的
docker-compose.yml
文件旁边。 - 每次系统打开时运行它(只需双击它或其快捷方式)。
- 完毕!即使您的项目在另一个驱动器中(远离
C:\Users
dir并且在dir之外),相对路径现在也应该可以工作。
Note:
笔记:
- With a little edit, it should work without
docker-compose
being required. - Consider running
docker system prune
to free disk-space (or simply adddocker system prune --force
to the above script, on a new line right aftermount
command).
- 稍加编辑,它应该可以
docker-compose
在不需要的情况下工作。 - 考虑运行
docker system prune
以释放磁盘空间(或简单地添加docker system prune --force
到上面的脚本中,在mount
命令之后的新行中)。
回答by Leonid Dashko
I faced with same issue (I'm using Docker Desktop).
我遇到了同样的问题(我正在使用 Docker 桌面)。
My steps were:
我的步骤是:
1) Place your folder under drive "C"
1)将您的文件夹放在驱动器“C”下
2) Open "Settings" in Docker Desktop -> "Shared Drives" -> "Reset Credentials" -> select drive "C" -> "Apply"
2)在Docker Desktop中打开“设置”->“共享驱动器”->“重置凭据”->选择驱动器“C”->“应用”
3) Open terminal and run (as proposed by Docker Desktop):docker run --rm -v c:/Users:/data alpine ls /data
3)打开终端并运行(由Docker Desktop提出):docker run --rm -v c:/Users:/data alpine ls /data
4) Open your docker-compose.yml
and update path in -volumes
:
4)在以下位置打开docker-compose.yml
并更新路径-volumes
:
5) restart docker container
5)重启docker容器
回答by zhrist
Is this is services section? You do now need it in the volume section, so if you had it just remove it.
这是服务区吗?您现在在卷部分确实需要它,因此如果您拥有它,只需将其删除。