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

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

问题描述

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

I can't figure out a good way to setup 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. 我可以使用工具修改OS X上的代码文本编辑器,IDE,git等)我已经安装了。

  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 mount < a>,但是Docker容器中的文件夹总是显示为空。

  4. 我尝试使用一致的文件系统,它可以简单地同步文件,但是保持< a href =https://github.com/leighmcculloch/docker-unison/issues/2 =nofollow noreferrer>显示连接错误。

  5. 我已启用在Jekyll投票,但这显着增加了延迟,直到我的变化被拾起。

  6. 我尝试过 dinghy ,一个更快,更友善的Docker on OS X与Vagrant,并获得了一些改进。而不是jekyll编译速度减慢了10-15倍,慢了2-3倍。有更好的,但仍然不太可用。

  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。我已经了解了如何在我自己的答案以及一个名为 docker-osx-dev

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.

推荐答案

我决定添加自己的回答我迄今为止发现的最佳解决方案。如果我找到更好的选择,我会更新这个。

I've decided to add my own answer with the best solution I've found so far. I'll update this if I find better options.

我在OS X上使用Docker设计一个高效的开发环境的最佳解决方案是: Boot2Docker + Rsync 。使用rsync,在Docker容器中的构建时间与在OSX上直接运行构建是一样的!此外,由于rsync使用普通文件夹,文件监视器代码不需要轮询( inotify ),所以热重新加载几乎 em>快。

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设置的所有步骤打包成一个名为泊坞窗-OSX-dev的。该代码有点粗糙,但是我已经成功使用它几个星期,可以轻松地在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 文件夹,则需要创建 / foo / bar boot2Docker VM: boot2docker sshmkdir -p / foo / bar&& chown -R docker / foo / bar

  6. 运行rsync将文件同步到Boot2Docker VM: rsync --archive --rsh =ssh -i $ HOME / .ssh / id_boot2docker -o StrictHostKeyChecking = no/ foo / bar docker @ dockerhost:/ foo 。检查rsync文档以了解可能要启用的各种设置,例如使用 - 排除.git 以排除 .git 文件夹同步。

  7. 使用文件观察器来保持文件同步。例如,您可以使用管道输入到rsync中的 fswatch brew install fswatch

  8. 此时,您应该可以使用 docker run 来启动Docker容器,并使用 -v 标志来挂载你要同步的文件夹: docker run -v / foo / bar:/ src some-docker-image

  9. 照常更新OS X上的代码。更改应该使用rsync非常快地传播,正常的文件监视器代码应该像往常一样接受更改(即使用 inotify ),并且构建应该运行得很快,因为所有的文件是容器中的本地。

  10. 如果您需要测试正在运行的网站,请运行 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天全站免登陆