3

My docker-compose file

version: "2" 
services:   db:
   restart: always
   image: postgres:latest
   ports:
     - "5435:5432"
   environment:
      POSTGRES_PASSWORD: password
      POSTGRES_USER: user
      POSTGRES_DB: db   adminer:    
   web:
      image: golang:1.7
      working_dir: /go/src/app
      command: go run bot.go
      ports:
        - "3000:3000"
      volumes:
        - ./bot:/go/src/app
      links:
        - db
      environment:
      PORT: 3000
      CONNECTION_STRING_DEV: postgres://user:password@db/db

and my bot.go, where I try connect

db, err = sql.Open("postgres", "user=user password=password host=db dbname=db port=5432 sslmode=verify-full ")

When I bring up my containers, I see errors:

panic: dial tcp 5.61.14.99:5432: getsockopt: connection refused

I changed the port on 5432 and tried connect like this:

db, err = sql.Open("postgres", "postgres://user:password@db/db")

but I get the same errors

What's wrong with my docker-compose setup?

5
  • thank you, i changed code in bot.go, used os.Getenv("CONNECTION_STRING_DEV"), but it didn't solve my problem Commented Oct 6, 2017 at 9:28
  • what's the value of os.Getenv("CONNECTION_STRING_DEV")? Commented Oct 6, 2017 at 9:28
  • Did you try without ssl? postgres://user:password@db/db?sslmode=disable or user=user password=password host=db dbname=db port=5432 sslmode=disable. Commented Oct 6, 2017 at 9:46
  • yes, i tried. I think, i tried all variants )) Commented Oct 6, 2017 at 9:59
  • I think you need a bridge? stackoverflow.com/questions/43754095/… Commented Oct 6, 2017 at 12:32

2 Answers 2

6

Your docker-compose looks a little messy but that's probably from copy and pasting. It's likely that postgres is not yet up and running when Go tries to connect. To test if that's the problem, first:

docker-compose up -d db

Then wait until postgres is ready by checking:

docker-compose logs -f db

and look out for a log line like:

db_1   | LOG:  database system is ready to accept connections

When that line appears, quit the log command (Ctrl+C) and run your bot:

docker-compose up web

If it is now working, that was indeed your problem.

Solution: Wait until postgres is ready. Easy ways to achieve this are:

  • sleep for an amount of time (e.g. 1 min) before running web
  • sleep inside web before connecting
  • when connecting fails, sleep for 5 seconds and retry indefinitely

The disadvantage of these are that you don't know when postgres is ready, so you could wait too long or not long enough. A better solution is to run your bot only after a successful connection to postgres has been made.

Example from https://docs.docker.com/compose/startup-order/:

#!/bin/bash
# wait-for-postgres.sh

set -e

host="$1"
shift
cmd="$@"

until psql -h "$host" -U "postgres" -c '\l'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

Add this script as wait-for-postgres.sh and in you docker-compose.yml change the command for web like so:

command: ["./wait-for-postgres.sh", "db", "go", "run", "bot.go"]
Sign up to request clarification or add additional context in comments.

6 Comments

thank you, it very good answer, i understanded how i can test my containers. I run "db" container, and then container "web", but see same error, then i add in code timeout, but it not work. So it may be error with pg_hba.conf?
After running db, did you wait for postgres to be ready? I don't think it's a configuration issue, because the error message indicates a problem at a lower level.
yes, LOG: database system is ready to accept connections and if i run sudo docker-compose ps, i see docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
but when i run sudo docker-compose up web, i see: panic: dial tcp 5.61.14.99:5432: getsockopt: connection refused
SSL is not enable on the postgres image. I can sql.Open(), but to actually do something with the database, I had to use ` db, err := sql.Open("postgres", "postgres://user:password@db/db?sslmode=disable")`.
|
1

I found answer, i ran container with postgres already in other app, i didn't think about this, because docker compose didn't show errors when build db container. I used docker ps then docker stop xxxxx and stop db container from other app, then build and up my app, and problem solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.