Vert.x Web app throws IllegalStateExceptions when run in a Docker container

313 Views Asked by At

I have a Vert.x Web application that acts as a simple web server. The start() method of the verticle is below:

 @Override
 public void start() throws Exception 
 {
    HttpServerOptions options = new HttpServerOptions();
  
    options.setMaxHeaderSize(6000);
    options.setMaxInitialLineLength(100000);
    options.setTcpKeepAlive(true);
  
    HttpServer server = vertx.createHttpServer(options);
  
    Router router = Router.router(vertx);
  
    router.route(HttpMethod.GET,"/*").handler(StaticHandler.create(".").setIndexPage("index.html"));
  
    server.requestHandler(router);
  
    server.listen(8099,result->{
       if(result.succeeded())
          log.info("Main Server has started! Listening on Port "+server.actualPort());
       else if(result.failed())
          log.error("Main Server start FAILED. Reason: "+result.cause());
    });
 }

The service works well, displaying the static content that I have without problems -- until I put it into a Docker container.

When I run the application as a Java jar file:

java -jar webserv-1.0.0.jar

and run a browser with the following URL:

http://localhost:8099

the index page is displayed without problems and any links to other pages are also displayed.

But then, I take the JAR file and place it into a Docker container (after stopping the server, of course!) using the following dockerfile:

FROM centos:7

RUN yum install -y \
   java-11-openjdk \
   java-11-openjdk-devel

RUN mkdir /home/factor3
ENV JAVA_HOME /usr/lib/jvm/java-1.11.0-openjdk/

RUN groupadd apprun && adduser factor3 -G appgroup
RUN chown factor3 /home/factor3

EXPOSE 8099

USER factor3

COPY ./app/webserv-1.0.0.jar /home/factor3
COPY app/asets /home/factor3

WORKDIR /home/factor3
ENTRYPOINT ["java","-jar","webserv-1.0.0.jar"]

Once the image (called webserv:1.0.0) is created, I run the application in docker:

sudo docker run -p 8099:8099 --name webpages webserv:1.0.0

and use the same URL on the same browser:

http://localhost:8099

I get the following failure:

java.lang.IllegalStateException: Response head already sent
    at io.vertx.core.http.impl.Http1xServerResponse.checkHeadWritten(Http1xServerResponse.java:675)
    at io.vertx.core.http.impl.Http1xServerResponse.setStatusCode(Http1xServerResponse.java:144)
    at io.vertx.ext.web.impl.RoutingContextImplBase.unhandledFailure(RoutingContextImplBase.java:217)
    at io.vertx.ext.web.impl.RoutingContextImpl.checkHandleNoMatch(RoutingContextImpl.java:142)
    at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:134)
    at io.vertx.ext.web.impl.RoutingContextImpl.doFail(RoutingContextImpl.java:591)
    at io.vertx.ext.web.impl.RoutingContextImpl.fail(RoutingContextImpl.java:184)
    at io.vertx.ext.web.impl.RoutingContextImpl.fail(RoutingContextImpl.java:173)
    at io.vertx.ext.web.handler.impl.StaticHandlerImpl.lambda$sendFile$6(StaticHandlerImpl.java:438)
    at io.vertx.core.http.impl.Http1xServerResponse.lambda$doSendFile$1(Http1xServerResponse.java:559)
    at io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:100)
    at io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:63)
    at io.vertx.core.impl.EventLoopContext.lambda$runOnContext$0(EventLoopContext.java:38)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:497)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:829)

This failure occurs consistently.

Have I found a bug in Vert.x's Http1xServerResponse class that causes it to fail when it is in a Docker container? Or is there something in Docker that causes multiple requests to hit the same route? Has anyone else put Vert.x applications into Docker containers and seen this kind of behavior?

Is there a way (some configuration, code change, or pother method) to make a Vert.x Web application to work properly from within a Docker container? Why does a Vert.x Web application work fine but suddenly throw IllegalStateExceptions when it is placed in a Docker container???

Someone please help fix this problem.

0

There are 0 best solutions below