Hardcoded localhost breaks Docker app — service-name DNS is the fix
A developer building TestFlow Agent, a three-service side project, discovered that a 'Run Enrollment Flow' button worked perfectly in native local development but consistently failed after the app was containerised with Docker Compose. The root cause was a hardcoded 'localhost:4000' URL in the backend code, which correctly pointed to the mock API on a single machine but resolved to the container itself once Dockerised, finding nothing on that port. Inside Docker Compose, containers communicate via service names on an internal network rather than through localhost, so the backend was effectively calling itself and receiving no response. The fix involved making the target URL configurable via an environment variable, defaulting to 'localhost:4000' for native runs while the Compose file sets it to the mock API's service name, ensuring both environments work without code changes. The developer noted that hardcoded localhost addresses are a silent liability that only surface when a network boundary shifts, and that making such values configurable with sensible defaults is the correct long-term approach.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in