Skip to content

Backend container loses connection to frontend after restart causing 502 Bad Gateway #206

@manavgup

Description

@manavgup

Problem Description

When restarting only the backend container using make restart-backend or docker compose restart backend, the backend loses its connection to the frontend container, resulting in a 502 Bad Gateway error when attempting to login via the frontend.

Steps to Reproduce

  1. Start the full application: make run-app or docker compose up -d
  2. Verify both frontend and backend are working properly
  3. Restart only the backend: make restart-backend or docker compose restart backend
  4. Try to login via the frontend (browser at localhost:3000)
  5. Observe 502 Bad Gateway error

Expected Behavior

Restarting the backend container should maintain proper connectivity with the frontend container. The nginx reverse proxy in the frontend should continue to properly route /api/* requests to the backend container.

Current Workaround

Currently need to restart both containers to restore connectivity:

make restart-app
# or
docker compose restart backend frontend

Technical Details

Container Setup:

  • Frontend: nginx serving React app on internal port 8080, exposed to host on port 3000
  • Backend: FastAPI on internal port 8000, exposed to host on port 8000
  • Network: Both containers on app-network

nginx Configuration in Frontend (webui/default.conf):

location /api/ {
    proxy_pass http://backend:8000;
    # ... proxy headers
}

docker-compose.yml Dependencies:

frontend:
  depends_on:
    backend:
      condition: service_healthy

Possible Root Causes

  1. nginx Connection Caching: nginx might be caching the backend connection and not detecting the backend restart
  2. Docker Network Issues: Container restart might cause network connectivity issues within app-network
  3. Health Check Timing: Frontend might not wait for backend to be fully healthy after restart
  4. nginx Upstream Configuration: Missing upstream configuration or connection pooling issues

Proposed Solutions

  1. Improve nginx Configuration: Add upstream block with proper health checking
  2. Add nginx Reload: Modify make restart-backend to reload nginx configuration in frontend
  3. Fix Container Dependencies: Ensure proper container restart order and health checking
  4. Add Connection Retry: Configure nginx to retry failed backend connections

Environment

  • Docker Compose version: Latest
  • Container setup: nginx (frontend) + FastAPI (backend)
  • Network: Custom bridge network app-network
  • Platform: macOS/Linux

Priority

Medium - Affects development workflow when debugging backend issues requiring container restarts.

Labels

  • bug
  • infrastructure
  • docker
  • nginx
  • development

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendBackend/API relatedbugSomething isn't workinginfrastructureInfrastructure and deployment

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions