user331465
user331465

Reputation: 3094

Spring boot app runs fine standalone, errs in docker

I have a spring-boot based java application which runs fine from the command line (embedded tomcat standalone).

Problem

When I run the app in docker, it does not run correctly. The console shows the application starts up fine with no errors; however, the browser displays the following error page:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

[I understand the message says no mapping for '/error' url. However I want to know the root cause ]

Additional Info/Context

build.gradle target

task buildDocker(type: Docker, dependsOn: build) {
  push = false
  applicationName = jar.baseName
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    copy {
      from war
      into stageDir
    }
  }
}

dockerfile

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD floss.war app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Command to Run Docker

dockerImg=1248c47d9cfa
docker run \
       -it \
       --net=host \
       -e SPRING_APPLICATION_JSON="$SPRING_APPLICATION_JSON" \
       $dockerImg

I'm new to docker and would appreciate any suggestions.

thanks in advance!

Upvotes: 3

Views: 1644

Answers (3)

thomas.schuerger
thomas.schuerger

Reputation: 121

I had the same issue. You seem to be using the same Docker example for Spring Boot as I was. As you can see, the WAR file gets a .jar extension when it is placed into the Docker image, not .war.

I was able to solve the issue by calling the file "app.war" instead of "app.jar" and changing the Java startup parameters accordingly.

If you use the extension ".jar", the access to static resources works fine, but accessing all dynamic content (JSPs, ...) leads to a 404 error.

Upvotes: 2

Manoj Sahu
Manoj Sahu

Reputation: 2932

Problems:

  1. No exposed port in dockerfile
  2. No port mapping with host.

Sol: 1. expose application port in dockerfile and build image EXPOSE $application_port 2. then run docker run -p 8080:8080 -d -e SPRING_APPLICATION_JSON="$SPRING_APPLICATION_JSON" $dockerImg

Upvotes: 3

jAvA
jAvA

Reputation: 583

i suspect it is due to the missing -p option, can you try this. change the port if it is different. docker run -p 8080:8080 -it \ --net=host \ -e SPRING_APPLICATION_JSON="$SPRING_APPLICATION_JSON" \ $dockerImg

Upvotes: 1

Related Questions