Home > Software design >  Docker, Communicate Two Containers Different Hosts Same Network
Docker, Communicate Two Containers Different Hosts Same Network

Time:07-08

Good morning, everybody,

I was doing some testing with docker and I encountered a problem, for which I did not find a solution for the same case.

In my deployment, I want a server and a client to communicate, so far in different containers but on the same host. From the client I used the private IP of the server, and everything worked fine, here I show the compose:

Server:

   version: "3.3"
services:
  app:
    image: python-server:ARM
    ports:
      - 8080:8080

Client:

  version: "3.3"
services:
  app:
    image: python-client:ARM
    ports:
      - 5040:5040
    volumes:
      - /dataf/1.csv:/app/1.csv:ro
networks:
  default:
    external:
      name: server_default

However, when trying to do this communication, on two separate hosts [for example one on my host and one on virtual machine, both are on the same network] this fails. It is doubtful to me what the yamls should look like so that client and server could communicate with each other

CodePudding user response:

In most cases, Docker networks are local to a specific machine. You may be able to set up an overlay network but this is an advanced setup, and you may be better served by using a cluster-aware container orchestrator like Kubernetes instead of trying to manually set this up.

Since both services publish ports:, from one machine you can call to the other machine's DNS name and the first published ports: number.

# client/docker-compose.yml
services:
  client:
    environment:
      SERVER_URL: http://server-host.example.com:8080

You do not need special networks: setup here, and again you can't share a Docker network between the two containers on different hosts. This setup actually doesn't care whether or not the server is running in a container or not, or if that URL is to a proxy that forwards to it.

Note that I've configured the URL in an environment variable. You'll need to update your code to honor this. The URL will be different in this multi-host setup, or in a single-host Compose-native setup, or if you're running the two processes side-by-side without Docker, or if you do eventually adopt Kubernetes.

CodePudding user response:

What i finally did was use overlay network.

--- SERVER ---
docker swarm init 
docker network create --driver=overlay --attachable test-net
docker run -it --network test-net python-server

---CLIENT---
sudo docker swarm join --token <gen_token_above>
sudo docker run -it -v path_host:path_container --network test-net python-client

Then use the server's private ip, also known to the client since they are on the same network, to point to it.

  • Related