在Dockers中对端点进行REST调用 [英] Making a REST Call to Endpoint in Dockers
问题描述
我正在构建一个Spring Boot应用程序,它具有一些不同的REST端点。可以将其本地打包并成功作为jar文件启动。在本地运行时,我可以通过 http:// localhost:8080 / endpoint?params 访问其端点。 。我的任务是现在准备将此应用程序运行在Docker之外。仍在我的本地计算机上工作,我已经基于Java:8映像创建了一个Dockers容器。在此容器中,我已经能够从.jar成功运行我的应用程序。我的问题是,当从Docker托管应用程序时,我不理解如何调用应用程序内的REST端点,因为从逻辑上讲localhost:8080 / endpoint不再响应该调用。
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.
边信息:我的本地计算机是Windows,Docker映像是Ubuntu(最终将在Linux服务器上启动)。
Side information: My local computer is Windows, the Docker image is Ubuntu (eventually will be launched onto a Linux server).
更新:使用以下Dockerfile创建了新映像:
UPDATE: Created a new image with the following Dockerfile:
FROM openjdk:8
MAINTAINER My Name email@email.com
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
EXPOSE 8080
RUN javac Main.java
CMD ["java", "Main"]
同一问题,无法通过 http:// localhost:8080 / endpoint
任何帮助将不胜感激。谢谢!
Any help will be appreciated. Thank you!
推荐答案
您需要发布端口(不是 EXPOSE
它)。公开端口主要用于链接和服务上下文。在仅运行Docker容器的示例中,您需要简单地发布端口,以便主机可以使用它。您可以通过-发布
或 -p
来做到这一点:
You need to publish the port (not EXPOSE
it). 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 --publish
or -p
:
docker run -d --name myapp -p 8080:8080 myappimage
然后,您可以访问主机IP地址上端口8080的应用程序(Windows上的Docker和Mac上的Docker运行一个代理,该代理应允许 localhost:8080
起作用)。
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:8080
to work).
这篇关于在Dockers中对端点进行REST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!