简体   繁体   English

如何在 Docker for Windows 中实际绑定挂载文件

[英]How to actually bind mount a file in Docker for Windows

I'm attempting to update the sebp/elk Logstash configuration following the documentation here.我正在尝试按照此处的文档更新sebp/elk Logstash 配置。 I'm running into a situation in which the host file that I am attempting to mount is being mounted as a directory in the container.我遇到的情况是,我尝试挂载的主机文件被挂载为容器中的目录。

I found this related question How to mount a single file in a volume but the notion of running with PWD didn't work for me on Windows as I got the following error: C:\\Program Files\\Docker\\Docker\\Resources\\bin\\docker.exe: invalid reference format我发现了这个相关的问题How to mount a single file in a volume但在 Windows 上使用 PWD 运行的概念对我不起作用,因为我收到以下错误: C:\\Program Files\\Docker\\Docker\\Resources\\bin\\docker.exe: invalid reference format

I'm running Docker on Windows 10 (Build 16299.192)我在 Windows 10 上运行 Docker(内部版本 16299.192)

λ docker version
Client:
 Version:      17.09.1-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:22:26 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.09.1-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:28:28 2017
 OS/Arch:      linux/amd64
 Experimental: true

My Docker run command is:我的 Docker 运行命令是:

docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -it -vd:/docker/elk/logstash-snmp.conf:/etc/logstash/conf.d/logstash-snmp.conf --rm --name elk sebp/elk

I've been able to run other containers with persistent storage out of this disk (SQL Server, Redis, Exist-DB), but I'm not sure if I'm missing something on this.我已经能够从这个磁盘(SQL Server、Redis、Exist-DB)运行其他带有持久存储的容器,但我不确定我是否遗漏了一些东西。 How can I tell Docker to actually mount this as a file and not as a directory.我如何告诉 Docker 将它实际挂载为文件而不是目录。

This works for me:这对我有用:

Note : This approach addresses W10 Home Edition with Docker Toolbox and VirtualBox.注意:此方法使用 Docker Toolbox 和 VirtualBox 解决 W10 Home Edition。

Overview : Create a folder in local-machine, mount this as a shared folder in Docker VM, use this shared folder as a bindmount to Docker container.概述:在本地机器上创建一个文件夹,将其挂载为 Docker VM 中的共享文件夹,将此共享文件夹用作 Docker 容器的绑定。

  1. Stop docker VM using docker-machine stop default使用docker-machine stop default停止 docker VM
  2. Open VirtualBox, find default go to Settings > Shared Folder打开 VirtualBox,找到default Settings > Shared Folder
  3. You will see c/Users is binded to your c:\\Users您将看到c/Users已绑定到您的c:\\Users
  4. Add a new shared folder, note the name it is assigned.添加一个新的共享文件夹,记下为其分配的名称。 Let's name this as [local-shared]让我们将其命名为 [local-shared]
  5. Exit Settings退出Settings
  6. docker-machine start default
  7. Once started, docker-machine ssh default启动后, docker-machine ssh default
  8. sudo vi /mnt/sda1/var/lib/boot2docker/profile
  9. Append the following:附加以下内容:

     # create a directory in VM mkdir /home/docker/[foldername] # mount/map shared folder on localmachine to directory sudo moun -t vboxsf -o uid=1000,gid=50 [local-shared] /home/docker/[foldername]
  10. Save and exit ssh.保存并退出 ssh。

  11. docker-machine stop default
  12. docker-machine start default (or perhaps docker-machine restart default ) docker-machine start default (或者docker-machine restart default
  13. now docker container run -d -p 1234:6789 -v /[local-shared]/sub-dir:/[container-dir] dockerImage2Run现在docker container run -d -p 1234:6789 -v /[local-shared]/sub-dir:/[container-dir] dockerImage2Run

And it should work.它应该工作。

Ref: http://support.divio.com/local-development/docker/how-to-use-a-directory-outside-cusers-with-docker-toolbox-on-windowsdocker-for-windows参考: http : //support.divio.com/local-development/docker/how-to-use-a-directory-outside-cusers-with-docker-toolbox-on-windowsdocker-for-windows

If you are on windows, there is a problem in mapping the paths.如果您在 Windows 上,则映射路径会出现问题。 I am using Git bash terminal on windows and the command which works for me is in this format.我在 Windows 上使用 Git bash 终端,对我有用的命令就是这种格式。

  1. To copy a file named runtime_config.yml from host to container.将名为 runtime_config.yml 的文件从主机复制到容器。 The host path is given relative by using PWD as below.主机路径是使用 PWD 相对给出的,如下所示。 And the container path is from root.并且容器路径来自 root。
docker run -d -p 5000:5000 -v "/${PWD}/etc/runtime_config.yml":/demand_forecast/etc/runtime_config.yml ....
  1. To copy a directory named etc from host to container.将名为 etc 的目录从主机复制到容器。 The host path is given relative by using PWD as below.主机路径是使用 PWD 相对给出的,如下所示。 And the container path is from root.并且容器路径来自 root。
docker run -d -p 5000:5000 -v "/${PWD}/etc/":/demand_forecast/etc/ ....

Notice the / at the beginning in host path, that is very important, otherwise you will run into errors.注意主机路径开头的/,这很重要,否则你会遇到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM