Testcontainers Redis

raw JSON →
0.0.1rc1 verified Fri May 01 auth: no python

Redis module for testcontainers-python, providing lightweight, throwaway Redis instances for integration tests. Current version is 0.0.1rc1. Part of the testcontainers-python ecosystem (v4.x). Release cadence follows the main testcontainers-python library (approximately monthly).

pip install testcontainers-redis
error ModuleNotFoundError: No module named 'testcontainers'
cause testcontainers core is missing; testcontainers-redis does not automatically pull testcontainers.
fix
pip install testcontainers or pip install testcontainers-redis (which pulls core).
error AttributeError: module 'testcontainers' has no attribute 'redis'
cause Incorrect import path. testcontainers-redis provides 'from testcontainers.redis import RedisContainer', not 'import testcontainers.redis'.
fix
Use 'from testcontainers.redis import RedisContainer'.
error redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
cause Attempting to connect to localhost instead of the container's host IP. The container is not exposed on localhost by default.
fix
Use the container's host and port: host = redis_container.get_container_host_ip(); port = redis_container.get_exposed_port(6379).
error docker.errors.DockerException: Error while fetching server API version
cause Docker is not running or the Python docker client is misconfigured.
fix
Start Docker daemon and ensure the user has permissions (e.g., on Linux, add user to docker group).
breaking RedisContainer.get_container_host_ip() returns the host IP, not 'localhost', when using default Docker bridge network. Use it for correct connectivity.
fix Always use get_container_host_ip() and get_exposed_port() instead of hardcoding host/port.
gotcha The module is at version 0.0.1rc1 and may have breaking changes before a stable release. Pin to exact version in production tests.
fix Pin to 'testcontainers-redis==0.0.1rc1' in your requirements.
deprecated The @deprecated decorator usage in testcontainers-python core means some wait strategies or container methods may be deprecated. Use the context manager pattern (with statement) for resource cleanup.
fix Use 'with RedisContainer() as container:' instead of manual start/stop.
gotcha Container may not be ready immediately; wait strategies are not automatically configured for Redis. Use redis.Redis() with retries or a custom wait strategy.
fix Add a loop with retries on redis.exceptions.ConnectionError after starting the container.

Creates a Redis container, connects using redis-py, and verifies basic set/get.

from testcontainers.redis import RedisContainer
import redis

with RedisContainer() as redis_container:
    client = redis.StrictRedis(
        host=redis_container.get_container_host_ip(),
        port=redis_container.get_exposed_port(6379)
    )
    client.set("test", "value")
    assert client.get("test") == b"value"