1

I am deploying a mariadb cluster like this.

(host) $ cat docker-compose.yaml
version:        '3.6'
services:
  parent:
    image:      erkules/galera
    command:    ["--wsrep-cluster-name=local-test", "--wsrep-cluster-address=gcomm://"]
    hostname:   parent
  child:
    image:      erkules/galera
    command:    ["--wsrep-cluster-name=local-test", "--wsrep-cluster-address=gcomm://parent"]
    depends_on:
      - parent
    deploy:
      replicas: 5
(host) $ sudo docker stack deploy --compose-file docker-compose.yaml mariadb

Now I am trying to find the ips of the containers within the bridge network, so that I can try the connect to the db servers from host machine. I can find like this,

(host) $ docker exec $(docker ps -q | head -n 1) /sbin/ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
176: eth0@if177: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default 
    link/ether 02:42:0a:00:01:07 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.0.1.7/24 brd 10.0.1.255 scope global eth0
       valid_lft forever preferred_lft forever
182: eth1@if183: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:13:00:08 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet 172.19.0.8/16 brd 172.19.255.255 scope global eth1
       valid_lft forever preferred_lft forever
(host) $ mysql -h 172.19.0.8 -u root
Welcome to the MariaDB monitor. ...

But I have to do some dirty parsing. So I am wondering if there is an elegant way to get this using only docker provided commands. Example, for ips in the overlay network, we can use inspect command to get a json output.

(host) $ docker ps -q | xargs docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{ .Id }}' 
10.0.1.7 c8e3dfc13c60c6925e55dff1c8dad5fb8e9bbb2335743671e45cd2f4d47fabab
10.0.1.8 7fbede8ffa63e007544c28efcc2ec2418ad44b2012e849489c25536a8408e9f6
10.0.1.6 fe0b7dcdd26fa3edecc025a5b6be0bfab04bce4d448587e5488e414dba595758
10.0.1.10 2fe03472255577db0b2d54f40422be15915121fedf3873d9e09082d5caad7f2f
10.0.1.9 0b34241582be3218d022cc58c95ce21a8be0c46dcd2ff7bca64a02a11427953a
10.0.1.4 5acc231db33b494a83010f0d6397b11365d14ca264f52bc477c642a9eda0be3f

Edit1: I want to keep the deploying part as general as possible. I don't want to publish ports. Then I have to assign a different port to every single container.

Edit2: Apparently, for multi-host swarm overlay network, Docker uses docker_gwbridge interface. So I can do docker network inspect docker_gwbridge to get the ips for each container.

rnbguy
  • 1,369
  • 1
  • 10
  • 28
  • Possible duplicate of [Connect to Docker MySQL container from localhost?](https://stackoverflow.com/questions/32360687/connect-to-docker-mysql-container-from-localhost) – David Maze Sep 12 '18 at 16:02
  • I don't want to publish the ports. Then I have to assign different ports to each container. I want to keep the deploying part as general as possible. – rnbguy Sep 12 '18 at 16:08

2 Answers2

0

Generally, when we use compose, the order of our docker-compose.yml containers id, take .2... .3 .... etc.

So, try creating a network (docker create network...) and put at the last of your docker-compose.yml por example:

rabbitmq:
ports:
   - "8201:8080"
volumes:
   - /share:/share
container_name: rabbitmq-int1
hostname: rabbitmq-int1
cpu_shares: 10
mem_limit: 2000000000
networks:
   compose_net:
         ipv4_address: 172.12.0.3
networks:
  compose_net:
     external:
        name: network_compose

Where "network_compose" is the network previously created in Host/Server (docker create network...)

Chuss
  • 142
  • 1
  • 3
0

This command show your network details, contain list containers join in your network and their IP. Hope this helpfull

docker network ls // To get list network running -> get network id
docker network inspect network_id // Now you can get container IP
Truong Dang
  • 3,119
  • 1
  • 15
  • 21