From 8cf63e951c2874919b3b8854e5fa5df9495227f9 Mon Sep 17 00:00:00 2001 From: sireeshajonnalagadda Date: Mon, 16 Mar 2026 10:41:10 +0000 Subject: [PATCH] Refactor Docker networking and health checks in docker-compose.yml --- .../.devcontainer/docker-compose.yml | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/go-postgres/.devcontainer/docker-compose.yml b/src/go-postgres/.devcontainer/docker-compose.yml index 071a9398..05b90500 100644 --- a/src/go-postgres/.devcontainer/docker-compose.yml +++ b/src/go-postgres/.devcontainer/docker-compose.yml @@ -18,8 +18,17 @@ services: # Overrides default command so things don't shut down after the process ends. command: sleep infinity - # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. - network_mode: service:db + # Database connection string for Rails applications + # Provides a standard way to connect to the PostgreSQL database + environment: + DATABASE_URL: "postgresql://postgres:postgres@db:5432/postgres" + + # Use proper Docker networking (not network_mode) to avoid namespace issues and ensure reliable container startup across environments + depends_on: + db: + condition: service_healthy # Wait for PostgreSQL to be ready before starting the app + networks: + - app-network # Connect to custom bridge network for service communication # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. # (Adding the "ports" property to this file will not forward from a Codespace.) @@ -27,8 +36,26 @@ services: db: image: postgres:latest restart: unless-stopped + networks: + - app-network # Connect to the same network as the app container + + # Health check ensures PostgreSQL is ready before dependent services start + # This is critical for depends_on with condition: service_healthy to work properly + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U postgres" ] + interval: 10s # Check every 10 seconds + timeout: 5s # Wait up to 5 seconds for response + retries: 5 # Retry 5 times before marking as unhealthy + start_period: 30s # Give PostgreSQL 30 seconds to start before health checks begin + volumes: - - postgres-data:/var/lib/postgresql/data + - postgres-data:/var/lib/postgresql/ + # Custom bridge network for service communication + # Replaces the problematic network_mode: service:db approach + networks: + app-network: + driver: bridge # Standard bridge driver for container-to-container communication + # Services on this network can communicate using service names (app -> db) env_file: # Ensure that the variables in .env match the same variables in devcontainer.json - .env