Python library for throwaway instances of anything that can run in a Docker container
Project Links
Meta
Author: Sergey Pirogov
Maintainer: Balint Bartha
Requires Python: >=3.9.2,<4.0
Classifiers
Intended Audience
- Developers
- Information Technology
License
- OSI Approved :: Apache Software License
Operating System
- MacOS
- Microsoft :: Windows
- POSIX
- Unix
Programming Language
- Python :: 3
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
- Python :: 3.14
- Python :: 3.9
Topic
- Software Development :: Libraries :: Python Modules
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.13.2
Oct 07, 2025
4.13.1
Sep 24, 2025
4.13.0
Sep 09, 2025
4.12.0
Jul 21, 2025
4.11.0
Jun 16, 2025
4.10.0
Apr 02, 2025
4.9.2
Mar 05, 2025
4.9.1
Jan 21, 2025
4.9.0
Dec 10, 2024
4.8.2
Oct 14, 2024
4.8.1
Sep 03, 2024
4.8.0
Aug 14, 2024
4.7.2
Jul 15, 2024
4.7.1
Jul 02, 2024
4.7.0
Jun 28, 2024
4.6.0
Jun 20, 2024
4.5.1
May 31, 2024
4.5.0
May 25, 2024
4.4.1
May 14, 2024
4.4.0
Apr 17, 2024
4.3.3
Apr 09, 2024
4.3.2
Apr 08, 2024
4.3.1
Apr 02, 2024
4.3.1rc1
Apr 02, 2024
4.3.0
Apr 01, 2024
4.2.0
Mar 24, 2024
4.2.0rc2
Mar 24, 2024
4.2.0rc1
Mar 23, 2024
4.1.2
Mar 24, 2024
4.1.1
Mar 20, 2024
4.1.0
Mar 11, 2024
4.0.1
Mar 11, 2024
4.0.0
Mar 06, 2024
4.0.0rc2
Jan 23, 2024
4.0.0rc1
Jan 06, 2023
3.7.1
Dec 06, 2022
3.7.0
Sep 21, 2022
3.6.1
Aug 15, 2022
3.6.0
Jun 14, 2022
3.5.4
May 13, 2022
3.5.3
Apr 04, 2022
3.5.2
Apr 03, 2022
3.5.1
Apr 03, 2022
3.5.0
Mar 30, 2022
3.4.2
Aug 15, 2021
3.4.1
Jun 03, 2021
3.4.0
Mar 29, 2021
3.3.0
Mar 06, 2021
3.2.0
Dec 11, 2020
3.1.0
Aug 31, 2020
3.0.3
Jun 08, 2020
3.0.2
May 26, 2020
3.0.0
Apr 28, 2020
2.6.0
Feb 20, 2020
2.5
Jul 25, 2019
2.3
Aug 19, 2018
2.2
Feb 25, 2018
2.1.2
Jan 09, 2018
2.1.0
Oct 01, 2017
2.0.0
Mar 28, 2017
1.0.2
Sep 26, 2016
1.0.1
Sep 12, 2016
1.0
Aug 21, 2016