[英]Volume binding using docker compose on Windows
I recently upgraded my Docker Toolbox on Windows 10, and now my volume mounts no longer work.我最近在 Windows 10 上升级了我的 Docker 工具箱,现在我的卷挂载不再起作用。 I've tried everything.我已经尝试了一切。 Here is the current mount path:这是当前的挂载路径:
volumes:
- C:\Users\Joey\Desktop\backend:/var/www/html
I receive an invalid bind mount error.我收到无效的绑定安装错误。
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
Use:采用:
volumes:
- "C:/Users/Joey/Desktop/backend:/var/www/html"
Putting the whole thing in double quotes and using forward slashes worked for me.将整个内容放在双引号中并使用正斜杠对我有用。 I was on windows 10 in windows 10 using Linux containers through WSL2我在 Windows 10 上通过 WSL2 使用 Linux 容器在 Windows 10 上
This answer was from Spenhouet given here .这个答案是从Spenhouet给出这里。
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): 3)打开终端并运行(由Docker Desktop提出):
docker run --rm -vc:/Users:/data alpine ls /data
4) Open your docker-compose.yml
and update path in -volumes
: 4) 打开你docker-compose.yml
并更新-volumes
路径:
volumes:
- /data/YOUR_USERNAME/projects/my_project/jssecacerts:/usr/lib/jvm/java-1.8-openjdk/jre/lib/security/jssecacerts/
5) restart docker container 5)重启docker容器
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
目录内的绝对路径,这对我也不起作用,如果您使用的是Docker-Toolbox
请参见下文。
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.yml
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
本身放在 VM 中,这意味着它无法访问您的文件系统,除了挂载共享-目录)。
So, basically placing your project there ( C:\\Users\\YOUR_USER_NAME
) should make ./
work.因此,基本上将您的项目放在那里( C:\\Users\\YOUR_USER_NAME
)应该使./
工作。 But not even that worked for me, and we ended up with below _prepare.sh
script:但即使这样对我也_prepare.sh
,我们最终得到了以下_prepare.sh
脚本:
#!/bin/bash
VBoxManage='/c/Program Files/Oracle/VirtualBox/VBoxManage'
# Defines variables for later use.
ROOT=$(dirname $0)
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:用法:
docker-compose.yml
file.将它的副本放在每个项目的docker-compose.yml
文件旁边。C:\\Users
dir).即使您的项目在另一个驱动器中(远离C:\\Users
目录,并且在C:\\Users
目录之外),相对路径现在也应该可以工作。Note:笔记:
docker-compose
being required.稍微编辑一下,它应该可以在不需要docker-compose
情况下工作。docker system prune
to free disk-space (or simply add docker system prune --force
to the above script, on a new line right after mount
command).考虑运行docker system prune
来释放磁盘空间(或者简单地将docker system prune --force
添加到上述脚本中,在mount
命令之后的新行中)。On windows 10, solved the problem with adding the last one /
at the end of host and mount path, like that:在 windows 10 上,通过在主机和挂载路径的末尾添加最后一个/
解决了问题,如下所示:
volumes:
- '/c/work/vcs/app/docker/i18n/:/usr/app/target/i18n/'
Without adding the last one /
mounted path contained some docker system folders and symlinks.没有添加最后一个/
安装路径包含一些 docker 系统文件夹和符号链接。
I solved it by replacing :
and '' in the windows path with /
at the first of the line.我通过将 windows 路径中的:
和 '' 替换为行首的/
来解决它。
to be like that:像那样:
volumes:
-/c/Users/Joey/Desktop/backend:/var/www/html
Please note: c
should be small.请注意: c
应该很小。
Is this is services section? 这是服务部分吗? You do now need it in the volume section, so if you had it just remove it. 你现在需要它在音量部分,所以如果你有它只是删除它。
If you're using the new Docker WSL2 backend, some drives may not be mounted in any WSL (and so Docker won't be able to see them either).如果您使用的是新的 Docker WSL2 后端,则某些驱动器可能未安装在任何 WSL 中(因此 Docker 也将无法看到它们)。 For example, D: or E: or usb drives.例如,D: 或 E: 或 USB 驱动器。 See看
To rule out this problem, try running docker-compose from a wsl
command line.要排除此问题,请尝试从wsl
命令行运行wsl
-compose。
This solution worked for me, in docker-compose.yml :这个解决方案对我有用,在 docker-compose.yml 中:
volumes:
- c/Users/Cyril/django:/mydjango
(Windows 10 with WSL2 and Docker Desktop) (带有 WSL2 和 Docker 桌面的 Windows 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.