2
votes

I am containerize my clojure project in docker. The container is successfully build and has both java 1.8.0_151 and lein 2.8.1 running on it. However when I try to start a ring server I get this exception. Yet when I run the same code in the mac environment everything runs normally.

exception:

$ docker-compose up
Starting clojurejournal_clojure_journal_1 ...
Starting clojurejournal_clojure_journal_1 ... done
Attaching to clojurejournal_clojure_journal_1
clojure_journal_1  | --> INFO: starting lein ring server
clojure_journal_1  | Nov 09, 2017 12:16:18 PM com.mchange.v2.log.MLog <clinit>
clojure_journal_1  | INFO: MLog clients using java 1.4+ standard logging.
clojure_journal_1  | 2017-11-09 12:16:20.027:INFO:oejs.Server:jetty-7.6.1.v20120215
clojure_journal_1  | 2017-11-09 12:16:20.084:INFO:oejs.AbstractConnector:Started [email protected]:3000
clojure_journal_1  | Started server on port 3000
clojure_journal_1  | Exception in thread "main" java.io.FileNotFoundException: http://localhost:3000, compiling:(/tmp/form-init462348242831821276.clj:1:71)
clojure_journal_1  |    at clojure.lang.Compiler.load(Compiler.java:7142)
clojure_journal_1  |    at clojure.lang.Compiler.loadFile(Compiler.java:7086)
clojure_journal_1  |    at clojure.main$load_script.invoke(main.clj:274)
clojure_journal_1  |    at clojure.main$init_opt.invoke(main.clj:279)
clojure_journal_1  |    at clojure.main$initialize.invoke(main.clj:307)
clojure_journal_1  |    at clojure.main$null_opt.invoke(main.clj:342)
clojure_journal_1  |    at clojure.main$main.doInvoke(main.clj:420)
clojure_journal_1  |    at clojure.lang.RestFn.invoke(RestFn.java:421)
clojure_journal_1  |    at clojure.lang.Var.invoke(Var.java:383)
clojure_journal_1  |    at clojure.lang.AFn.applyToHelper(AFn.java:156)
clojure_journal_1  |    at clojure.lang.Var.applyTo(Var.java:700)
clojure_journal_1  |    at clojure.main.main(main.java:37)
clojure_journal_1  | Caused by: java.io.FileNotFoundException: http://localhost:3000
clojure_journal_1  |    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
clojure_journal_1  |    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
clojure_journal_1  |    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
clojure_journal_1  |    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944)
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939)
clojure_journal_1  |    at java.security.AccessController.doPrivileged(Native Method)
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938)
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508)
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
clojure_journal_1  |    at javax.swing.JEditorPane.getStream(JEditorPane.java:768)
clojure_journal_1  |    at javax.swing.JEditorPane.setPage(JEditorPane.java:432)
clojure_journal_1  |    at javax.swing.JEditorPane.setPage(JEditorPane.java:880)
clojure_journal_1  |    at javax.swing.JEditorPane.<init>(JEditorPane.java:274)
clojure_journal_1  |    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
clojure_journal_1  |    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
clojure_journal_1  |    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
clojure_journal_1  |    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
clojure_journal_1  |    at clojure.lang.Reflector.invokeConstructor(Reflector.java:180)
clojure_journal_1  |    at clojure.java.browse_ui$open_url_in_swing.invoke(browse_ui.clj:15)
clojure_journal_1  |    at clojure.lang.Var.invoke(Var.java:379)
clojure_journal_1  |    at clojure.java.browse$open_url_in_swing.invoke(browse.clj:64)
clojure_journal_1  |    at clojure.java.browse$browse_url.invoke(browse.clj:76)
clojure_journal_1  |    at ring.server.standalone$open_browser_to.invoke(standalone.clj:39)
clojure_journal_1  |    at ring.server.standalone$serve$fn__1676.invoke(standalone.clj:102)
clojure_journal_1  |    at ring.server.standalone$try_port.invoke(standalone.clj:16)
clojure_journal_1  |    at ring.server.standalone$serve.doInvoke(standalone.clj:95)
clojure_journal_1  |    at clojure.lang.RestFn.invoke(RestFn.java:423)
clojure_journal_1  |    at ring.server.leiningen$serve.invoke(leiningen.clj:20)
clojure_journal_1  |    at user$eval3389.invoke(form-init462348242831821276.clj:1)
clojure_journal_1  |    at clojure.lang.Compiler.eval(Compiler.java:6703)
clojure_journal_1  |    at clojure.lang.Compiler.eval(Compiler.java:6693)
clojure_journal_1  |    at clojure.lang.Compiler.load(Compiler.java:7130)
clojure_journal_1  |    ... 11 more
clojure_journal_1  | Caused by: java.io.FileNotFoundException: http://localhost:3000
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1890)
clojure_journal_1  |    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
clojure_journal_1  |    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
clojure_journal_1  |    at javax.swing.JEditorPane.getStream(JEditorPane.java:733)
clojure_journal_1  |    ... 33 more

dockerfile:

FROM ubuntu:xenial

LABEL maintainer=freid
ENV WORK_DIR /var/www

# install dependancies
RUN apt-get update
RUN apt-get install -y default-jre curl wget

RUN wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
RUN chmod +x lein
RUN mv lein /usr/local/bin

# copy to /var/www
COPY . $WORK_DIR

# make it a working directory
WORKDIR $WORK_DIR

# fix permissions
RUN chmod a+x /var/www/entrypoint.sh

# expose port 3000
EXPOSE 3000

# make entrypoint
ENTRYPOINT ["/bin/sh", "/var/www/entrypoint.sh"]

docker-compose:

clojure_journal:
  image: clojure_journal:latest
  command: run
  ports:
    - 3000:3000
  volumes:
    - .:/opt/clojure_journal

entrypoint.sh

#!/usr/bin/env bash

start(){
    echo "--> INFO: starting lein ring server"
    lein ring server
}

case $1 in

run)
    shift 1
    start $@
;;

*)
   >&2 echo "---> INFO: running: '$1'."
;;
esac
2
Looks like your code is trying to open a browser to localhost:3000 during startup, from within the container, which is probably not what you want? I assume you just want to run the HTTP server from the container. Try lein ring server-headless in your entrypoint.sh script, which will prevent it from trying to open a browser. Also, it's possible to just lein uberjar your project and copy the standalone JAR into your container and run that with java.Taylor Wood

2 Answers

2
votes

As per @Taylor Wood 's comment using lein ring server-headless resolved the issue.

1
votes

Looking at the exception stack trace, it looks like your code is trying to open a browser to http://localhost:3000 during startup, which is probably not what you want from within the container. I assume you only want to run the HTTP server from the container. Try lein ring server-headless in your entrypoint.sh script, which will prevent it from trying to open a browser

As an aside, it's also possible (and easy) to lein uberjar your project and copy the standalone JAR into your container and run that with java, assuming you're not using the Docker image as a dev environment. That way you don't need to have Leiningen installed, which means you can just use an OpenJDK image and your Dockerfile could be much simpler:

FROM java:8-alpine    
ADD path/to/your.jar /your-app/app.jar
EXPOSE 3000 
CMD ["java", "-jar", "/your-app/app.jar"]