What's the right way to setup a development en

2019-01-12 14:56发布

Intro

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. I can modify the code on OS X using the tools (text editor, IDE, git, etc) I already have installed.
  2. Those modifications are reflected in the Docker container so if I re-run tests or refresh a webpage, I can see my changes immediately.

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

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

Issue #1: Mounted volumes on Virtual Box (which use vboxsf) are extremely slow

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

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

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.

Workarounds I tried

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. 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.

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

Update: a solution at last!

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.

10条回答
做个烂人
2楼-- · 2019-01-12 15:42

I've been developing in a OS X (mid 2011 Macbook Air) + Boot2Docker + Docker-compose environment for a few weeks now. Haven't run into major performance issues but I avoid running any sort of build when developing (why not use something like jekyll serve --skip-initial-build?). Here's an example docker-compose.yml file I'm using:

docker-compose.yml:

test:
  build: .
  volumes:
    - ./client:/src/client
    - ./server:/src/server
    - ./test:/src/test
  command: nodemon --exec jasmine-node -- test/ --verbose --autotest --captureExceptions --color
  environment:
    - DEBUG=*

Dockerfile:

FROM node:0.12

RUN mkdir -p /src
WORKDIR /src

ENV PATH=/src/node_modules/.bin:$PATH

# We add package.json first so that we the
# image build can use the cache as long as the
# contents of package.json hasn't changed.

COPY package.json /src/
RUN npm install --unsafe-perm

COPY . /src

CMD [ "npm", "start" ]
EXPOSE 3000

I sometimes use NFS (http://syskall.com/using-boot2docker-using-nfs-instead-of-vboxsf/) but haven't noticed a big performance difference when doing so.

For me, the convenience of a simple docker-compose up test to get my environment running has been worth the cost in performance (I routinely work on multiple projects with different stacks).

PS: nodemon is one of the few file watchers which work with vboxsf (see https://github.com/remy/nodemon/issues/419).

查看更多
来,给爷笑一个
3楼-- · 2019-01-12 15:46

I feel you! I think I've tried pretty much everything you tried and unfortunately it was still slow. Then I came across this comment https://github.com/boot2docker/boot2docker/issues/64#issuecomment-70689254 that suggests using Vagrant and Parallels and instead of Virtualbox. This allowed me to use nfs and I did indeed saw a big performance boost for my project (Drupal).

Here's the Vagrant file. All you need to do is install vagrant, copy this in a file called Vagrantfile and put it in some folder. Go to that folder and just do a vagrant up instead of your normal boot2docker up.

Vagrant.configure(2) do |config|
  config.vm.box = "parallels/boot2docker"

  config.vm.network "forwarded_port", guest: 80, host: 80

  config.vm.synced_folder(
    "/Users/dicix/work/www", "/vagrant",
    type: 'nfs',
    nfs_udp: true,
    mount_options: %w[actimeo=2],
    bsd__nfs_options: %w[alldirs maproot=root:wheel]
  )
end
查看更多
闹够了就滚
4楼-- · 2019-01-12 15:51

Getting docker to work as a development tool is possible. But its going to hurt. I've documented the process here :

http://harmingcola.blogspot.com/2015/05/how-to-setup-docker-as-development-tool.html

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-12 15:52

Docker for Mac and Windows shall be the definitive way of developing with Docker on OS X (and Windows). A Docker product, the software is an “integrated, easy-to-deploy environment for building, assembling, and shipping applications from Mac or Windows.” It purports to be able to solve the issues presented by the OP. From its March 24, 2016 announcement:

  • Faster and more reliable: no more VirtualBox! The Docker engine is running in an Alpine Linux distribution on top of an xhyve Virtual Machine on Mac OS X or on a Hyper-V VM on Windows, and that VM is managed by the Docker application. You don’t need docker-machine to run Docker for Mac and Windows.
  • Tools integration: Docker for Mac is a Mac application and Docker for Windows is a Windows application, including a native user interface and auto-update capability. The Docker tool set comes bundled with it: Docker command line, Docker Compose, and Docker Notary command line.
  • Volume mounting for your code and data: volume data access works correctly, including file change notifications (on Mac inotify now works seamlessly inside containers for volume mounted directories). This enables edit/test cycles for “in container” development.
  • Easy access to running containers on the local host network: Docker for Mac and Windows include a DNS server for containers, and are integrated with the Mac OS X and Windows networking system. On a Mac, Docker can be used even when connected to a very restrictive corporate VPN.
  • Docker for Mac was architected from scratch to be able to fit the OS X sandbox security model and we are working closely with Apple to achieve this.
查看更多
登录 后发表回答