dockerdocker-composeexternalrestart

How to wait for external Docker service to be healthy before starting another container?


I have Pi-Hole and Homepage running as separate docker-compose.yml files.

When I reboot my Docker host, Homepage finishes loading before Pi-Hole. The problem is that Pi-Hole is my DNS server, and the Homepage UI uses domain name records I have set up in Pi-Hole.

Both containers load fine and show status as "healthy". However, I have to manually restart the Homepage container for it to work properly, because if Pi-Hole isn't online when Homepage starts, things don't work properly.

To be clear: the Homepage container has a status of "healthy", even when it isn't working properly.

As such, I can't use restart:on-failure, because the Homepage container is considered healthy and doesn't shut down.

Therefore, I need the Pi-Hole container to be running and marked as healthy before the Homepage container starts.

In docker-compose-homepage.yml, I included the lines:

external_links:
      - pihole

...as explained in this Stack Overflow answer, but that seems only applicable to services within the same docker compose file. Of course, the depends-on option only works with services within the same docker-compose file, so that's also not a valid solution.

Is there a way of doing this?

Thank you.


Solution

  • The solution was to use wait-for-it upon boot.

    sudo apt install wait-for-it
    crontab -e
    @reboot wait-for-it IPaddress:port -- docker compose -f /path/to/docker-compose-homepage.yml restart
    

    (where IPaddress:port is the IP address of the Docker host and the port number for Pi-Hole.)

    This means that, upon system boot, it'll run the Wait For It command, which waits until the specified port is available, and once that port is accessible, it'll then restart the specified Docker container.