在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:46:15  来源:igfitidea点击:

Volume binding using docker compose on Windows

windowsdockerdocker-composedocker-machinedocker-toolbox

提问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

  1. Share nfs path using docker settings
  1. 使用 docker 设置共享 nfs 路径

enter image description here2. execute following command

在此处输入图片说明2.执行以下命令

docker run --rm -v c:/Users:/data alpine ls /data
  1. Set path in docker compose file as shown below enter image description here

  2. File copied to windows

  1. 在docker compose文件中设置路径如下图 在此处输入图片说明

  2. 文件复制到窗口

enter image description here

在此处输入图片说明

回答by Top-Master

It seems you are using an absolute path located inside C:\Usersdir, that didn't work for me too, if you are using Docker-Toolboxsee below.

看来您使用的是位于C:\Usersdir内的绝对路径,如果您使用,Docker-Toolbox请参见下文,这对我也不起作用。

Overview

概述

Forwarding the ./relative path in volumessection will automatically get resolved by docker-composeto the directory containing docker-compose.ymlfile (for example, if your project is in %UserProfile%/my-projectthen ./:/var/www/htmlgets /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/Usersdirectory gets shared with the Virtual-Machine (toolbox puts dockeritself 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.shscript:

所以,基本上把你的项目放在那里 ( 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.ymlfile.
  • 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:\Usersdir).
  • 将它的副本放在每个项目的docker-compose.yml文件旁边。
  • 每次系统打开时运行它(只需双击它或其快捷方式)。
  • 完毕!即使您的项目在另一个驱动器中(远离C:\Usersdir并且在dir之外),相对路径现在也应该可以工作。

Note:

笔记:

  • With a little edit, it should work without docker-composebeing required.
  • Consider running docker system pruneto free disk-space (or simply add docker system prune --forceto the above script, on a new line right after mountcommand).
  • 稍加编辑,它应该可以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.ymland 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.

这是服务区吗?您现在在卷部分确实需要它,因此如果您拥有它,只需将其删除。