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

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

问题描述

我有以下 docker 文件...

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

和弹簧配置...

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

一切似乎都开始了文件,我明白了

Everything seems to start file and I see

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

我看到...

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

但是当我运行 docker inspect <imageId>|grep "IPAddress" 并像这样在浏览器中输入该地址 http://<IP>:8080 我收到超时错误.我知道我可以在运行时使用端口映射,但有没有一种方法可以在不映射到我的本地主机的情况下做到这一点?

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?

我也试过这个...

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

所以它不是浏览器.

也试过这样映射...

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

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

但是 localhost:8080(或 IP 地址)发送了一个空响应没有发送任何数据."

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

推荐答案

dockerizing web 服务(这里是 Java Spring Boot 应用程序)时发生的典型问题是 localhost 地址不应该t 被使用并替换为 0.0.0.0.

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.

有关更多详细信息,请参阅此 SO 答案,它很好地解释了 localhost 在Docker 的上下文.

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

关于0.0.0.0地址,这个特殊IP 此处仅表示任何 IPv4 地址".

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

最后,正如 OP 在评论中确认的那样,对于 Spring Boot,只需在 application.properties 文件中分配 server.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天全站免登陆