title: Python ConnectionRefusedError: [Errno 111] Connection refused on Docker container
body: <p>I have a simple Python script, that creates a Docker container of choice, and then creates a TCP tunnel that forwards requests to it. I use sockets within Python to forward the data sent in between the client and Docker container.</p>
  <p>My piece of code that is not working:</p>
  <pre class="lang-py prettyprint-override"><code># open socket to container
  socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  socket.connect((socket.gethostbyname('container IP - e.g. 172.17.0.2'), 2222))
  </code></pre>
  <p>I tried this part of the code also like this: <code>socket.connect(('172.17.0.2', 2222))</code> and like it is shown in the code example. Only difference is that in my real code, I have the IP pulled from the container like so:</p>
  <pre class="lang-py prettyprint-override"><code>import docker
  _DOCKER_CLIENT = docker.from_env()
  c = _DOCKER_CLIENT.containers.run(
      image='linuxserver/openssh-server:latest',
      auto_remove=True,
      detach=True
  )
  
  # wait for container to start
  cc = _DOCKER_CLIENT.containers.get(c.id)
  
  # get container IP address
  ip = cc.attrs['NetworkSettings']['IPAddress']
  </code></pre>
  <p>Error I get when I try to connect to the Docker container from the socket in Python:</p>
  <pre><code>ConnectionRefusedError: [Errno 111] Connection refused
  </code></pre>
  <p>Then, I tried to do the same thing from the Python cli in a standard shell, so I launched this in bash:</p>
  <pre class="lang-sh prettyprint-override"><code>~$ python3
  &gt;&gt;&gt; import socket
  &gt;&gt;&gt; socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  &gt;&gt;&gt; socket.connect(('172.17.0.2', 2222))
  
  ----- no issues up until this point -----
  
  &gt;&gt;&gt; buffer = socket.recv(0x400)
  &gt;&gt;&gt; print(buffer)
  SSH-2.0-OpenSSH_8.3
  
  &gt;&gt;&gt; quit()
  </code></pre>
  <p>As you can see, I can do this without issues in bash, but, when I launch the Python script as the same user, I get the <code>Connection refused</code> error.</p>
  <p>The routing table is also correct:</p>
  <pre><code>Kernel IP routing table
  Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  default         _gateway        0.0.0.0         UG    600    0        0 wlp0s20f3
  link-local      0.0.0.0         255.255.0.0     U     1000   0        0 virbr1
  172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
  172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-7ee23b92049c
  192.168.0.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp0s20f3
  192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 virbr2
  192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr1
  192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
  </code></pre>
  <p>I can also ping the container without issues.</p>
  <p>Anyone, can you please spot the issue? Is Python working differently in cli mode than in standard interpreting mode in a script?</p>