title: Cannot connect from python to influxdb when running in docker
body: <p>Title is not entirely accurate, I'm open for suggestions!</p>
  
  <p>You can find a full MCVE here: <a href="https://github.com/timstoop/20170614-python-docker-influxdb-problem" rel="nofollow noreferrer">https://github.com/timstoop/20170614-python-docker-influxdb-problem</a></p>
  
  <p>Tested with:</p>
  
  <ul>
  <li>docker-engine 17.05.0~ce-0~ubuntu-xenial</li>
  <li>docker-compose version 1.13.0, build 1719ceb</li>
  </ul>
  
  <p>So the docker-compose starts an influxdb and a small python3 app. The only thing the app does is try to connect to influxdb, run a command and if that fails, wait 5 seconds and try again. If I run <code>docker-compose up</code> on this code, the app will never connect, it keeps retrying. These are the log lines it outputs:</p>
  
  <pre><code>app_1       | 2017-06-14 18:57:36.892955 ticker Trying InfluxDB connection...
  app_1       | 2017-06-14 18:57:36.892977 ticker Influx host: 'influxdb'
  app_1       | 2017-06-14 18:57:36.892984 ticker Influx port: 8086
  app_1       | 2017-06-14 18:57:36.935112 ticker No InfluxDB connection yet. Waiting 5 seconds and retrying.
  </code></pre>
  
  <p>However, if I open a shell in that specific container using <code>docker exec -ti &lt;container name&gt; /bin/sh</code>, the following works fine:</p>
  
  <pre><code>/ # python3 -u
  Python 3.6.1 (default, Jun  8 2017, 21:50:56) 
  [GCC 6.3.0] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; from influxdb import InfluxDBClient
  &gt;&gt;&gt; ci = InfluxDBClient(host='influxdb')
  &gt;&gt;&gt; ci.get_list_database()
  [{'name': '_internal'}]
  &gt;&gt;&gt;
  </code></pre>
  
  <p>I'm sure I'm overlooking something silly, but I'm unable to explain why the <code>app.py</code> won't make a connection. And thus, I'm unable to fix that connection. Any advise here would be greatly appreciated.</p>
  
  <p>My code, for reference, follows below.</p>
  
  <p>app.py:</p>
  
  <pre><code>#!/usr/bin/env python
  from influxdb import InfluxDBClient
  import datetime
  import sys
  import time
  import os
  import requests
  
  
  def output(msg):
      # Convenience function to always show a correct output
      now = datetime.datetime.now()
      print("%s ticker %s" % (now, msg))
  
  
  if __name__ == '__main__':
      # Gather our settings
      influx_host = os.getenv('INFLUX_HOST', 'localhost')
      influx_port = os.getenv('INFLUX_PORT', '8086')
      influx_user = os.getenv('INFLUX_USER', 'root')
      influx_pass = os.getenv('INFLUX_PASS', 'root')
      # Create our connections
      # Check to make sure we can create a connection
      got_if_connection = False
      while not got_if_connection:
          output('Trying InfluxDB connection...')
          output("Influx host: %s" % influx_host)
          output("Influx port: %s" % influx_port)
          influx_client = InfluxDBClient(host=influx_host, port=influx_port,
                                         username=influx_user,
                                         password=influx_pass)
          try:
              influx_client.get_list_database()
          except requests.exceptions.ConnectionError:
              output('No InfluxDB connection yet. Waiting 5 seconds and '+
                     'retrying.')
              time.sleep(5)
          else:
              got_if_connection = True
  </code></pre>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM python:alpine3.6
  MAINTAINER Tim Stoop &lt;tim@kumina.nl&gt;
  
  # Copy the script in
  COPY app.py /app.py
  COPY requirements.txt /requirements.txt
  
  # Install dependencies
  RUN pip install -r /requirements.txt
  
  
  CMD ["python3", "-u", "/app.py"]
  </code></pre>
  
  <p>docker-compose.yml:</p>
  
  <pre><code>version: '2'
  services:
    influxdb:
      image: "influxdb:alpine"
      ports:
        - "8086:8086"
    app:
      build: .
      links:
        - influxdb
      environment:
        - INFLUX_HOST='influxdb'
  </code></pre>
  
  <p>Please let me know if you need any additional information!</p>