Docker Community Forums

Share and learn in the Docker community.

Errors when connecting postgres to Django

docker

(Benlindsay) #1

I’m following along with this Docker/Django/Postgres quickstart tutorial and there’s a recurring issue with starting up the Postgres image. If I have my docker-compose.yml file the way the tutorial has it, i.e.

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

then the web process starts kicking in before the database is set up, and I get an error message like this:

web_1  | 	Is the server running on host "db" (172.19.0.2) and accepting
web_1  | 	TCP/IP connections on port 5432?

followed by errors in the db process that look like this:

db_1   | waiting for server to start....2018-01-31 04:42:44.584 UTC [37] LOG:  listening on IPv4 address "127.0.0.1", port 5432
db_1   | 2018-01-31 04:42:44.584 UTC [37] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
db_1   | 2018-01-31 04:42:44.584 UTC [37] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
db_1   | 2018-01-31 04:42:44.588 UTC [37] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-31 04:42:44.601 UTC [38] LOG:  database system was shut down at 2018-01-31 04:42:43 UTC
db_1   | 2018-01-31 04:42:44.608 UTC [37] LOG:  database system is ready to accept connections
db_1   |  done

and my app doesn’t work. If I use the wait-for-it.sh hack and replace the command line with this:

    command: ["./wait-for-it.sh", "db:5432", "--", "python3", "manage.py", "runserver", "0.0.0.0:8000"]

then the web process actually waits for the db process, but I still get the same db error initially, and it goes away when the process is restarted, so the app ends up working.

The wait-for-it.sh thing feels really hacky, and I don’t feel like I should be seeing any of these error messages. How should I go about getting rid of those? I’m on a Mid 2012 Macbook Pro on OSX version 10.12.1 and Docker Version 17.12.0-ce-mac49 (21995) if that’s important.

Thanks in advance!


(Sam) #2

i don’t see anything ‘hacky’ about the problem… it is a real world issue if you start the db server everytime you start a using container.

the original poster of the quickstart didn’t experience the problem, so didn’t document it.

docker-compose depends_on: only confirms the container is running, not what is inside.


(Benlindsay) #3

That’s good to know. I should have also made it clear that the database errors occur even if I don’t run the web service. For example, if I just run docker-compose up db, then I still see those db_1 errors. To me that seems like there’s something more deeply wrong with either my system or how I run the db. Any thoughts on that?


(Sam) #4

those are configuration and dirty shutdown problems… (cannot connect::1, and shutdown on…)


(Benlindsay) #5

Ok, cool. Do you know how to fix/prevent those or where I could read more about fixing/preventing those?


(Sam) #6

i do not, and these are not docker related specifically. but google is your friend.


(Benlindsay) #7

Ok, good to know. Thanks!