从一个 docker 容器到另一个容器的 REST 请求失败 [英] REST request from one docker container to another fails

查看:130
本文介绍了从一个 docker 容器到另一个容器的 REST 请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序,其中一个具有 RESTful 接口,供另一个使用.两者都在同一台机器上运行.

I have two applications, one of which has a RESTful interface that is used by the other. Both are running on the same machine.

应用程序 A 在 docker 容器中运行.我正在使用命令行运行它:

Application A runs in a docker container. I am running it using the command line:

docker run -p 40000:8080 --name AppA image1

当我在 docker 容器外测试应用程序 B 时(换句话说,在它被 dockerized 之前)应用程序 B 成功执行了所有 RESTful 请求并毫无问题地接收响应.

When I test Application B outside a docker container (in other words, before it is dockerized) Application B successfully executes all RESTful requests and receives responses without problems.

不幸的是,当我在容器中 dockerize 和运行应用程序 B 时:

Unfortunately, when I dockerize and run Application B within a container:

docker run -p 8081:8081 --name AppB image2

每当我尝试向应用程序 A 发送 RESTful 请求时,我都会收到以下信息:

whenever I attempt to send a RESTful request to Application A, I get the following:

Connect to localhost:40000 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused

当然,我也尝试使用我机器的 IP 地址连接应用程序 B.当我这样做时,我得到以下失败:

Of course, I also tried making Application B connect using my machine's IP address. When I do that, I get the following failure:

Connect to 192.168.1.101:40000 failed: No route to Host

有没有人见过这种行为?是什么导致与 docker 容器外的另一个 dockerized 应用程序完美通信的应用程序一旦本身被 dockerized 就无法与同一个 dockerized 应用程序通信???

Has anyone seen this kind of behavior before? What causes an application that communicates perfectly well with another dockerized application outside a docker container to fail to communicate with that same dockerized application once it is itself dockerized???

有人请指教...

推荐答案

只需将B链接到A docker run -p 8081:8081 --link AppA --name AppB image2,即可访问使用 AppA:8080 的 REST 服务.

Simply linking B to A docker run -p 8081:8081 --link AppA --name AppB image2, then you can access the REST service using AppA:8080.

原因是 Docker 容器在其自己的子网(通常为 172.17.0.0-255)上运行,并且它们无法访问您的主机所在的网络.另外 localhost 将是容器本身,而不是主机.

The reason is that Docker containers run on its own subnet (normally 172.17.0.0-255) and they cannot access the network that your host is on. Also localhost would be the container itself, not the host.

这篇关于从一个 docker 容器到另一个容器的 REST 请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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