title: making docker containers communicate through port
body: <p>Im trying to create 2 docker containers and run them in a way they can communicate with each other through a port in localhost. I have created 2 python files as sender and receiver. When Im running them without docker they communicate nicely. But with docker they are not running properly.</p>
  
  <h2>Sender</h2>
  
  <h3>Python script</h3>
  
  <pre><code>#!/usr/bin/python
  # -*- encoding: utf-8 -*-
  import socket
  import time
  import sys
  
  print sys.argv[1]
  print sys.argv[2]
  
  
  for i in range(1,10):
      time.sleep(2)
      data = "My parameters that I want to share with the server on ith iteration %d" % (i)
      print "sedning data: %d" % (i)
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect((sys.argv[1], int(sys.argv[2])))
      sock.sendall(data)
      sock.close()
  </code></pre>
  
  <h3>Dockerfile</h3>
  
  <pre><code>FROM ubuntu
  
  RUN \
    apt-get update &amp;&amp; \
    apt-get install -y python python-dev python-pip python-virtualenv &amp;&amp; \
    rm -rf /var/lib/apt/lists/*
  
  ADD script.py /root/script.py
  
  CMD python -u /root/script.py $SEND_HOST $SEND_PORT
  </code></pre>
  
  <h2>Receiver</h2>
  
  <h3>Python script</h3>
  
  <pre><code>#!/usr/bin/python
  # -*- encoding: utf-8 -*-
  import socket
  import sys
  
  
  
  print sys.argv[1]
  print sys.argv[2]
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((sys.argv[1], int(sys.argv[2])))
  s.listen(3)
  
  while True:
      conn, addr = s.accept()
      data = conn.recv(1024)
      conn.close()
      print "received data from sender: %s" % (data)
  </code></pre>
  
  <h3>Dockerfile</h3>
  
  <pre><code>FROM ubuntu
  
  RUN \
    apt-get update &amp;&amp; \
    apt-get install -y python python-dev python-pip python-virtualenv &amp;&amp; \
    rm -rf /var/lib/apt/lists/*
  
  ADD script.py /root/script.py
  
  CMD python -u /root/script.py $LISTEN_HOST $LISTEN_PORT
  </code></pre>
  
  <p>Run command for Receiver</p>
  
  <pre><code>docker run --name="testListen" -p 5555:5555 --env LISTEN_HOST="localhost" --env LISTEN_PORT="5555" docker.io/ayonnayihan/sample-sendr-rcv-test:receiver0.1
  </code></pre>
  
  <p>Run command for Sender</p>
  
  <pre><code>docker run --name="testTalk" --env SEND_HOST="localhost" --env SEND_PORT="5555" docker.io/ayonnayihan/sample-sendr-rcv-test:sender0.1
  </code></pre>
  
  <p>Before running the containers I made sure that both images are built. Can anyone tell why its not running properly?</p>
  
  <p>Here is the simple python running command that works just fine without docker: </p>
  
  <p>on receiver <code>python script.py localhost 5555</code></p>
  
  <p>on sender <code>python script.py localhost 5555</code></p>