Docker中的共享文件夹。与Windows。不仅“C /用户/”路径 [英] Shared folder in Docker. With Windows. Not only "C/user/" path

查看:408
本文介绍了Docker中的共享文件夹。与Windows。不仅“C /用户/”路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新人,来自Vagrant。



我在我的D:/ Works / something / DockerFirstTime文件夹。



现在我用



docker-machine创建机器第一个



和简单的 Dockerfile



FROM ruby​​:2.2-onbuild



和简单的 Gemfile

 源'https://rubygems.org'
gem'rails'

现在使用这个命令,我想使用一个共享文件夹,就像我在Dockerfile的同一个硬盘驱动器中的Vagrant一​​样:



#p $ docker run -it -v // d / Works / something / DockerFirstTime:/ usr / src / app -w / usr / src / app ruby​​:2.2 bundle install



但它不起作用。



如何做?



我知道Docker只分享了 / c / User / folder ,是吗?



我使用的文件夹用文件修改我的文件,并在Windows中使用编辑器修改我的文件,然后重新启动服务器,就像在一台PC上的普通shell一样,或者像Vagrant一​​样。

解决方案

这个问题和这个问题有类似的根问题,在boot2docker中安装一个非C:/ drive文件夹。我为另一个问题写了一个深入的答案,提供了与VonC答案的上半部分相同的信息。



Docker文档


所有其他路径都来自您的虚拟机的文件系统。 [...]在
的VirtualBox的情况下,您需要使主机文件夹可用作VirtualBox中的
共享文件夹。然后,您可以使用Docker
-v 标志来装载它。






这将挂载您的整个D:\驱动器,您可以简单地将文件路径更改为更细化和具体的



使用VBox共享目录:



只需要完成一次。



在Windows CMD:

  VBoxManage共享文件夹添加boot2docker-vm--name d-share--hostpathD:\



在VM中挂载共享目录:



每次重新启动虚拟机时,都需要完成此操作。



在Boot2Docker VM终端中: / p>

  mount -t vboxsf -o uid = 1000,gid = 50 d-share / d 

要查看这些工作原理的来源和说明,请参阅我对其他类似问题的完整回答



此后,您可以使用在Docker中的-v / - volume 标志将此文件夹或任何子文件夹或文件装载到容器中。如果你安装了你的整个D:\驱动器,你可以使用你的问题的确切的docker运行命令,它现在应该可以工作。如果你挂载了你的驱动器的一部分,你将不得不改变匹配的路径。



要在Windows中编辑,在docker中运行:



另外从 Docker文档


安装主机目录可用于测试。例如,
可以将源代码装载到容器中。然后,更改源代码
并实时查看其对应用程序的影响。


作为一个VBox共享目录应该能够看到从Windows端所做的更改反映在boot2docker vm中。



您可能需要重新启动容器才能看到实际显示的更改,这取决于程序在容器内运行的程序,在您的情况下,ruby使用这些文件。如果文件在容器启动时被编译成应用程序,例如,您一定需要重新启动容器才能看到更改。



注意:



在Windows中编写文件并在Linux中阅读文本时,请注意CR LF与LF行的最终差异。确保您的文本编辑器正在使用Unix行结尾保存文件,否则您可能会开始看到由^ M引起的错误附加到所有行的末尾。


I'm new to Docker, I come from Vagrant.

I'm using Docker (1.9.1) inside my "D:/Works/something/DockerFirstTime" folder.

Now I create the machine with

docker-machine create first

and simple Dockerfile:

FROM ruby:2.2-onbuild

and simple Gemfile:

source 'https://rubygems.org'
gem 'rails'

Now with this command I want to use a shared folder like in Vagrant in the same hard drive of my Dockerfile:

docker run -it -v //d/Works/something/DockerFirstTime:/usr/src/app -w /usr/src/app ruby:2.2 bundle install

But it doesn't works.

How to do this?

I know that Docker only shares the /c/User/folder, is that right?

How can I use the folder with the files and modify my files with editor in Windows and then restart server like in a normal shell on a single PC or like in Vagrant?

解决方案

This question and this question have a similar root problem, mounting a non C:/ drive folder in boot2docker. I wrote an in-depth answer to the other question that provide the same information that is in the first half of @VonC's answer.

From Docker Docs:

All other paths come from your virtual machine’s filesystem. [...] In the case of VirtualBox you need to make the host folder available as a shared folder in VirtualBox. Then, you can mount it using the Docker -v flag.

To get your folder mounted in a container:

This mounts your entire D:\ drive, you can simply change the file paths to be more granular and specific.

Share the directory with VBox:

This only needs to be done once.

In windows CMD:

VBoxManage sharedfolder add "boot2docker-vm" --name "d-share" --hostpath "D:\"

Mount the shared directory in your VM:

This will need to be done each time you restart the VM.

In the Boot2Docker VM terminal:

mount -t vboxsf -o uid=1000,gid=50 d-share /d

To see sources and explanation for how this works see my full answer to the other similar question

After this you can use the -v/--volume flag in Docker to mount this folder or any sub-folders or files into containers. If you mounted your whole D:\ drive you can use that exact docker run command from your question and it should now work. If you mounted a specific part of you drive you will have to change the paths to match.

To edit in windows, run in docker:

Also from Docker Docs:

Mounting a host directory can be useful for testing. For example, you can mount source code inside a container. Then, change the source code and see its effect on the application in real time.

As a VBox shared directory you should be able to see changes made from the Windows side reflected in the boot2docker vm.

You may need to restart containers to see the changes actually appear, this depends on how the program running inside the container, in your case ruby, uses the files. If the files are compiled into an app when the container starts, for example, you will definitely need to restart the container to see the changes.

Note:

Beware the CR LF vs. LF line ending difference when writing files in Windows and reading them in Linux. Make sure your text editor is saving files with Unix line endings or else you may start to see errors caused by '^M' appended to the end of all your lines.

这篇关于Docker中的共享文件夹。与Windows。不仅“C /用户/”路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆