Java 如何向在 docker 容器中运行的服务器发送 HTTP 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36338538/
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
How to send HTTP requests to my server running in a docker container?
提问by Andy
I've set up a java application which acts as a web server and handles http requests. I've tested that it works outside the container (which it does), but when in the container my requests don't seem to get to it.
我已经设置了一个 Java 应用程序,它充当 Web 服务器并处理 http 请求。我已经测试过它在容器外工作(确实如此),但是当在容器中时,我的请求似乎没有得到它。
The server listens on port 3971, and the Dockerfile looks like this:
服务器监听 3971 端口,Dockerfile 如下所示:
FROM java:8
ADD VaultServer /
EXPOSE 3971
EXPOSE 3972
ENTRYPOINT ["java", "-jar", "VaultServer.jar"]
Calling to the root address should return something (normally I'd send a GET to http://localhost:3971/).
调用根地址应该返回一些东西(通常我会发送一个 GET 到http://localhost:3971/)。
I've tried replacing 'localhost' with the the ip address of docker-machine, and also with the ip address I get when inspecting the running container for my server, but neither seem to respond. When I call to the ip address of docker-machine, I get ERR_CONNECTION_REFUSED. Is there something else I need to enable?
我尝试用 docker-machine 的 ip 地址替换 'localhost',以及在检查服务器正在运行的容器时得到的 ip 地址,但似乎都没有响应。当我调用 docker-machine 的 IP 地址时,我得到 ERR_CONNECTION_REFUSED。我还需要启用其他功能吗?
回答by danday74
you are doing EXPOSE 3972 which exposes the port to other linkedcontainers but NOT to the host machine
你正在做 EXPOSE 3972 它将端口暴露给其他链接的容器而不是主机
To expose the port to the host machine you do ...
要将端口公开给主机,您可以...
docker run -p 3972:3972 ....... etc
you can also do ...
你也可以做...
docker run -P
which exposes all ports exposed by EXPOSE to the host (this is not the default - you must use the -P flag here)
它将 EXPOSE 公开的所有端口暴露给主机(这不是默认设置 - 您必须在此处使用 -P 标志)
回答by Gene
Use the "action="http://192.168.99.100:8080/rest/data/ingest" in your html to refer to your Docker Container's Rest Path. See the bottom HTML Code, line 4.
使用 html 中的“action="http://192.168.99.100:8080/rest/data/ingest”来引用 Docker 容器的 Rest Path。请参阅底部的 HTML 代码,第 4 行。
http://192.168.99.100:8080/rest/data/ingestBreakdown:
http://192.168.99.100:8080/rest/data/ingest细分:
1) 192.168.99.100:8080= Docker's IP Address & Port #.
1) 192.168.99.100:8080= Docker 的 IP 地址和端口号。
2) /rest/ = In your web.xml file
2) /rest/ = 在你的 web.xml 文件中
3) data/= @Path("/data")
3) data/= @Path("/data")
4) /ingest= @Path("/ingest")
4) /ingest= @Path("/ingest")
HTML CODE:
HTML代码:
<html>
<body>
<h1>Data Ingest: Web Uploader</h1>
<form action="http://192.168.99.100:8080/rest/data/ingest" method="post" enctype="multipart/form-data">
<p>Select files
: <input type="file" name="file" size="10000000" multiple/> </p>
<input type="submit" value="Upload Files"/>
</form>
</body>
</html>
JAVA CODE:
爪哇代码:
@Path("/data")
public class YourClass {
@POST
@Path("/ingest")
public Response yourMethod (...){
}
回答by Sergiy Matvienko
I can show you how I compose dotnet core project with other containers (MSSQL and Elasticsearch), maybe it will help you.
我可以向您展示我如何使用其他容器(MSSQL 和 Elasticsearch)组合 dotnet 核心项目,也许它会对您有所帮助。
To docker-compose.yml add dependencies for another container:
要 docker-compose.yml 为另一个容器添加依赖项:
services:
prime.web:
image: prime.web
build:
context: ./Prime.Web
dockerfile: Dockerfile
links:
- sql.data
- elasticsearch
sql.data:
image: microsoft/mssql-server-linux
elasticsearch:
image: elasticsearch
And write in docker-compose.override.yml, where you will override your appsetting file, and will get access to another container:
并在 docker-compose.override.yml 中写入,您将在其中覆盖您的 appsetting 文件,并可以访问另一个容器:
services:
prime.web:
environment:
- 'ConnectionStrings:primekinetics=Server=tcp:sql.data,1433;InitialCatalog=prime;User ID=sa;Password=Pass@word;Connection Timeout=30;'
- 'ElasticSearchConnection=http://elasticsearch:9200/'
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "5105:80"
sql.data:
environment:
- SA_PASSWORD=Pass@word
- ACCEPT_EULA=Y
ports:
- "5433:1433"
elasticsearch:
ports:
- "9200:9200"
I hope it will be helpful.
我希望它会有所帮助。
回答by Mauricio
Are you using Docker Toolbox???
你在使用 Docker 工具箱吗???
In my case, the normal docker installation didnt work on my Windows 10 so I had to install Docker toolbox; docker toolbox adds a virtual machine where the docker runs all their containers.
就我而言,正常的 docker 安装在我的 Windows 10 上不起作用,所以我不得不安装 Docker 工具箱;docker 工具箱添加了一个虚拟机,docker 在其中运行所有容器。
For example, to access my dockerized apps I have to use: http://192.168.99.100:8080/app.
例如,要访问我的 dockerized 应用程序,我必须使用:http: //192.168.99.100: 8080/app 。
Here, 192.168.99.100 is the virtual machine ip.
这里,192.168.99.100 是虚拟机的ip。
Hope it helps you!!!!
希望对你有帮助!!!!