在Docker容器之间共享IP地址 [英] Share IP address between docker containers
问题描述
我正在使用以下命令
docker run -it -p 2181:2181 --name zookeeper zookeeper
运行单节点zookeeper docker映像。
为了运行kafka docker映像,我需要将zookeper docker映像的IP地址共享为ENV变量并更新kafka server.properties文件。
I am running single node zookeeper docker image using below command
docker run -it -p 2181:2181 --name zookeeper zookeeper
.
Inorder to run kafka docker image, I need to share the IP address of the zookeper docker image as ENV variable and update kafka server.properties file.
使用docker inspect,我能够获取zookeeper图片的IP地址,并在 docker运行
的kafka期间传递它。
Using docker inspect, I am able to fetch the IP address of zookeeper image and pass it during docker run
of kafka.
但是有什么方法可以自动检测和共享Docker容器之间的IP地址。
But is there any way to automatically detect and share the IP address between docker containers.
我可以看到一些使用--link的示例,但是在最新的docker文档中
I could see some example using --link, but in the latest docker documentation it seems deprecated.
感谢任何建议/帮助
谢谢
推荐答案
Docker Compose 是一个完美的解决方案
Docker Compose is a perfect solution for your case.
默认情况下,Compose为您的应用设置单个网络。服务的每个
容器都会加入默认网络,并且该网络上的其他容器都可以访问
,并且
可以在与容器名称相同的主机名下发现它们。
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
docker-compose.yaml
类似于以下情况:
version: '3'
services:
zookeeper:
image: <zookeeper_image_name>
restart: always
ports:
- 2181:2181
kafka:
image: <kafka_image_name>
restart: always
Kafka容器应具有 zookeeper
作为 server.properties
文件中的Zookeeper主机名。
Kafka container should have zookeeper
as a zookeeper hostname in server.properties
file.
这篇关于在Docker容器之间共享IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!