如何在 Docker 图片中使用托管在 Tomcat 9 服务器上的静态文件

How to consume static file hosted on Tomcat 9 Server in Docker Image

Tomcat 服务器的版本:tomcat-9.0.10

基本上,我在 tomcat 服务器的 ROOT 文件夹中托管了一个 docx 文件。如果我尝试在我的浏览器上获取文件,它可以使用“http://localhost:1212/Manufacturing.docx”。当我尝试在我的 webapp 的本地构建中使用该文件时,它起作用了。但是当我尝试在 docker 容器上托管的应用程序中使用“http://localhost:1212/Manufacturing.docx”时,它无法获取文件(似乎无法建立连接) .知道为什么我可以在容器外提供文件但不能在容器内提供服务吗?

当您尝试从容器访问 localhost 时,它解析为容器内的本地主机,而不是主机。

可能的解决方案:

  1. 如果您使用 Docker 18.03+ 并且在 Windows / Mac 上,访问 host.docker.internal 将解析为主机的 127.0.0.1。
  2. 运行 您的容器使用 docker run -d --network=host your-image:tag。这样你的容器将与你的主机共享网络命名空间,并且容器内的 localhost 将与主机上的 localhost 相同。

另见:From inside of a Docker container, how do I connect to the localhost of the machine? and What is linux equivalent of "host.docker.internal"