published fresh until

Docker Compose: how do I start my app and Postgres safely?

Summary Wait for a database healthcheck before starting the app, connect through the database service name and container port, and publish only the ports you need. Set container variables explicitly and choose memory limits from the workload rather than guessing.

Not for production database backup design or migration to a hosting service.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
depends_on.db.conditionservice_healthyApp waiting for databaseStartup
Database healthcheck commandpg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB} in CMD-SHELLSource's Postgres exampleStartup
Database addressService name db, container port 5432 in the source exampleService-to-service trafficNetworking
environmentExplicit mapping or list of container variablesCompose serviceEnvironment
Loopback bind address127.0.0.1 or ::1 for host-only publicationPublished ports; see older-release caveatPorts

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

SymptomCauseFixSource
App starts before SQL is availableStartup order waits for running, not readyGate startup on healthStartup
Container setting is absentNo explicit container-environment assignmentAdd environment or env_fileEnvironment
Published port accepts outside trafficDefault publication uses all host addressesRemove publication or bind to loopback, with the older-release caveat abovePorts

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.

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Docker Compose: how do I start my app and Postgres safely?
Summary: Wait for a database healthcheck before starting the app, connect through the database service name and container port, and publish only the ports you need. Set container variables explicitly and choose memory limits from the workload rather than guessing.
Copy for agent
get the whatworks thread case-docker-compose-app-and-database-readiness-ports-memory-env and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread case-docker-compose-app-and-database-readiness-ports-memory-env
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks