Docker 中的共享文件夹.带窗户.不仅是“C/user/"小路 [英] Shared folder in Docker. With Windows. Not only "C/user/" path

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

问题描述

我是 Docker 的新手,我来自 Vagrant.

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

我在D:/Works/something/DockerFirstTime"文件夹中使用 Docker (1.9.1).

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

现在我用

docker-machine 先创建

和简单的Dockerfile:

来自 ruby​​:2.2-onbuild

和简单的Gemfile:

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

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

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

但它不起作用.

如何做到这一点?

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

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

如何使用包含文件的文件夹并在 Windows 中使用编辑器修改我的文件,然后像在单个 PC 上的普通 shell 中或在 Vagrant 中一样重新启动服务器?

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?

推荐答案

这个问题和 这个问题 有一个类似的根本问题,在 boot2docker 中安装一个非 C:/驱动器文件夹.我为另一个问题写了一篇深入的回答,其中提供的信息与 @VonC 回答的前半部分相同.

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.

来自 Docker 文档:

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

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.

要将您的文件夹安装在容器中:

这会挂载您的整个 D: 驱动器,您只需将文件路径更改为更细化和更具体.

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.

这只需要做一次.

在 Windows CMD 中:

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.

在 Boot2Docker 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

此后,您可以使用 Docker 中的 -v/--volume 标志将此文件夹或任何子文件夹或文件装入容器.如果您安装了整个 D: 驱动器,您可以使用问题中的确切 docker run 命令,它现在应该可以工作了.如果您安装了驱动器的特定部分,则必须更改路径以匹配.

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.

也来自 Docker文档:

挂载主机目录对于测试很有用.例如,你可以在容器内挂载源代码.然后,更改源代码并实时查看其对应用程序的影响.

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.

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

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

您可能需要重新启动容器才能看到实际出现的更改,这取决于容器内运行的程序(在您的情况下为 ruby​​)使用文件的方式.例如,如果在容器启动时将文件编译为应用程序,则您肯定需要重新启动容器才能看到更改.

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.

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

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 中的共享文件夹.带窗户.不仅是“C/user/"小路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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