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

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

问题描述

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

I have a container1 running a service1 on port1

我有一个容器2在端口2上运行service2

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