在Docker容器中播放框架自动加载 [英] Play framework auto-loading in docker container

查看:122
本文介绍了在Docker容器中播放框架自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个开发环境,用于在码头容器中开发一个播放应用程序。我已经安装了sbt创建了一个图像。然后,我将主机上的项目文件夹作为卷映射到容器,并以交互模式运行shell:

I'm trying to set up a development environment for developing a play application in a docker container. I have created an image with sbt installed. I then map the project folder on my host to the container as a volume and run shell in interactive mode:

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 启动播放应用程序。播放服务器启动时只是找到,甚至在我编辑主机文件时重新编译:

I then starts the play application by running sbt ~run. The play server starts just find, it even recompiles when i edit my files on the host:

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

问题是刷新时更改不会出现在浏览器中。没有缓存问题,因为我已禁用缓存。如果我从我的主机运行应用程序,一切都正常。

The problem is that the changes does not appear in the browser when I refresh. There is no caching issue since I have disabled caching. If I run the application from my host, everything works fine.

编辑:
这是我的Dockerfile,用于创建带有sbt的容器:

This is my Dockerfile used to create the container with 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容器内,我开始播放应用程序:

I did some more research. Inside the docker container i start the play application like this:

[ 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

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

Loading the page in my browser works fine. I then change my index file on the host. This triggers recompile inside the container:

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

刷新浏览器仍显示初始索引文件。即使更改由容器内的播放应用程序纠正。我还检查了 target / scala-2.10 / classes / views / html 中的编译文件(在我的主机上,由于我在容器中运行播放应用程序, '不知道如何连接多个终端)。编译文件已更改。

Refreshing my browser still shows the initial index file. Even if the changes was picket up by the play application inside the container. I have also inspected the compiled files in target/scala-2.10/classes/views/html (on my host, since I'm running the play application in the container and I'm not sure how to attach multiple terminals to it). The compiled files has changed.

接下来我所做的是按Ctrl-D。这应该按照上面打印的消息(服务器启动,使用Ctrl + D停止并返回到控制台...)返回到sbt控制台。但是,这会产生以下输出:

The next thing I did was pressing Ctrl-D. This should take my back to the sbt console according the printed message above "(Server started, use Ctrl+D to stop and go back to the console...)". However this results in the following output:

[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)

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

Now the changes I made earlier are reflected in the browser after a refresh.

推荐答案

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

I solved the problem (sort of). The issue is not specific to either docker or play framework, but is related to how changes to files are detected using JNotify (play uses this library). Changes are detected by using native file system hooks. These hooks are not available in shared folders for virtual machines (I run the docker service in a VM machine since I'm on OSX). This means that the only way of automatically detecting file changes is to use a polling strategy. Play framework supports in version 2.3.2 and later. To enable, add this to your build.sbt:

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

答案来自github上的问题帖子:播放2.3.2自动重新加载无法在共享文件夹中运行

The answer is taken from an issue post on github: Play 2.3.2 auto reload is not working on shared folder

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

Update for play 2.4: Play 2.4 renames the config parameter. This is how to enable polling in 2.4:

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

感谢philipphoffmann的答案与更新的信息。将其添加到我的答案中,为2.3和2.4提供解决方案。

Thanks to philipphoffmann for his answer with the updated info. Added it to my answer to provide a solution for both 2.3 and 2.4.

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

Update: I just discovered a handy tool for OSX users: docker-osx-dev. It uses rsync to keep the host and virtual file systems in sync. This will trigger file system change on your virtual machine.

这篇关于在Docker容器中播放框架自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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