从另一个容器访问的 docker 容器端口 [英] docker container port accessed from another container

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

问题描述

我有一个容器 1 在端口 1 上运行服务 1

I have a container1 running a service1 on port1

还有

我有一个在端口 2 上运行服务 2 的容器 2

I have a container2 running a service2 on port2

如何从 service1:port1 访问 service2:port2?

How can I access service2:port2 from service1:port1?

我提到容器是链接在一起的.

I mention that the container are linked together.

请问有没有办法在不访问docker0 IP(端口可见)的情况下做到这一点

I ask if there is a way to do it without accessing the docker0 IP (where the port is visible)

谢谢

推荐答案

首选的解决方案是将两个容器放在同一个网络上,使用内置的 dns 发现通过名称到达另一个节点,您将能够通过容器端口访问它们,而不是主机发布的端口.通过 CLI,看起来像:

The preferred solution is to place both containers on the same network, use the build-in dns discovery to reach the other node by name, and you'll be able to access them by the container port, not the host published port. By CLI, that looks like:

docker network create testnet
docker run -d --net testnet --name web nginx
docker run -it --rm --net testnet busybox wget -qO - http://web

busybox 显示了一个示例客户端容器,它通过端口 80 连接到名为 web 的 nginx 容器.请注意,无需发布此端口即可被其他容器访问.

The busybox shows a sample client container connecting to the nginx container with the name web, over port 80. Note that this port didn't need to be published to be reachable by other containers.

使用自己的网络设置多容器环境是 docker-compose 的一项常见任务,因此如果您发现自己经常这样做,我建议您研究一下这个工具.

Setting up multi-container environments with their own network is a common task for docker-compose, so I'd recommend looking into this tool if you find yourself doing this a lot.

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

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