简体   繁体   English

在Dockers中对端点进行REST调用

[英]Making a REST Call to Endpoint in Dockers

I am building a Spring Boot application, which has a few different REST endpoints. 我正在构建一个Spring Boot应用程序,它具有几个不同的REST端点。 It can be locally packaged and launched as a jar file successfully. 可以将其本地打包并成功作为jar文件启动。 When running locally, I can access its endpoints via " http://localhost:8080/endpoint?params ..". 在本地运行时,我可以通过“ http:// localhost:8080 / endpoint?params ..”访问其端点。 I was tasked with now preparing this application to run off of Dockers. 我的任务是现在准备将此应用程序运行在Docker之外。 Still working on my local machine, I have created a Dockers container based off of the Java:8 image. 仍在我的本地计算机上工作,我基于Java:8映像创建了一个Dockers容器。 In this container, I have been able to run my application from the .jar successfully. 在此容器中,我已经能够从.jar成功运行我的应用程序。 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. 我的问题是,从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@email.com
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! 谢谢!

You need to publish the port (not EXPOSE it). 您需要发布端口(而不是EXPOSE端口)。 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. 在仅运行Docker容器的示例中,您只需简单地发布端口,以便主机可以使用它。 You do this with --publish or -p : 您可以使用--publish-p进行此--publish

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:8080 to work). 然后,您可以在主机IP地址的端口8080上访问应用程序(Windows上的Docker和Mac上的Docker运行应允许localhost:8080正常工作的代理)。

If your application is running inside of a Docker Container and you can access from inside this container using 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. 然后,您可能将能够使用来自docker0网络接口的默认IP从主机(安装并运行Docker的主机)进行访问。 Generally this IP is 172.17.0.X, where X is 2 for your first container, and so on (see docker default networking ). 通常,此IP为172.17.0.X,其中第一个容器的X为2,依此类推(请参阅docker default network )。

So try to access from outside of the docker using " http://172.17.0.2:8080/endpoint?params ..". 因此,尝试使用“ 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 ). 另外,如果要允许外部访问(或从主机使用localhost访问),则应使用-p参数从EXPOSE指令启动容器映射端口(请参阅映射暴露的传入端口 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM