Back to directory
testcontainers avatar

testcontainers-python

Testcontainers is a Python library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/testcontainers/testcontainers-python.git

2,279

Stars

379

Forks

18

Watchers

Apache-2.0

License

uv Ruff PyPI - Version PyPI - License PyPI - Python Version codecov Core Tests Community Tests Docs

Codespace

Testcontainers Python

testcontainers-python facilitates the use of Docker containers for functional and integration testing.

For more information, see the docs.

Getting Started

>>> from testcontainers.postgres import PostgresContainer
>>> import sqlalchemy

>>> with PostgresContainer("postgres:16") as postgres:
...     engine = sqlalchemy.create_engine(postgres.get_connection_url())
...     with engine.begin() as connection:
...         result = connection.execute(sqlalchemy.text("select version()"))
...         version, = result.fetchone()
>>> version
'PostgreSQL 16...'

The snippet above will spin up a postgres database in a container. The get_connection_url() convenience method returns a sqlalchemy compatible url we use to connect to the database and retrieve the database version.

Contributing / Development / Release

See CONTRIBUTING.md for more details.

Configuration

You can set environment variables to configure the library behaviour:

Env Variable Example Description
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE /var/run/docker.sock Path to Docker's socket used by ryuk
TESTCONTAINERS_RYUK_PRIVILEGED false Run ryuk as a privileged container
TESTCONTAINERS_RYUK_DISABLED false Disable ryuk
RYUK_CONTAINER_IMAGE testcontainers/ryuk:0.8.1 Custom image for ryuk
RYUK_RECONNECTION_TIMEOUT 10s Reconnection timeout for Ryuk TCP socket before Ryuk reaps all dangling containers

Alternatively you can set the configuration during runtime:

from testcontainers.core import testcontainers_config

testcontainers_config.ryuk_docker_socket = "/home/user/docker.sock"

Releases

Jul 24, 2026

testcontainers: v4.15.0

Download .zip

4.15.0 (2026-07-24) Bug Fixes

cratedb: add CrateDB community module (#1051) (0976c7e)

Jun 11, 2026

testcontainers: v4.15.0-rc4

Download .zip

4.15.0-rc4 (2026-06-11) Features

extended configuration options for container.exec method (#1050) (7dee471) main: enable typing for complete package (237be27) main: make legacy imports available with...

Jun 3, 2026

testcontainers: v4.15.0-rc3

Download .zip

4.15.0-rc3 (2026-06-03) Features

core: add Podman compatibility (#1028) (37093ab) core: auto-detect DOCKER_HOST from current docker context (#1026) (d4b8ff6), closes #1025

Bug Fixes

core: add conta...

Apr 30, 2026

testcontainers: v4.15.0-rc2

Download .zip

4.15.0-rc2 (2026-04-30) Features

core: support TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX (#961) (be9a0a6) mongodb: Add Atlas Local for MongoDb (#873) (73aeb43) support with_copy_to (#976) (59ec1ce) valkey...

Apr 7, 2026

testcontainers: v4.15.0-rc.1

Download .zip

4.15.0-rc.1 (2026-04-07) Features

compose: add structured container inspect information (#897) (58459a1) core: support SSH-based DOCKER_HOST (#993) (d48115d) generic: Reintroducing the generic SQL mo...