如何让Clojure Compojure应用程序通过编译的jar在Docker容器中运行无头? [英] How to get Clojure Compojure app to run headless via compiled jar within Docker container?
问题描述
更新:此问题自原始评论者留下回应后已更改。对于任何混乱,表示抱歉。
Update: this question has changed since the original set of commenters left responses. Apologies for any confusion.
这是我的代码库 https://github.com/Integralist/spurious-clojure-example 您可以使用它作为我正在使用的示例。
This is my code repository https://github.com/Integralist/spurious-clojure-example you can use it as an example for what I'm working with.
请注意,上面的repo依赖于我还没有发布到Clojars的库(因为我仍在测试它 - 因此这个问题打开) 。您可以在此处查看库的源代码: https://github.com/ Integralist / spurious-clojure-aws-sdk-helper
我有一个hello worldClojure Compojure,我使用 lein环服务器
和 lein run
(因为我有一个 -main
function now created)。它也在一定程度上运行到编译成一个jar,我运行 java -jar app.jar
。
I have a "hello world" Clojure web app written with Compojure that I have working fine when run using lein ring server
and lein run
(as I have a -main
function now created). It also runs to a certain extent when compiled down into a jar and I run java -jar app.jar
.
我现在的问题是,如果我试图在Docker中运行默认的 java -jar app.jar
容器我得到以下错误告诉我...
My problem now is that if I try to run the default java -jar app.jar
from within a Docker container I get the following error telling me...
spurious-clojure-example is starting
2015-02-14 00:58:03.812:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2015-02-14 00:58:03.854:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
Started server on port 8080
Exception in thread "main" java.awt.HeadlessException:
我的代码目前正在使用 -main
函数...
My code is currently using a -main
function like so...
(ns spurious-clojure-example.repl
(:use spurious-clojure-example.handler
ring.server.standalone
[ring.middleware file-info file])
(:gen-class))
(defonce server (atom nil))
(defn get-handler []
(-> #'app
(wrap-file "resources")
(wrap-file-info)))
(defn start-server
"used for starting the server in development mode from REPL"
[& [port]]
(let [port (if port (Integer/parseInt port) 8080)]
(reset! server
(serve (get-handler)
{:port port
:init init
:auto-reload? true
:destroy destroy
:join true}))
(println (str "You can view the site at http://localhost:" port))))
(defn stop-server []
(.stop @server)
(reset! server nil))
(defn -main []
(start-server))
...但是如何让服务器开始无头?我不能完全按照Compojure样板代码来解释在哪里或如何知道何时运行无头或通过浏览器?
...but how do I get the server to start headless? I can't quite follow the Compojure boilerplate code to decipher where or how it knows when to run headlessly or via browser?
我知道在命令行上 lein ring server-headless
所以什么是程序化的呢?
I know that on the command line you can do lein ring server-headless
so what's the programmatic equivalent of that?
推荐答案
因为环网服务器主要是用于开发,它尝试在服务器启动时打开浏览器。在没有GUI的平台上,这会失败并显示 java.awt.HeadlessException
。您需要将:open-browser?
选项设置为false以防止出现此情况。
Because ring-server is primarily meant for development, it tries to open a browser when the server starts. This fails with a java.awt.HeadlessException
on platforms without a GUI. You'll want to set the :open-browser?
option to false to prevent this.
这篇关于如何让Clojure Compojure应用程序通过编译的jar在Docker容器中运行无头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!