Skip to main content

pytest-ephemeral-container

This is a bit of code to allow test suites (namely pytest) to spawn and destroy containers tied to fixtures.

This uses the Docker Python library, not the Docker CLI, and should be compatible with a wide variety of platforms and implementations. It should work either as the container host or with the Docker socket mounted in.

Example

For example, to create an S3 based on localstack:

from pytest_ephemeral_container import spawn_container, discover_ports, wait_for_http


@pytest.fixture(scope="session")
def _localstack_s3():
    with spawn_container(
        image="docker.io/localstack/localstack:s3-latest",
        ports={"4566/tcp": None},
        environment={
            "DEBUG": "0",
        },
    ) as container:
        ip, port = next(discover_ports(container, "4566/tcp"))

        wait_for_http(ip, port)

        yield f"{ip}:{port}"

Reference

Jamie doesn't feel like doing a full docs site yet, so have quick reference.

There are features of the library not discussed here; read the source for more information.

def spawn_container(**props) -> ContextManager[Container]

Start a container, as a context manager. Cleans itself up.

props are the container properties passed to client.containers.run(). The image parameter is required, and you probably want to pass a ports (see discover_ports()). name is left to be autogenerated by Docker, and you probably want to keep that.

def discover_ports(container: Container, port: str) -> typing.Iterable[tuple[str, int]]

Docker has the option to automatically pick ports for you. This is very valuable in the context of ephemeral containers, since you can avoid conflicting with anything else the user is running.

This function will find the ports that Docker picked, and tell you about them.

In most cases, the first port given is fine, so you can wrap this in next().

def wait_for_http(addr: str, port: int, *, timeout: float = 30.0) -> None

Uses urllib to repeatedly make HTTP requests at the given port until success.

timeout is for the total wait time.

pytest fixture dockerclient ()

Gets the Docker client that is in use, in case you want to do anything advanced.

Metadata

Release files for pytest-ephemeral-container 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-ephemeral-container 0.3.0
File Size Uploaded
pytest_ephemeral_container-0.3.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-ephemeral-container 0.3.0
File Interpreter ABI Platform
pytest_ephemeral_container-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.9 kB

Release files / pytest_ephemeral_container-0.3.0.tar.gz

Download URL pytest_ephemeral_container-0.3.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
49c78f536b7e58897957330fb0377a91c80f345d80ddae62af2af36b0520208a
BLAKE2b-256 checksum
How to use checksums
467e5e42d0454243dbaa11586349582d11d867d4dc70cae359db2d2ed434cf24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.1 CPython/3.14.3 Linux/6.19.11-200.fc43.x86_64

Release files / pytest_ephemeral_container-0.3.0-py3-none-any.whl

Download URL pytest_ephemeral_container-0.3.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
961e5905d773b9e0437219de02f7dc177e635c51d99bf319050f34446946774e
BLAKE2b-256 checksum
How to use checksums
48a841e47429d7e0b803d82434628386818d1bf5a348c70c28fc5d322a560dd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.1 CPython/3.14.3 Linux/6.19.11-200.fc43.x86_64

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page