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

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

问题描述

假设我们只在开发的某些阶段很少需要这样做(以冒烟测试一些 api 调用),让项目 Bar 中的 dockerized web 服务访问 Project Foo 中的 dockerized web 服务的最简单方法是什么?

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

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

然而,Project Foo 的 Web 服务托管了一个 API,我们只是偶尔想从 Project Bar 的 Web 容器中访问该 API

它们被分配到不同的主机端口,docker ps 显示 Project Foo 使用端口 0.0.0.0:3000->3000/tcp(例如,我们使用 localhost:3000 从Mac 浏览器,项目栏使用端口 0.0.0.0:3010->3010/tcp

docker inspect for Foo 显示它的 IP 地址是172.18.0.3"(网关172.18.0.1"),而 Bar 显示它的 IP 地址是172.22.0.4"(网关172.22.1").0.1")

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

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

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

从 Bar web 容器中,我尝试了 http://localhost:3000/api_testhttp://127.0.0.1:3000/api_test(这是我们从 Web 浏览器使用的内容,所以并没有真正期望它可以从容器到容器工作),我已经尝试过 http://172.18.0.3:3000/api_test 和 <代码>http://172.18.0.3/api_test 两者都不起作用.

我看到了关于设置链接或 docker 网络的在线参考资料,但所有示例都是针对使用 docker run 而不是使用 docker-compose 时的.我希望如果您知道每个容器的 Web 服务器的 IP 和端口,那么应该是使用正确的 URL 而无需任何额外网络设置的问题?

任何帮助将不胜感激.

首选手动分配的静态 IP 解决方案... 在 Docker 之前,我们使用 Vagrant,这很简单,在每个项目的 Vagrantfile 中,我们只需手动为它们分配同一私有子网 192.168 上的 IP.50.50 和 192.168.50.51 并且它们彼此交谈"得很好,我们可以简单地将这些 IP编码"到我们的开发代码中.Docker 似乎有一个额外的抽象层,这让我感到困惑.

解决方案

TLDR: 使用 http://host.docker.internal

来自 https://docs.docker.com/docker-for-mac/网络/,

<块引用>

主机有一个不断变化的 IP 地址(如果您没有网络访问权限,则没有).从 18.03 开始​​,我们的建议是连接到特殊的 DNS 名称 host.docker.internal,它解析为主机使用的内部 IP 地址.这是用于开发目的,不适用于 Docker Desktop for Mac 之外的生产环境.

我试过了,这也适用于 Windows.

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

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

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?

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.

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

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 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 shows they are both using the same "bridge" Driver.

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

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

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.

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?

Any help would be appreciated.

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: Use http://host.docker.internal

From https://docs.docker.com/docker-for-mac/networking/,

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.

I tried and this works for windows as well.

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天全站免登陆