Skip to content

Conversation

surister
Copy link

@surister surister commented Oct 5, 2025

This utils method runs containers on the same context manager. This ensures that the passed containers are run in order and cleaned up.

The main use case is to be able to run code after all containers have started. I need this to create CrateDB clusters.

Practical example: run containers inside a network and clean up the network (network can only be cleaned up after all containers using it are stopped)

network = Network()
network.create()

with run_containers(
        PostgresContainer(network=network),
        PostgresContainer(image='postgres:16', network=network),
) as containers:
    c1, c2 = containers
    conn1 = sqlalchemy.engine.create_engine(c1.get_connection_url()).connect()
    conn2 = sqlalchemy.engine.create_engine(c2.get_connection_url()).connect()

    result1 = conn1.execute(sqlalchemy.text("select version()")).fetchone()
    result2 = conn2.execute(sqlalchemy.text("select version()")).fetchone()

    print(result1, result2, sep='\n')

# The network gets removed only when containers are stopped.
network.remove()

@alexanderankin
Copy link
Member

if we add this can we name it consistently with any one of the other implementations, thanks

@surister
Copy link
Author

surister commented Oct 8, 2025

if we add this can we name it consistently with any one of the other implementations, thanks

Hm, could you like any of the other implementations? Can't find them @alexanderankin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants