How do I access a spring app running in a docker c

2019-01-20 18:36发布

问题:

I have the following for a docker file...

FROM openjdk:11-jdk-slim
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Dexternal.config.active='false'","-jar","/app.jar"]

And spring config...

docker {
  springBootApplication {
    baseImage = 'openjdk:11-jdk-slim'
    ports = [9090, 8080]
    tag = 'app:0.2'
  }
}

Everything seems to start file and I see

Started Application in 8.743 seconds (JVM running for 9.153)

and I see...

f46b4cbfa799        646eced45077        "java -jar /app/simp…"   15 minutes ago      Up 15 minutes                 8080/tcp, 9090/tcp   mystifying_kirch

But when I run docker inspect <imageId> | grep "IPAddress" and enter that address in the browser like this http://<IP>:8080 I get a timeout error. I know I can use the port mapping on run but is there a way I can do it without mapping to my localhost?

Also I tried this...

curl 172.17.0.2:8080
curl: (7) Failed to connect to 172.17.0.2 port 8080: Operation timed out

So it isn't the browser.

Also tried to map it like this...

0.0.0.0:8080->8080/tcp, 9090/tcp

But localhost:8080 (or the ip address) sends an empty response " didn’t send any data."

回答1:

The typical issue that occurs when dockerizing a web service (here, a Java Spring Boot application) is that the localhost address shouldn't be used and replaced with 0.0.0.0.

For more details, see this SO answer that explains well what represents localhost in the context of Docker.

Regarding the 0.0.0.0 address, this special IP just means here "any IPv4 address".

Finally as the OP confirmed in a comment, for Spring Boot it suffices to assign the server.address property in the application.properties file to achieve this (cf. documentation).



标签: docker