# ConnectionRefusedError: [Errno 111] Connection refused

- **ID:** `python/pytest-docker-compose-not-ready`
- **Domain:** python
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (98% success)
   ```
   In docker-compose.yml, add healthcheck to the service and use depends_on with condition: service_healthy.
   ```
2. **** (95% success)
   ```
   pip install pytest-docker
Then use the docker_services fixture to wait for ports.
   ```
3. **** (90% success)
   ```
   @pytest.fixture
def db_connection():
    for _ in range(30):
        try:
            conn = connect()
            break
        except ConnectionRefusedError:
            time.sleep(1)
    else:
        raise
    yield conn
    conn.close()
   ```

## Dead Ends

- **** — This is unreliable; services may take longer or shorter to start, leading to flaky tests. (80% fail)
- **** — If the service never starts, the test hangs forever. (90% fail)
- **** — This hides the issue and reduces test coverage. (95% fail)
