Share IP address between docker containers

2019-09-01 08:13发布

问题:

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.

Using docker inspect, I am able to fetch the IP address of zookeeper image and pass it during docker run of kafka.

But is there any way to automatically detect and share the IP address between docker containers.

I could see some example using --link, but in the latest docker documentation it seems deprecated.

Appreciate any suggestion/help

Thanks

回答1:

Docker Compose is a perfect solution for your case.

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.

The docker-compose.yaml is something like below one for your case:

version: '3'
services:
  zookeeper:
    image: <zookeeper_image_name>
    restart: always
    ports:
      - 2181:2181
  kafka:
    image: <kafka_image_name>
    restart: always

Kafka container should have zookeeper as a zookeeper hostname in server.properties file.



标签: docker