Search code examples
postgresqldocker

Docker wait for postgresql to be running


I am using postgresql with django in my project. I've got them in different containers and the problem is that i need to wait for postgres before running django. At this time i am doing it with sleep 5 in command.sh file for django container. I also found that netcat can do the trick but I would prefer way without additional packages. curl and wget can't do this because they do not support postgres protocol. Is there a way to do it?


Solution

  • Problem with your solution tiziano is that curl is not installed by default and i wanted to avoid installing additional stuff. Anyway i did what bereal said. Here is the script if anyone would need it.

    import socket
    import time
    import os
    
    port = int(os.environ["DB_PORT"]) # 5432
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while True:
        try:
            s.connect(('myproject-db', port))
            s.close()
            break
        except socket.error as ex:
            time.sleep(0.1)