docker容器A可以使用什么URL来访问另一个docker容器B(同一台开发机,不同的项目) [英] what URL can docker container A use to access another docker container B (same dev machine, different projects)

查看:437
本文介绍了docker容器A可以使用什么URL来访问另一个docker容器B(同一台开发机,不同的项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设仅在开发的某些阶段(烟熏测试一些api调用)就很少需要这样做,那么让Project Bar中的dockerized Web服务访问Project Foo中的dockerized web服务的最简单方法是什么?

Assuming we need to do this very rarely only at certain stages of development (to smoke test a few api calls), what is the simplest possible way to let a dockerized web service in project Bar access a dockerized web service in Project Foo?

在开发Mac上,Docker Engine:18.09.2,Compose:1.23.2,我们有Project Foo和Project Bar,它们各自都有自己的docker-compose文件,每个都有Web服务和数据库服务。

On a development Mac, Docker Engine: 18.09.2, Compose: 1.23.2, we have Project Foo and Project Bar which each have their own docker-compose files, each with a web service and a database service.

通常它们独立运行,并且是独立开发的。

Normally they run stand-alone, and are developed independently.

但是Project Foo的Web服务托管的API仅偶尔需要我们从Project Bar的Web容器访问

However Project Foo's web service hosts an API that only occasionally we want to access from Project Bar's web container

它们被分配给不同的主机端口, docker ps 显示Project Foo使用端口0.0.0.0:3000-> 3000 / tcp(例如,我们使用localhost:3000从Mac浏览器访问Web服务。ProjectBar使用端口0.0。 0.0:3010-> 3010 / tcp

They are assigned to different host ports, docker ps shows Project Foo uses port 0.0.0.0:3000->3000/tcp (eg, we use localhost:3000 to access the web service from the Mac's browser. Project Bar uses port 0.0.0.0:3010->3010/tcp

docker inspect for Foo显示这是我P地址为 172.18.0.3(网关 172.18.0.1),对于Bar显示,其IP地址为 172.22.0.4(网关 172.22.0.1)

docker inspect for Foo shows it's IP address is "172.18.0.3" (gateway "172.18.0.1") and for Bar shows it's IP address is "172.22.0.4" (gateway "172.22.0.1")

docker network ls 显示它们都使用相同的桥驱动程序。

docker network ls shows they are both using the same "bridge" Driver.

两个项目都在运行Mac,总共4个容器(Foo上的web + db和Bar上的web + db)

Both projects are running on the Mac, 4 containers total, (web+db on Foo, and web+db on Bar)

如果在Bar上运行的程序(Ruby)需要访问REST Foo上的URL,Foo上 / api_test的URL是什么?

If a program (Ruby) running on Bar needs to access a REST URL on Foo, what is the URL of "/api_test" on Foo?

从我尝试过的Bar Web容器中 http:// localhost:3000 / api_test http://127.0.0.1:3000/api_test (这是我们在网络浏览器中使用的功能)并没有真正期望从容器到容器都能正常工作),我尝试了 http://172.18.0.3:3000/api_test http://172.18.0.3/api_test 都不起作用。

From the Bar web container I've tried http://localhost:3000/api_test and http://127.0.0.1:3000/api_test (which is what we'd use from a web browser so didn't really expect that to work from container-to-container) and I've tried http://172.18.0.3:3000/api_test and http://172.18.0.3/api_test neither of which worked.

我看到有关设置链接或docker网络的在线参考,但是所有示例都是使用docker run代替usin时的示例g docker-compose。我希望,如果您知道每个容器的Web服务器的IP和端口,那么应该使用正确的URL而无需进行任何额外的网络设置吗?

I see online references to setting up a link or a docker network, but all of the examples are for when using docker run instead of using docker-compose. I would expect that if you know the IP and port of each container's web server, it ought to be a matter of using the correct URL without any extra network setup?

可以提供任何帮助。

首选手动分配的静态IP解决方案 ...在Docker之前,我们使用了Vagrant,这很简单,在每个项目的Vagrantfile中,我们只需在相同的专用子网192.168.50.50和192.168.50.51上手动为其分配IP,它们就可以互相交谈了,我们可以简单地将这些IP编码到我们的开发代码中。 Docker似乎还有一层抽象层让我感到困惑。

A manually-assigned static IP solution is preferred... Before Docker, we used Vagrant and that was simple, in each project's Vagrantfile we simply manually assigned them an IP on the same private subnet 192.168.50.50 and 192.168.50.51 and they "talked" to each other just fine, and we could simply 'code' those IPs into our development code. Docker seems to have an additional layer of abstraction that has me puzzled.

推荐答案

TLDR :使用< a href = http://host.docker.internal rel = noreferrer> http://host.docker.internal

来自< a href = https://docs.docker.com/docker-for-mac/networking/ rel = noreferrer> https://docs.docker.com/docker-for-mac/networking/ ,


主机的IP地址正在更改(如果没有网络访问权限,则没有IP地址)。从18.03开始,我们的建议是连接到特殊的DNS名称host.docker.internal,它将解析为主机使用的内部IP地址。这是出于开发目的,不适用于Mac的Docker Desktop以外的生产环境。

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac.

我尝试过了,并且该方法适用于Windows

I tried and this works for windows as well.

例如,如果您在端口5000上的容器中运行python应用程序/微服务,则可以使用它来连接它:

For example, if you are running a python app/microservice in a container on port 5000, you may use this to connect to it:

requests.get('http://host.docker.internal:5000')

这篇关于docker容器A可以使用什么URL来访问另一个docker容器B(同一台开发机,不同的项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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