title: How to connect Docker web app container to Docker PostgreSQL container?
body: <p>I'm practicing making a Golang web app that interacts with a PostgreSQL database, each running on their own container.</p>
  
  <p>I'm running the containers with <code>docker-compose up</code></p>
  
  <p>But I seem to be failing on getting the postgres container properly set up.</p>
  
  <p>For brevity, links to <code>Dockerfile</code>s and other settings files are <a href="https://gist.github.com/andradei/922e11f469aec277ab6e" rel="nofollow">on this gist</a> (let me know if you want it here instead).</p>
  
  <pre><code>version: '2'
  services:
    web_app:
      build: dockerfiles/web_app
      ports:
        - "9000:9000"
      volumes:
        - .:/go/src/gitlab.com/repo/web_app
      # links might be replaced by depends_on.
      # links:
      #   - db
      depends_on:
        - db
      # tty and stdin_open cause docker-compose to disconnect from docker-machine after 60sec.
      # A fix is on the way.
      # tty: true
      # stdin_open: true
    db:
      build: dockerfiles/db
      volumes:
        - data:/var/lib/postgresql/data
  volumes:
    data: {}
  </code></pre>
  
  <p><code>docker-compose up</code> works fine. But when the application tries to open a database connection with:</p>
  
  <pre><code>var pgConf string = "user=web_app dbname=web_app sslmode=verify-full password=password"
  
  db, err := sql.Open("postgres", pgConf)
  </code></pre>
  
  <p>I get the following error from docker compose:</p>
  
  <pre><code>Error creating new user:  dial tcp [::1]:5432: getsockopt: connection refused
  </code></pre>
  
  <p>What can I do to make both containers talk to each other?</p>
  
  <p>Thank you in advance.</p>