在Docker容器中播放框架自动加载

我正在尝试build立一个在Docker容器中开发游戏应用程序的开发环境。 我已经创build了一个安装sbt的图像。 然后,将主机上的项目文件夹映射到容器,并以交互模式运行shell:

docker run -v /Users/jorgen/dev/play-sbt-docker/app:/data/app -w /data/app -p 9999:9000 -i -t jorgenfb/sbt /bin/bash 

然后我通过运行sbt ~run启动应用程序。 播放服务器启动只是发现,它甚至重新编译时,我在主机上编辑我的文件:

 [info] Compiling 1 Scala source to /data/app/target/scala-2.10/classes... [success] Compiled in 2s 

问题是刷新时浏览器中没有显示更改。 没有caching问题,因为我禁用了caching。 如果我从我的主机运行应用程序,一切工作正常。

编辑:这是我的Dockerfile用于创build容器与sbt:

 FROM dockerfile/java:oracle-java8 MAINTAINER Jørgen Borgesen ENV SBT_VERSION 0.13.5 # Install sbt RUN cd /tmp && \ wget https://dl.bintray.com/sbt/native-packages/sbt/$SBT_VERSION/sbt-$SBT_VERSION.zip && \ unzip sbt-$SBT_VERSION.zip -d /usr/local && \ rm sbt-$SBT_VERSION.zip 

我做了更多的研究。 在docker集装箱内,我开始播放应用程序,如下所示:

 [ root@aa1f2327d938:/data/app ]$ /usr/local/sbt/bin/sbt [info] Loading project definition from /data/app/project [info] Set current project to my-first-app (in build file:/data/app/) [my-first-app] $ ~run --- (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000 (Server started, use Ctrl+D to stop and go back to the console...) [success] Compiled in 740ms 

在我的浏览器中加载页面正常工作。 然后我改变我的索引文件在主机上。 这会在容器内触发重新编译:

 [info] Compiling 1 Scala source to /data/app/target/scala-2.10/classes... [success] Compiled in 1s 

刷新我的浏览器仍然显示最初的索引文件。 即使这些变化是由容器内的游戏应用程序所纠缠的。 我也检查了target/scala-2.10/classes/views/html的编译文件(在我的主机上,因为我正在容器中运行应用程序,我不知道如何连接多个terminal)。 编译的文件已经改变。

接下来的事情是按Ctrl-D。 这应该根据上面的打印信息“(服务器启动,使用Ctrl + D停止并返回到控制台…”)带我回到sbt控制台。 但是这会导致以下输出:

 [success] Total time: 455 s, completed Sep 25, 2014 7:40:35 AM 1. Waiting for source changes... (press enter to interrupt) --- (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000 (Server started, use Ctrl+D to stop and go back to the console...) [info] play - Application started (Dev) 

现在,我之前做的更改在刷新后反映在浏览器中。

我解决了这个问题(有点)。 这个问题不是特定于docker或play框架,而是与如何使用JNotify检测文件更改有关(play using this library)。 更改通过使用本机文件系统挂钩进行检测。 这些挂钩在虚拟机的共享文件夹中不可用(我在OSX上运行VM机器中的docker服务)。 这意味着自动检测文件更改的唯一方法是使用轮询策略。 Play版本2.3.2及更高版本支持Play框架。 要启用,添加到您的build.sbt:

 PlayKeys.playWatchService := play.sbtplugin.run.PlayWatchService.sbt(pollInterval.value) 

答案取自github上的post: Play 2.3.2自动重新加载在共享文件夹上不起作用

播放2.4更新:播放2.4重命名configuration参数。 这是如何在2.4中启用轮询:

 PlayKeys.fileWatchService := play.runsupport.FileWatchService.sbt(pollInterval.value) 

感谢philipphoffmann提供的最新信息。 添加到我的答案为2.3和2.4提供解决scheme。

更新:我刚刚发现了OSX用户的一个方便的工具: docker-osx-dev 。 它使用rsync来保持主机和虚拟文件系统的同步。 这将触发虚拟机上的文件系统更改。

对于玩2.4这将是:

 PlayKeys.fileWatchService := play.runsupport.FileWatchService.sbt(pollInterval.value)