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

查看:30
本文介绍了使用 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 mount,但文件夹总是显示为空在 Docker 容器中.
  4. 我尝试使用 Unison 文件系统,该系统曾短暂地同步文件,但然后保持显示连接错误.
  5. 我启用了在 Jekyll 中投票,但这显着增加了我的更改被接受之前的延迟.
  6. 我尝试了 Dinghy,这是一个在 OS X 上使用 Vagrant 的更快、更友好的 Docker",并获得了 一些改进.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.

推荐答案

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

我发现在 OS X 上使用 Docker 设置高效开发环境的最佳解决方案是:Boot2Docker + Rsync.使用 rsync,Docker 容器中的构建时间与直接在 OSX 上运行构建时间相当!此外,文件观察器代码不需要轮询(inotify 可以工作,因为 rsync 使用普通文件夹),因此热重载几乎一样快.

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 虚拟机上安装 rsync:boot2docker ssh "tce-load -wi rsync".
  5. 在 Boot2Docker VM 上创建您需要的基本文件夹,并为它们正确设置权限.例如,如果您要从 OS X 同步 /foo/bar 文件夹,则需要在 Boot2Docker VM 上创建 /foo/bar:boot2dockerssh "mkdir -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 文档以了解您可能想要启用的各种设置,例如在同步时使用 --exclude .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天全站免登陆