315
@integral_ines ·

Reverse proxy returns 502 for one container after every host reboot Networking

Running about a dozen containers behind a reverse proxy. After every host reboot, eleven of them come up fine and one returns 502 until I manually restart that container, after which it works for weeks. The container itself reports healthy in docker ps even while the proxy is returning 502. Proxy config is identical in shape to the others, just a different upstream port. Nothing useful in the proxy logs beyond the 502 itself. What is the usual cause of this pattern?

7 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @chmod_confused · 4w ago · 3 replies

    Almost always a DNS caching issue in the proxy combined with startup ordering. The proxy resolves the container name to an IP once at startup, the container gets a different IP on the reboot because it came up in a different order, and the proxy keeps talking to an address that no longer exists. Restarting the container makes it re-register and often gets the old IP back, which is why the manual fix looks like it fixes the container. The real fix is to make the proxy re-resolve: use a variable for the upstream so it resolves per request, or pin the container to a static IP or a user-defined network with a stable alias.

    288
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @chmod_confused · 4w ago

      Exactly the mechanism. Add a depends_on with a health condition so the proxy starts last, and use the resolver approach so ordering stops mattering at all. Belt and braces because reboots are the one time you are not watching.

      112
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @integral_ines · 4w ago

      That explains why it is always the same container: it is the one whose start is slowest because it waits on a database. Presumably it comes up after the proxy every single time.

      98
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @quietpackets · 4w ago · 2 replies

    Worth confirming before you change anything: while it is 502ing, exec into the proxy container and curl the upstream by name and by IP. If name fails and IP works, it is DNS. If both fail, the app is not actually listening yet regardless of what the health check says.

    196
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @stdlib_stef · 4w ago

      This is the right first move. A healthy container in docker ps only means the process is running, not that the app has finished starting, unless you wrote a real health check.

      84
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @flakey_test · 4w ago

    Check whether that container is the one with a long migration on startup. I had this with an app that ran schema migrations for 90 seconds and answered on the port immediately but returned nothing usable.

    148
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @epoxy_puddle · 4w ago

    If you want a blunt fix while you sort out the real one, a restart policy plus a small startup delay on the proxy service costs nothing and takes five minutes.

    92
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report