title: Can`t connect to postgres in docker container outside of Python shell
body: <p>I can`t connect to Postgres image in Docker container outside Python shell.</p>
  
  <p>I have a script, which downloads and rolls out Postgres Docker image. One of the steps is that I would like to create some tables within the database.</p>
  
  <p>If I run the <code>setup.py</code>, it will download the image and install Docker container on the local machine and wait for a connection. If next I will open a Python shell and try to connect to DB - everything works fine.</p>
  
  <p>My <code>Dockerfile</code>:</p>
  
  <pre><code>FROM postgres:11.1
  RUN apt-get update
  EXPOSE 5432
  ENV POSTGRES_DB monitoring
  ENV POSTGRES_USER process_monitor
  </code></pre>
  
  <p>My <code>setup.py</code> file:</p>
  
  <pre class="lang-py prettyprint-override"><code>import subprocess
  import psycopg2
  
  
  def update_apt():
      subprocess.run(['sudo', 'apt-get', '-y', 'update'])
  
  def install_docker():
      subprocess.run(['sudo', 'apt-get', '-y', 'install', 'docker.io'])
  
  def install_docker_image():
      subprocess.run(['sudo','docker', 'build', '-t', 'postgres-image', '.'])
  
  def docker_run():
      subprocess.run(['sudo', 'docker', 'run', '-p', '5432:5432', '-v', '/var/run/postgresql:/var/run/postgresql', '-d', 'postgres-image'])
  
  
  if __name__ == "__main__":
      update_apt()
      install_docker()
      install_docker_image()
      docker_run()
  </code></pre>
  
  <p>Here is what I have in cli when I run this:</p>
  
  <pre class="lang-sh prettyprint-override"><code>Successfully built d7a66c60b43e
  Successfully tagged postgres-image:latest
  2d1c667a84513943dad359b73962b6314590b1bf79ab4376aa7b513684629e5f
  
  --- Successul built and ran container ---
  
  (venv) simon@simon-HP-EliteBook-840-G2:~/Desktop/Projects/x5-test-task$ sudo docker ps
  CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
  2d1c667a8451        postgres-image      "docker-entrypoint.s…"   7 seconds ago       Up 5 seconds        0.0.0.0:5432-&gt;5432/tcp   loving_shtern
  
  -- Check if it works ----
  
  (venv) simon@simon-HP-EliteBook-840-G2:~/Desktop/Projects/x5-test-task$ python3
  Python 3.6.7 (default, Oct 22 2018, 11:32:17)
  [GCC 8.2.0] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import psycopg2
  &gt;&gt;&gt; conn = psycopg2.connect(dbname='monitoring', user='process_monitor')
  &gt;&gt;&gt; cur = conn.cursor()
  &gt;&gt;&gt; cur.execute('SELECT datname FROM pg_database')
  &gt;&gt;&gt; cur.fetchall()
  [('postgres',), ('monitoring',), ('template1',), ('template0',)]
  &gt;&gt;&gt; exit()
  
  --- as you can see, there is a monitoring database I need ---
  
  </code></pre>
  
  <p>See, the successful connection from Python shell.</p>
  
  <p>But when I tried to add the same code I checked in shell, i get the error:</p>
  
  <pre class="lang-py prettyprint-override"><code>if __name__ == "__main__":
      update_apt()
      install_docker()
      install_docker_image()
      docker_run()
      conn = psycopg2.connect(dbname='monitoring', user='process_monitor')
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code>Traceback (most recent call last):
    File "setup.py", line 58, in &lt;module&gt;
      conn = psycopg2.connect(dbname='monitoring', user='process_monitor')
    File "/home/simon/Desktop/Projects/x5-test-task/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  psycopg2.OperationalError: could not connect to server: No such file or directory
          Is the server running locally and accepting
          connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
  </code></pre>
  
  <p>Can't figure out, why I can`t connect from the same script. Should I add some sleep time?</p>
  
  <p><strong>Edit1</strong>: it seems lite it is the problem of timing. Tried <code>sleep(60)</code> successfully connected.</p>