testcontainers

Python library for throwaway instances of anything that can run in a Docker container

71 个版本 Python >=3.10
安装
pip install testcontainers
poetry add testcontainers
pipenv install testcontainers
conda install testcontainers
描述

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"
版本列表
4.15.0rc4 2026-06-11
4.15.0rc3 2026-06-03
4.15.0rc2 2026-04-30
4.15.0rc1 2026-04-07
4.14.2 2026-03-18
4.14.1 2026-01-31
4.14.0 2026-01-07
4.13.3 2025-11-14
4.13.2 2025-10-07
4.13.1 2025-09-24
4.13.0 2025-09-09
4.12.0 2025-07-21
4.11.0 2025-06-16
4.10.0 2025-04-02
4.9.2 2025-03-05
4.9.1 2025-01-21
4.9.0 2024-12-10
4.8.2 2024-10-14
4.8.1 2024-09-03
4.8.0 2024-08-14
4.7.2 2024-07-15
4.7.1 2024-07-02
4.7.0 2024-06-28
4.6.0 2024-06-20
4.5.1 2024-05-31
4.5.0 2024-05-25
4.4.1 2024-05-14
4.4.0 2024-04-17
4.3.3 2024-04-09
4.3.2 2024-04-08
4.3.1 2024-04-02
4.3.0 2024-04-01
4.3.1rc1 2024-04-02
4.2.0 2024-03-24
4.2.0rc2 2024-03-24
4.2.0rc1 2024-03-23
4.1.2 2024-03-24
4.1.1 2024-03-20
4.1.0 2024-03-11
4.0.1 2024-03-11
4.0.0 2024-03-06
4.0.0rc2 2024-01-23
4.0.0rc1 2023-01-06
3.7.1 2022-12-06
3.7.0 2022-09-21
3.6.1 2022-08-15
3.6.0 2022-06-14
3.5.4 2022-05-13
3.5.3 2022-04-04
3.5.2 2022-04-03
3.5.1 2022-04-03
3.5.0 2022-03-30
3.4.2 2021-08-15
3.4.1 2021-06-03
3.4.0 2021-03-29
3.3.0 2021-03-06
3.2.0 2020-12-11
3.1.0 2020-08-31
3.0.3 2020-06-08
3.0.2 2020-05-26
3.0.0 2020-04-28
2.6.0 2020-02-20
2.5 2019-07-25
2.3 2018-08-19
2.2 2018-02-25
2.1.2 2018-01-09
2.1.0 2017-10-01
2.0.0 2017-03-28
1.0.2 2016-09-26
1.0.1 2016-09-12
1.0 2016-08-21