用Docker在OS X上设置开发环境的正确方法是什么? [英] What's the right way to set up a development environment on OS X with Docker?

查看:73
本文介绍了用Docker在OS X上设置开发环境的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不出一种使用Docker和Boot2Docker在OS X上设置开发环境的好方法.我遇到的问题是如何管理源代码,以便:

I can't figure out a good way to set up a development environment on OS X using Docker and Boot2Docker. The problem I'm hitting is how to manage the source code so that:

  1. 我可以使用已经安装的工具(文本编辑器,IDE,git等)在OS X上修改代码.
  2. 这些修改反映在Docker容器中,因此,如果我重新运行测试或刷新网页,我可以立即看到我的更改.

从理论上讲,通过将我的源代码安装为一个卷即可轻松做到这一点:

In theory, this should be easy to do by mounting my source code as a volume:

docker run -it -v /path/to/my/source/code:/src some-docker-image

不幸的是,这有两个主要问题,使其在OS X上完全无法使用:

Unfortunately, this has two major issues that make it completely unusable on OS X:

例如,这是Jekyll编译我的首页所花费的时间源代码是Docker映像的一部分:

For example, here is how long it takes Jekyll to compile my homepage if the source code is part of the Docker image:

> docker run -it brikis98/yevgeniy-brikman-homepage:v1 bash

root@7aaea30d98a1:/src# time bundle exec jekyll build

[...]

real    0m7.879s
user    0m7.360s
sys     0m0.600s

这里是完全相同的Docker映像,除了这次,我从OS X挂载了源代码:

Here is the exact same Docker image, except this time, I mount the source code from OS X:

> docker run -it -v $(pwd):/src brikis98/yevgeniy-brikman-homepage:v1 bash

root@1521b0b4ce6a:/src# time bundle exec jekyll build

[...]

real    1m14.701s
user    0m9.450s
sys     0m3.410s

问题#2:文件查看功能已损坏

SBT,Jekyll和grunt中的默认监视机制使用诸如inotify的技术,如果它们在Docker容器中运行并且在OS X中对已安装的文件夹进行了更改,则这些技术将不起作用.

Issue #2: File watching is broken

The default watch mechanisms in SBT, Jekyll, and grunt use technologies such as inotify, which do not work if they are running in a Docker container and the changes are made in OS X to a mounted folder.

我搜索了解决方案(包括SO上的所有解决方案),并尝试了其中的一些解决方案,但没有找到成功的解决方案:

I searched for solutions (including all the ones on SO) and tried out a few of them, but have not found a successful one:

  1. 切换了Boot2Docker以使用NFS ,但是速度同样慢.
  2. 我尝试了 Vagrant + NFS ,而且速度也一样慢.
  3. 我尝试了 Samba安装,但是文件夹始终显示为空在Docker容器中.
  4. 我尝试使用 Unison文件系统,该文件系统可以短暂地同步文件,但是然后显示连接错误.
  5. 我启用了在Jekyll中进行轮询,但这大大增加了延迟,直到我的更改被拿走为止.
  6. 我尝试了 Dinghy ,使用Vagrant的OS X上更快,更友好的Docker",得到了一些改进. Jekyll编译的速度要慢2-3倍,而不是Jekyll的慢10-15倍.更好,但仍然不太实用.
  1. I switched Boot2Docker to use NFS, but it was just as slow.
  2. I tried Vagrant + NFS, and that was also just as slow.
  3. I tried a Samba mount, but the folder always showed up empty in the Docker container.
  4. I tried to use the Unison file system, which worked briefly to sync files, but then kept showing connection errors.
  5. I enabled polling in Jekyll, but that significantly increased the delay until my changes were picked up.
  6. I tried Dinghy, a "faster, friendlier Docker on OS X with Vagrant" and got some improvement. Instead of Jekyll compilation being 10-15x slower, it was 2-3x slower. That's better, but still not quite usable.

有没有人找到一个切实可行的解决方案,并允许您使用Docker和OS X高效地开发代码?

Has anyone found a solution that actually works and allows you to productively develop code with Docker and OS X?

我终于找到了一个使用Boot2Docker + rsync似乎很有成效的解决方案.我已经在

I have finally found a solution that seems productive using Boot2Docker + rsync. I've captured the details on how to set this up in my own answer as well as an open-source project called docker-osx-dev.

推荐答案

我已决定添加自己的答案,并添加迄今为止我发现的最佳解决方案.如果有更好的选择,我会进行更新.

我发现在OS X上使用Docker设置生产性开发环境的最佳解决方案是: Boot2Docker + Rsync .使用rsync,Docker容器中的构建时间与直接在OSX上运行构建相当!而且,文件监视程序代码不需要不需要轮询(由于rsync使用普通文件夹,因此inotify可以工作),因此热重新加载的速度几乎与 一样快.

The best solution I've found for setting up a productive development environment with Docker on OS X is: Boot2Docker + Rsync. With rsync, build times in a Docker container are on par with running the build directly on OSX! Moreover, the file watcher code does not need polling (inotify works since rsync uses normal folders), so hot reload is almost as fast.

有两种设置方法:自动安装和手动安装.

There are two ways to set it up: an automated install and a manual install.

我已将使用Rsync设置Boot2Docker的所有步骤打包到一个名为 docker-osx-dev <的开源项目中/a>.代码有点粗糙,但是我已经成功使用了几周,可以轻松地在具有3个不同技术堆栈的3个项目之间切换.试试看,报告错误,并提交一些PR!另外,请参阅我的博客文章在OS X上使用Docker的高效开发环境了解更多信息.

I've packaged all the steps for setting up Boot2Docker with Rsync into an open source project called docker-osx-dev. The code is a bit rough, but I've been successfully using it for several weeks to easily switch between 3 projects with 3 different tech stacks. Try it out, report bugs, and submit some PRs! Also, see my blog post, A productive development environment with Docker on OS X for more info.

  1. 安装 Boot2Docker :brew install boot2docker.
  2. 运行Boot2Docker,但禁用VirtualBox共享文件夹:boot2docker init && boot2docker start --vbox-share=disable.
  3. 运行boot2docker shellinit并将其打印出的环境变量复制到您的~/.bash_profile文件中.
  4. 在Boot2Docker VM上安装rsync:boot2docker ssh "tce-load -wi rsync".
  5. 在Boot2Docker VM上创建所需的基本文件夹,并为其正确设置权限.例如,如果要从OS X同步/foo/bar文件夹,则需要在Boot2Docker VM:boot2docker ssh "mkdir -p /foo/bar && chown -R docker /foo/bar"上创建/foo/bar.
  6. 运行rsync将文件同步到Boot2Docker VM:rsync --archive --rsh="ssh -i $HOME/.ssh/id_boot2docker -o StrictHostKeyChecking=no" /foo/bar docker@dockerhost:/foo.在rsync文档中检查您可能要启用的各种设置,例如在同步时使用--exclude .git排除.git文件夹.
  7. 使用文件监视程序使文件保持同步.例如,您可以使用通过管道传送到rsync的 fswatch (brew install fswatch)
  8. .
  9. 这时,您应该能够使用docker run启动Docker容器,并使用-v标志安装要同步的文件夹:docker run -v /foo/bar:/src some-docker-image.
  10. 照常更新OS X上的代码.更改应使用rsync迅速传播,常规文件监视程序代码应照常进行更改(即,使用inotify),并且构建应运行得很快,因为所有文件都是本地"到容器的.
  11. 如果您需要测试正在运行的网站,请运行boot2docker ip命令以查找其所使用的IP.
  1. Install Boot2Docker: brew install boot2docker.
  2. Run Boot2Docker, but with VirtualBox shared folders disabled: boot2docker init && boot2docker start --vbox-share=disable.
  3. Run boot2docker shellinit and copy the environment variables it prints out into your ~/.bash_profile file.
  4. Install rsync on the Boot2Docker VM: boot2docker ssh "tce-load -wi rsync".
  5. Create the base folders you need on the Boot2Docker VM and set permissions correctly for them. For example, if you'll be syncing the /foo/bar folder from OS X, you need to create /foo/bar on the Boot2Docker VM: boot2docker ssh "mkdir -p /foo/bar && chown -R docker /foo/bar".
  6. Run rsync to sync the files to the Boot2Docker VM: rsync --archive --rsh="ssh -i $HOME/.ssh/id_boot2docker -o StrictHostKeyChecking=no" /foo/bar docker@dockerhost:/foo. Check the rsync docs for various settings you may want to enable, such as using --exclude .git to exclude the .git folder when syncing.
  7. Use a file watcher to keep files in sync. For example, you could use fswatch (brew install fswatch) piped into rsync.
  8. At this point, you should be able to use docker run to fire up your Docker container and use the -v flag to mount the folder you're syncing: docker run -v /foo/bar:/src some-docker-image.
  9. Update the code on OS X as usual. Changes should propagate very quickly using rsync, the normal file watcher code should pick up the changes as usual (ie, using inotify), and the build should run fast because all the files are "local" to the container.
  10. If you need to test a running website, run the boot2docker ip command to find out what IP it's on.

这篇关于用Docker在OS X上设置开发环境的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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