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.

This is my code repository you can use it as an example for what I’m working with.

  • Coupling Compojure application with rethinkdb and docker-compose
  • I'm attempting to connect to MongoDB with a Clojure application, via Docker
  • Callback (or equivalent) when container w/ Rails app finishes booting?
  • Failed to find a usable hardware address from the network interfaces
  • Building a non-uberjar Docker image with leiningen
  • Vim-Fireplace in a Docker container
  • Note that the above repo relies on a library I’ve not yet published to Clojars (as I’m still testing it – hence this question opened). You can see the source code of the library here:

    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.

    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@
    Started server on port 8080
    Exception in thread "main" java.awt.HeadlessException:

    My code is currently using a -main function like so…

    (ns spurious-clojure-example.repl
      (:use spurious-clojure-example.handler
            [ring.middleware file-info file])
    (defonce server (atom nil))
    (defn get-handler []
      (-> #'app
        (wrap-file "resources")
    (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 []

    …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?

    I know that on the command line you can do lein ring server-headless so what’s the programmatic equivalent of that?

  • What are the benefits of using DEB for installation with Dockerfile
  • Oracle on Docker with pre-pumped Data
  • Docker Redis container getting WARNING: /proc/sys/net/core/somaxconn in a user defined network
  • Jenkins Docker Push to google cloud fails with an exception
  • Connecting to mongo docker container from host
  • Creating isolated Docker Zones
  • 2 Solutions collect form web for “How to get Clojure Compojure app to run headless via compiled jar within Docker container?”

    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.

    From the offical Docker docs on EXPOSE

    The EXPOSE instructions informs Docker that the container will listen
    on the specified network ports at runtime. Docker uses this
    information to interconnect containers using links (see the Docker
    User Guide) and to determine which ports to expose to the host when
    using the -P flag. Note: EXPOSE doesn’t define which ports can be
    exposed to the host or make ports accessible from the host by default.
    To expose ports to the host, at runtime, use the -p flag or the -P flag.

    So if you’re setting the ring server port manually in your project.clj, ensure that you’re using the same port also in your Dockerfile and then provide a mapping via -p or -P when starting docker.

    I can’t say for sure but I believe that your headless error message doesn’t contribute to your problem.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.