2

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."

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • 1
    Which docker command did you use to run the image? – ErikMD Oct 03 '18 at 19:16
  • The first was supplied directly from the gradle plugin. When that wasn't working I tried to run it manually using docker (I pass a bunch of envs)... docker run -e DB_URL=$DB_URL -e DB_PORT=$DB_PORT -e DB_USER=$DB_USER ... -p 8080:8080 646eced45077 – Jackie Oct 03 '18 at 19:21
  • Another question: the docker engine is running on which OS? – ErikMD Oct 03 '18 at 19:21
  • 1
    You'd need to expose your contaimer ports in Dockerfile. Example: EXPOSE 8080. https://docs.docker.com/engine/reference/builder/#parser-directives – Kartik Pandya Oct 03 '18 at 20:30
  • you can also test by usimg exec command: "docker exec - it container_id bash" and inside the container run "curl localhost:8080/..." – Kartik Pandya Oct 03 '18 at 20:32
  • @Bloodysock thanks I will try that I included my plugin stuff because I thought that was handling that. – Jackie Oct 03 '18 at 22:36
  • adding EXPOSE 8080 did not work. – Jackie Oct 04 '18 at 19:25
  • You did not reply my previous question :) which OS are you using? if it is windows, do you use Docker for Windows with Hyper-V, or Docker Toolbox? in which case the problem you observe could be solved by looking at `docker-machine ip default` – ErikMD Oct 04 '18 at 21:08
  • Otherwise, another possible issue could be that in the Java code, you happen to listen to `localhost:8080`, while the proper ip to listen is `0.0.0.0:8080`, cf. [this SO question](https://stackoverflow.com/questions/34145880/docker-machine-port-forwarding-on-windows-not-working) – ErikMD Oct 04 '18 at 21:11
  • The Java code works fine locally and the log shows it starting correctly. I am on OSX – Jackie Oct 05 '18 at 12:17
  • Also if I don't map with the run command I should still be able to access it via the IP provided by `docker inspect` rt? I tried it both ways but with the expose the mapping seems to stop working. For example without expose 8080 I get empty responses for both the container IP and my local IP. When I add expose, I only get it from the container IP the local IP times out – Jackie Oct 05 '18 at 12:21
  • OK... Just to be sure, can you confirm you have no occurrence of "localhost" or "127.0.01" in your Java code or config file, and that you are using "0.0.0.0" as listened ip address in the Java code? – ErikMD Oct 05 '18 at 16:55
  • @ErikMD sorry it took so long. I can confirm. There is 1 place localhost is mentioned but it is in a web socket on the UI side so I would at least be getting the site just not the heartbeat. Not sure where I would be declaring the 0.0.0.0 but let me do some more looking. – Jackie Oct 09 '18 at 02:25
  • 1
    This is what I am using as my example FYI https://dzone.com/articles/a-start-to-finish-guide-to-docker-with-java?preview=true – Jackie Oct 09 '18 at 02:31
  • @ErikMD I'll let you take credit if you answer. Needed to add server.address=0.0.0.0 to application.properties – Jackie Oct 09 '18 at 20:22
  • Hi @Jackie! I'm glad that you solved the issue thanks to my suggestion; I'm just going to post an answer for future reference. – ErikMD Oct 09 '18 at 21:01

1 Answers1

3

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).

ErikMD
  • 13,377
  • 3
  • 35
  • 71