简体   繁体   English

在OSX上将卷装载到Docker镜像

[英]Mount volume to Docker image on OSX

On a Mac, how do you mount a volume to a Docker container? 在Mac上,如何将卷安装到Docker容器?

On my linux box, this is easy. 在我的linux机器上,这很容易。 All I need to do is something like -v /src/webapp:/opt/webapp when running the container. 在运行容器时,我需要做的就是-v /src/webapp:/opt/webapp But Mac is different since I have to run boot2docker to run a VM in VirtualBox. 但Mac不同,因为我必须运行boot2docker才能在VirtualBox中运行VM。 I've tried running 我试过跑步

boot2docker init
boot2docker up
boot2docker ssh # to poke around
boot2docker stop
VBoxManage sharedfolder add "boot2docker-vm" --name "Users" --hostpath /Users
boot2docker up
boot2docker ssh "sudo modprobe vboxsf"

but I get 但我明白了

modprobe: module vboxsf not found in modules.dep

If I ignore that and still try to mount on the VM like so 如果我忽略它并仍然尝试像这样挂载在VM上

boot2docker ssh "sudo mkdir /test && sudo mount -t vboxsf Users /test"

I get 我明白了

mount: mounting Users on /test failed: No such device

I feel like I'm missing something extremely simple, but I can't quite figure it out. 我觉得我错过了一些非常简单的东西,但我无法弄明白。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Ok, after digging through a GitHub PR , I was able to figure out a way to do this. 好的,在通过GitHub PR挖掘之后,我能够找到一种方法来做到这一点。 For the future readers out there, this process should be fixed in an upcoming release of boot2docker. 对于未来的读者,这个过程应该在即将发布的boot2docker版本中修复。

# assuming boot2docker and VirtualBox are installed
wget http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso
mv boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso ~/.boot2docker/boot2docker.iso
# blow away your old boot2docker-vm if it exists (boot2docker down && boot2docker destroy)
boot2docker init
boot2docker up
# set DOCKER_HOST as instructed
boot2docker stop
VBoxManage sharedfolder add boot2docker-vm --name /Users --hostpath /Users
boot2docker up
# if you ssh into the VM now, you'll notice /Users is present, but empty; I don't know/care why.
boot2docker ssh "sudo mount -t vboxsf -o uid=1000,gid=50 /Users /Users"
# done

This worked for me so I hope it works for others. 这对我有用,所以我希望它适用于其他人。 In the near future, I expect this issue to be solved by boot2docker, especially since the PR from which I got these commands was merged. 在不久的将来,我希望boot2docker可以解决这个问题,特别是因为我得到了这些命令的PR被合并了。

EDIT : boot2docker 1.3.0 supports this without any further changes. 编辑 :boot2docker 1.3.0支持此功能,无需进一步更改。 After updating, I ran these commands: 更新后,我运行了以下命令:

boot2docker destroy  # start over
boot2docker download # download the udpated ISO
boot2docker init
boot2docker up
# done

For folks finding this in the future who want to mount anything other than /Users, there's a script someone made as a gist on github that does the whole process for you and is awesome. 对于那些想要在/ Users之外安装任何东西的人来说,有一个人在github上作为要点制作的脚本,为你完成整个过程并且非常棒。 Just use this . 只需使用 It saved me a lot of headache of having to keep screwing around with virtualbox. 它让我不得不一直不停地使用virtualbox。 This is tested as of Docker 1.3.0 on my Mac running Yosemite. 这是在运行Yosemite的Mac上从Docker 1.3.0开始测试的。

EDIT: 编辑:

Now that docker-machine cli has been deprecated in favor or docker-machine, here's how you can do it with docker-machine: 现在docker-machine cli已经被弃用了,或者是docker-machine,这里有你如何用docker-machine做到这一点:

First, ssh into the docker-machine vm and create the folder we'll be mapping to: 首先,ssh进入docker-machine vm并创建我们要映射到的文件夹:

docker-machine ssh $MACHINE_NAME "sudo mkdir -p \"$VOL_DIR\""

Now share the folder to VirtualBox: 现在将文件夹共享到VirtualBox:

WORKDIR=$(basename "$VOL_DIR")
vboxmanage sharedfolder add "$MACHINE_NAME" --name "$WORKDIR" --hostpath "$VOL_DIR" --transient

Finally, ssh into the docker-machine again and mount the folder we just shared: 最后,再次进入docker-machine并挂载我们刚刚共享的文件夹:

docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""

Note: for UID and GID you can basically use whatever integers as long as they're not already taken. 注意:对于UID和GID,只要它们尚未被采用,您基本上可以使用任何整数。

This is tested as of docker-machine 0.4.1 and docker 1.8.3 on OS X El Capitan. 这是在OS X El Capitan上测试的docker-machine 0.4.1和docker 1.8.3。

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

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