FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(core): Add `run_containers` function in utils.py to run several containers by surister · Pull Request #896 · testcontainers/testcontainers-python · GitHub

feat(core): Add run_containers function in utils.py to run several containers - #896

Open
surister wants to merge 1 commit into
testcontainers:mainfrom
surister:feat/run_many_containers
Open

feat(core): Add run_containers function in utils.py to run several containers#896
surister wants to merge 1 commit into
testcontainers:mainfrom
surister:feat/run_many_containers

Conversation

surister commented Oct 5, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

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()

Copy link
Copy Markdown
Member

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

surister commented Oct 8, 2025
edited
Loading

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor

I'm admittedly not sure this should be part of the library, when the caller can achieve the same result explicitly with the same amount of code.

with run_containers(
        PostgresContainer(network=network),
        PostgresContainer(image='postgres:16', network=network),
) as containers:
    c1, c2 = containers

Becomes

with contextlib.ExitStack() as stack:
    c1, c2 = stack.enter_context(container) for container in (
        PostgresContainer(network=network),
        PostgresContainer(image='postgres:16', network=network)
    )

alexanderankin commented Oct 15, 2025 via email

Copy link
Copy Markdown
Member

alexanderankin commented Oct 15, 2025 via email

Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Contributor

Ahhhh, I see what you're looking to achieve. I could definitely see the parallel container startup being a really useful feature. In it's current form, that's not what this PR proposes.

I suspect such an implementation and would warrant some discussion on which of python's plethora of ways to achieve parallelism makes most sense here.

Tranquility2 added 🛠️ needs more work Need to invest more time, can be a rebase or code updates ✨ feat labels Apr 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✨ feat 🛠️ needs more work Need to invest more time, can be a rebase or code updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL