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

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.