java 在 Docker 中对端点进行 REST 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44784666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:21:05  来源:igfitidea点击:

Making a REST Call to Endpoint in Dockers

javarestdockerspring-boot

提问by dFrancisco

I am building a Spring Boot application, which has a few different REST endpoints. It can be locally packaged and launched as a jar file successfully. When running locally, I can access its endpoints via "http://localhost:8080/endpoint?params..". I was tasked with now preparing this application to run off of Dockers. Still working on my local machine, I have created a Dockers container based off of the Java:8 image. In this container, I have been able to run my application from the .jar successfully. My issue is, I do not understand how to call to the REST endpoints inside the application, when the application is hosted off of Docker, since logically localhost:8080/endpoint is no longer responsive to the call.

我正在构建一个 Spring Boot 应用程序,它有几个不同的 REST 端点。它可以在本地打包并作为 jar 文件成功启动。在本地运行时,我可以通过“ http://localhost:8080/endpoint?params..”访问它的端点。我现在的任务是准备这个应用程序以运行 Docker。仍然在我的本地机器上工作,我创建了一个基于 Java:8 镜像的 Dockers 容器。在这个容器中,我已经能够成功地从 .jar 运行我的应用程序。我的问题是,当应用程序托管在 Docker 之外时,我不明白如何调用应用程序内部的 REST 端点,因为逻辑上 localhost:8080/endpoint 不再响应调用。

Side information: My local computer is Windows, the Docker image is Ubuntu (eventually will be launched onto a Linux server).

附带信息:我的本地计算机是 Windows,Docker 映像是 Ubuntu(最终将在 Linux 服务器上启动)。

UPDATE: Created a new image with the following Dockerfile:

更新:使用以下 Dockerfile 创建了一个新映像:

FROM openjdk:8
MAINTAINER  My Name [email protected]
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
EXPOSE 8080
RUN javac Main.java
CMD ["java", "Main"]

Same issue, cannot access endpoint via http://localhost:8080/endpoint

同样的问题,无法通过http://localhost:8080/endpoint访问端点

Any help will be appreciated. Thank you!

任何帮助将不胜感激。谢谢!

回答by Andy Shinn

You need to publish the port (not EXPOSEit). Exposing a port is largely used for links and service contexts. In your example of just running a Docker container, you need to simply publish the port so it is available from the host. You do this with --publishor -p:

您需要发布端口(不是EXPOSE它)。暴露端口主要用于链接和服务上下文。在您仅运行 Docker 容器的示例中,您只需发布端口即可从主机获得该端口。您可以使用--publish或执行此操作-p

docker run -d --name myapp -p 8080:8080 myappimage

Then you can access the application at port 8080 on the host IP address (Docker on Windows and Docker on Mac run a proxy that should allow localhost:8080to work).

然后,您可以访问主机 IP 地址上端口 8080 上的应用程序(Windows 上的 Docker 和 Mac 上的 Docker 运行应该允许localhost:8080工作的代理)。

回答by Jairton Junior

If your application is running inside of a Docker Container and you can access from inside this containerusing localhost:8080, then all you have to do is add the EXPOSE instruction in your DOCKERFILE (see Dockerfile expose option).

如果您的应用程序在 Docker 容器内运行,并且您可以使用 localhost:8080从该容器内进行访问,那么您所要做的就是在 DOCKERFILE 中添加 EXPOSE 指令(请参阅Dockerfile 公开选项)。

EXPOSE 8080

Then you probably will be able to access from the host machine (where Docker is installed and running) using the default IP from the docker0 network interface. Generally this IP is 172.17.0.X, where X is 2 for your first container, and so on (see docker default networking).

然后,您可能能够使用来自 docker0 网络接口的默认 IP 从主机(安装并运行 Docker 的地方)进行访问。通常,此 IP 为 172.17.0.X,其中第一个容器的 X 为 2,依此类推(请参阅docker 默认网络)。

So try to access from outside of the docker using "http://172.17.0.2:8080/endpoint?params..". Also, if you want to allow external access (or access using localhost from the host machine) you should start your container mapping the port from the EXPOSE instruction by using -p parameter (see Mapping Exposed Incoming Ports).

所以尝试使用“ http://172.17.0.2:8080/endpoint?params..”从docker外部访问。此外,如果您想允许外部访问(或从主机使用 localhost 访问),您应该使用 -p 参数从 EXPOSE 指令启动容器映射端口(请参阅映射公开的传入端口)。