{
  "id": "python/pytest-docker-compose-not-ready",
  "signature": "ConnectionRefusedError: [Errno 111] Connection refused",
  "signature_zh": "ConnectionRefusedError: [Errno 111] 连接被拒绝",
  "regex": "ConnectionRefusedError:\\ \\[Errno\\ 111\\]\\ Connection\\ refused",
  "domain": "python",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "A test tries to connect to a service (e.g., database, Redis) that is not yet ready. This often happens in CI when using docker-compose without waiting for services to be healthy.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试尝试连接尚未准备好的服务（例如数据库、Redis）。这通常发生在 CI 中使用 docker-compose 而没有等待服务健康时。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This is unreliable; services may take longer or shorter to start, leading to flaky tests.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the service never starts, the test hangs forever.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This hides the issue and reduces test coverage.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "In docker-compose.yml, add healthcheck to the service and use depends_on with condition: service_healthy.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "pip install pytest-docker\nThen use the docker_services fixture to wait for ports.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "@pytest.fixture\ndef db_connection():\n    for _ in range(30):\n        try:\n            conn = connect()\n            break\n        except ConnectionRefusedError:\n            time.sleep(1)\n    else:\n        raise\n    yield conn\n    conn.close()",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2026-01-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}