Errors when connecting postgres to Django

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'

    image: postgres
    build: .
    command: python3 runserver
      - .:/code
      - "8000:8000"
      - 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" ( 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 "", 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 hack and replace the command line with this:

    command: ["./", "db:5432", "--", "python3", "", "runserver", ""]

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 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!

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.

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?

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

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

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

Ok, good to know. Thanks!

I was able to fix this issue simply building my db container, wait few seconds, then building the web container:
docker-compose up -d --build db
*wait a few seconds*
docker-compose up -d --build web

I hope this helps

Please can you share your source code?

For an automated solution, see Control startup and shutdown order in Compose | Docker Documentation (And even when using or your own wrapper like described there, also note its “design your application to attempt to re-establish a connection to the database after a failure”.)