如何访问在Docker容器中运行的Spring应用程序? [英] How do I access a spring app running in a docker container?

查看:254
本文介绍了如何访问在Docker容器中运行的Spring应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对docker文件有以下要求...

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

然后配置弹簧...

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

一切似乎可以启动文件,而且我看到



在8.743秒内启动了应用程序(JVM运行9.153)



然后我看到...

  f46b4cbfa799 646eced45077 java -jar / app / simp… 15分钟前上15分钟8080 / tcp,9090 / tcp mystifying_kirch 

docker inspect< imageId> | grep IPAddress 并在浏览器中输入该地址,例如 http://< IP>:8080 我收到超时错误。我知道我可以在运行时使用端口映射,但是有没有一种方法可以映射到我的本地主机呢?



我也尝试过此方法...

 卷曲172.17.0.2:8080 
curl:(7)无法连接到172.17.0.2端口8080:操作超时

所以它不是浏览器。



也尝试这样映射它...



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



但是localhost:8080(或IP地址)发送一个空响应未发送任何数据。

解决方案

在对Web服务(这里是Java Spring Boot应用程序)进行泊坞处理时,典型的问题是 localhost 地址不应该使用,而应替换为 0.0.0.0



有关更多详细信息,请参见此 SO答案很好地解释了在Docker上下文中什么代表 localhost



< >关于 0.0.0.0 地址,此特殊IP 在这里的意思是任何IPv4地址。



最后,正如OP在评论中确认的那样,对于Spring Boot,只需分配<$ application.properties 文件中的c $ c> server.address 属性来实现此目的(参见文档)。


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

解决方案

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容器中运行的Spring应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆