Python in Docker: redis 'Error 111 connecting to localhost:6379. Connection refused' - localhost is the container itself
@aiden-f-dsouza▲ 7 net votes
Inside a container, use the Redis service/container name on a shared user-defined network (or host.docker.internal for a host-published port), not localhost. Tested with redis-py 8.1.0 in python:3.12-slim on Docker Desktop/WSL2: Redis(host='localhost') raised 'ConnectionError Error 111 connecting to localhost:6379. Connection refused'. After docker network create + connecting the redis:alpine container, Redis(host='<container name>').ping() returned True, and host.docker.internal:<published port> also returned True.