What this thread does
Do first: add a database healthcheck and make the app's depends_on condition service_healthy (startup order).
Short answer
A running database container may not be ready for SQL; gate the app on health (startup). Connect services by name on the container port (networking). Avoid unnecessary host publication and supply environment values explicitly (ports, environment). Memory limits need workload-specific sizing (constraints).
Choose / don't choose
service_healthy: choose when the app needs a ready database; simple startup order does not wait for readiness (startup).- Host port publication: choose only when host access is needed. Internal service connections use the container port (networking).
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
depends_on.db.condition | service_healthy | App waiting for database | Startup |
| Database healthcheck command | pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB} in CMD-SHELL | Source's Postgres example | Startup |
| Database address | Service name db, container port 5432 in the source example | Service-to-service traffic | Networking |
environment | Explicit mapping or list of container variables | Compose service | Environment |
| Loopback bind address | 127.0.0.1 or ::1 for host-only publication | Published ports; see older-release caveat | Ports |
Order of operations
1. Configure the database healthcheck and readiness dependency (startup). 2. Point the app at the database service name and container port, rather than the host's mapped port (networking). 3. Omit database publication if only peer services need it; when publishing for host access, bind to loopback. Docker warns that releases older than 28.0.0 can expose loopback-published ports to the same network segment (networking, ports). 4. Supply variables through environment or env_file. A bare variable name uses the shell value without warning if missing; interpolation warns when unresolved (environment). 5. Determine memory requirements before choosing a hard limit; low host memory can cause process termination (constraints).
Known failures and fixes
| Symptom | Cause | Fix | Source |
|---|---|---|---|
| App starts before SQL is available | Startup order waits for running, not ready | Gate startup on health | Startup |
| Container setting is absent | No explicit container-environment assignment | Add environment or env_file | Environment |
| Published port accepts outside traffic | Default publication uses all host addresses | Remove publication or bind to loopback, with the older-release caveat above | Ports |
Missing
- Which memory value fits this app and database? The resource guide provides no workload-specific budget.
- Does exit code 137 identify this container's root cause? These references do not establish that diagnosis.
- What volume and restore procedure preserve this database across rebuilds? Persistence and recovery are not covered by this startup recipe.
Checked
2026-11-23; sources read:
Started from a post by @WhatWorks editors, packaged by the editor.