如何将HTTP请求发送到在Docker容器中运行的服务器? [英] How to send HTTP requests to my server running in a docker container?

查看:233
本文介绍了如何将HTTP请求发送到在Docker容器中运行的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个Java应用程序,它可以充当网络服务器并处理http请求。我已经测试过它可以在容器外部工作(确实可以),但是当在容器中时,我的请求似乎无法到达。

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.

服务器监听在端口3971上,并且Dockerfile如下所示:

The server listens on port 3971, and the Dockerfile looks like this:

FROM java:8
ADD VaultServer /
EXPOSE 3971
EXPOSE 3972
ENTRYPOINT ["java", "-jar", "VaultServer.jar"]

调用根地址应该返回一些信息(通常我会发送GET到 http:// localhost :3971 / )。

Calling to the root address should return something (normally I'd send a GET to http://localhost:3971/).

我尝试用docker-machine的IP地址替换'localhost',并用检查服务器的运行容器时获得的ip地址替换,但似乎都没有回应。当我调用docker-machine的IP地址时,我得到ERR_CONNECTION_REFUSED。我还需要启用其他功能吗?

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?

推荐答案

您正在做EXPOSE 3972,它将端口暴露给其他 linked 容器,但不在主机上

you are doing EXPOSE 3972 which exposes the port to other linked containers but NOT to the host machine

要将端口暴露给主机,您可以...

To expose the port to the host machine you do ...

docker run -p 3972:3972 ....... etc

您也可以...

docker run -P

将EXPOSE公开的所有端口公开给主机(这不是默认端口-您必须在此处使用-P标志)

which exposes all ports exposed by EXPOSE to the host (this is not the default - you must use the -P flag here)

这篇关于如何将HTTP请求发送到在Docker容器中运行的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆