Python "urllib3 NewConnectionError" in CI
urllib3.exceptions.NewConnectionError: Failed to establish a new connection: [Errno 111] Connection refused is the low-level cause behind many requests ConnectionErrors: the TCP connect itself failed.
What this error means
A traceback shows "NewConnectionError('...: Failed to establish a new connection: [Errno 111] Connection refused')" beneath a requests or urllib3 call.
python
urllib3.exceptions.NewConnectionError: <...>: Failed to establish a new connection: [Errno 111] Connection refusedCommon causes
Nothing is listening on the target host:port
A dependent service has not started, crashed, or is bound to a different port.
Wrong host inside the CI network
The code used localhost where the service runs under a different container hostname.
How to fix it
Point at the right address and wait for the listener
- Verify the host and port match where the service actually listens in CI.
- Use the service container hostname (not localhost) in container-network CI.
- Poll the port until it accepts connections before running dependent tests.
Python
# in containerized CI, use the service name, not localhost
DB_HOST = "postgres" # docker/compose service nameHow to prevent it
- On Latchkey managed runners, transient connection-refused failures during service warm-up are auto-retried.
- Use service hostnames in containerized CI networks.
- Gate connections on a port-readiness poll.
Related guides
Python "requests.exceptions.ConnectionError: Max retries exceeded" in CIFix "requests.exceptions.ConnectionError: Max retries exceeded with url" in CI - requests could not establish…
Python "httpx.ConnectError" in CIFix "httpx.ConnectError: All connection attempts failed" in CI - httpx could not open a connection to the hos…
Python "redis.exceptions.ConnectionError" in CIFix "redis.exceptions.ConnectionError: Error connecting to Redis" in CI - the redis-py client could not reach…