title: Docker: Multiple Client/Server Containers without Hardcoding IP Addresses
body: <p>Good afternoon. I am brand new to Docker and I'm struggling to come up with a scalable solution for generating network traffic. I want to have many client and server containers talking with each other across a docker bridge network. Right now I have one client and one server container, and they talk to each other but it is incredibly slow. Additionally, I had to hard-code their IP addresses for them to be able to find each other. There has to be a better way to scale this up, but I'm struggling because my client.py code needs the server's IP address and port number to find the server.</p>
  <p>Client.py</p>
  <pre class="lang-py prettyprint-override"><code>from pyModbusTCP.client import ModbusClient
  import time    
  
  c = ModbusClient()
  c.host(&quot;172.20.0.2&quot;)
  c.port(502)
      
  while True:
      if not c.is_open():
          if not c.open():
              print(&quot;Unable to connect to Server at 172.20.0.2:502&quot;)
          
      if c.is_open():
          regs = c.read_holding_registers(0, 4)
          if regs:
              print(&quot;Register #0: &quot; + str(regs[0]))
              print(&quot;Register #1: &quot; + str(regs[1]))
              print(&quot;Register #2: &quot; + str(regs[2]))
              print(&quot;Register #3: &quot; + str(regs[3]))
      time.sleep(5)
  </code></pre>
  <p>Client DockerFile</p>
  <pre class="lang-docker prettyprint-override"><code>FROM python:3
      
  WORKDIR ~/client-server-docker-test/client
      
  COPY requirements.txt ./
  RUN pip install --no-cache-dir -r requirements.txt
      
  COPY . .
      
  CMD [&quot;python&quot;, &quot;./client.py&quot;]
  </code></pre>
  <p>Server.py</p>
  <pre class="lang-py prettyprint-override"><code>from pyModbusTCP.server import ModbusServer, DataBank
  from time import sleep
  from random import uniform
      
  # set up server
  server = ModbusServer('localhost',502, no_block=True)
  # initialize register 0 with value of 80
  DataBank.set_words(0, [80])
      
  try:
      print(&quot;Start server...&quot;)
          server.start()
          print(&quot;Server is online...&quot;)
          # change register value every 5 seconds. 
          while True:
              # Set Register @ Address 0 to random int. value
              DataBank.set_words(0, [int(uniform(0,100))])
              # Tank 2
              DataBank.set_words(1, [int(uniform(0,100))])
              # Tank 3
              DataBank.set_words(2, [int(uniform(0,100))])
              # Tank 4
              DataBank.set_words(3, [int(uniform(0,100))])
              sleep(5)
  # when hit ctrl+C in CMD line, shut down server
  except:
      print(&quot;Shutdown server....&quot;)
      server.stop()
      print(&quot;Server is offline...&quot;)
      sleep(0.5)
  </code></pre>
  <p>Server DockerFile</p>
  <pre class="lang-docker prettyprint-override"><code>FROM python:3
      
  WORKDIR ~/client-server-docker-test/server
      
  COPY requirements.txt ./
  RUN pip install --no-cache-dir -r requirements.txt
      
  COPY . .
      
  CMD [&quot;python&quot;, &quot;./server.py&quot;]
  </code></pre>
  <p>Docker-Compose File</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '2.1'
  services:
    server:
      image: server1
      container_name: server1_con
      ports:
        - 502:502
      networks:
        test_net:
          ipv4_address: 172.20.0.2
      
    client:
      image: client1
      container_name: client1_con
      depends_on:
        - server
      networks:
        test_net:
          ipv4_address: 172.20.0.3
      
  networks:
    test_net:
      driver: bridge
      ipam:
        driver: default
        config:
          - subnet: 172.20.0.0/16
  </code></pre>
  <p>This runs, but it takes forever for my client to print out register values to the command line. Besides being slow, my current method isn't scalable. I have to use the MODBUS TCP protocol for communications, which is why the python code uses pyModbusTCP. But there has to be a way to have 10 client and server containers talking to each other without having to specify an IP address for each one?</p>
  <p>I've looked into docker swarm, but I couldn't see how it would help my problem about assigning static IP addresses for each container. I've also tried to add an environmental variable where docker gets the IP address of the server and passes it into the client.py code, but I couldn't get it to work correctly. I'm very stuck, and any help would be greatly appreciated. Thank you so much for your time.</p>
  <p>Edit: Its incredibly slow means when I run &quot;docker-compose up&quot;, I get a notification &quot;Attaching to server1_con, client1_con&quot; but then it takes around 10 minutes for my command line to start showing print outs.</p>