Middle · Docker and Kubernetes
Go, MongoDB and Redis in Compose
Why can a container not reach MongoDB at localhost:27017?
Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.
Reveal a strong answer
Inside a container, localhost normally refers to that container, not another service or the host. Use the configured service DNS name and internal port on the shared network; published host ports serve a different path.
Startup ordering does not mean dependency readiness. Implement bounded connection/readiness checks and retry only according to the operation policy. Development data volumes and credentials should be explicit and isolated.
Keep database and Redis ports private unless intentionally exposed, use test data and secure credentials, and include health checks that test the intended readiness. Production requires backups, resource budgets and access controls beyond a Compose demo.
Common trap
depends_on alone is not proof that a database is ready for the first query.
Try it
The Go container uses mongodb://localhost:27017 and Redis localhost:6379. What do you replace?
Worked solution
Use the actual Compose service names, for example mongodb://mongo:27017 and redis:6379, within the shared network. Keep authentication and TLS settings appropriate to that environment and wait with bounded readiness behavior.
Change the constraint
How would you prevent a developer integration test from using a production connection string?
How did you do?
Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.
Not marked. Progress stays on this browser, separate from course completion.
Primary references
Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.