Tuesday, 31 December 2019

linux - Communication between Docker containers on differents hosts in the same local network


I have two Docker containers running on two different hosts:



  • Computer A (PC, uses Ethernet), ip 192.168.0.11 [ Docker container running inside on 172.17.0.2 ], OS Windows 7

  • Computer B (laptop, uses WLAN), ip 192.168.0.12 [ Docker container running inside on 172.17.0.2 ], OS Linux Ubuntu


I would like to somehow connect them, so they could communicate with each other (e.g. via ssh).


I have tried to "link" my containers by running them with:


docker run --name master_node --rm -it branislava/ubuntu1504java8:v1

docker run --rm -it --link master_node:master_node1 --name slave_node branislava/ubuntu1504java8:v1

As suggested in comments. This is working; containers can communicate - but only when they are run on the same host machine.


How could this be accomplished for containers that are running on different machines in the same local network?



Answer



Following is the bash script for establishing communication between docker containers of the same image across different hosts in the same local network:


 # Firstly, we have to make a swarm
# We have to discover ip address of the swarm leader (i.e. manager, master node)
ifconfig
# My master computer has ip 192.168.0.12 in the local network
sudo docker swarm init --advertise-addr 192.168.0.12
# Info about swarm
sudo docker info
# info about nodes (currently only one)
sudo docker node ls
# hostaname got it name automatically, by the host's name

# adding new nodes to the swarm
# ssh to the slave node (i.e. worker node) or type physically:
# this command was generated after
# sudo docker swarm init --advertise-addr 192.168.0.12
# on manager computer
docker swarm join --token SWMTKN-1-55b12pdctfnvr1wd4idsuzwx34vcjwv9589azdgi0srgr3626q-01zjw639dyoy1ccgpiwcouqlk 192.168.0.12:2377

# if one cannot remember or find this command
# should type again on the manager host
docker swarm join-token worker

# ssh to the manager or type in directly:
# (listing existing nodes in a swarm)
sudo docker node ls
# adding docker image as a process that will run in this containers
# ssh to the manager or type in directly:
# replicas will be the number of nodes
# manager is also a worker node
sudo docker service create --replicas 2 --name master_node image-name sleep infinity
# I have to enter inside of the containers and set up some things before
# running application, so I say `sleep infinity`
# Else, this is not necessary.

# what's up with the running process
sudo docker service inspect --pretty etdo0z8o8timbsdmn3qdv381i
# or
sudo docker service inspect master_node
# also, but only from manager
sudo docker service ps master_node
# see running containers (from worker or from manager)
sudo docker ps

# promote node from worker to manager
# `default` is the name of my worker node
sudo docker node promote default
# denote node from manager to worker
sudo docker node demote default

# entering container, if needed
# getting container id with `sudo docker ps`
sudo docker exec -it bb923e379cbd bash

# retrieving ip and port of the container
# I need this since my containers are communicating via ssh
sudo docker ps
sudo docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 5395cdd22c44

# removing processes from all nodes
sudo docker service rm master_node
# this command should say `no process`
sudo docker service inspect master_node

Hopefully, someone will find this helpful.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...