Skip to content

fix: reuse container requires name#887

Merged
DDtKey merged 3 commits intotestcontainers:mainfrom
tisonkun:fixup-reuse-container
Dec 19, 2025
Merged

fix: reuse container requires name#887
DDtKey merged 3 commits intotestcontainers:mainfrom
tisonkun:fixup-reuse-container

Conversation

@tisonkun
Copy link
Contributor

@tisonkun tisonkun commented Dec 1, 2025

... also sync Container should reuse ContainerAsync's drop impl.

This resolves #742.

cc @DDtKey @the-wondersmith

Signed-off-by: tison <wander4096@gmail.com>
@netlify
Copy link

netlify bot commented Dec 1, 2025

Deploy Preview for testcontainers-rust ready!

Name Link
🔨 Latest commit 499d5c6
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-rust/deploys/692d43a922732600080d6777
😎 Deploy Preview https://deploy-preview-887--testcontainers-rust.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@tisonkun tisonkun mentioned this pull request Dec 1, 2025
active.runtime.block_on(async {
match active.async_impl.docker_client().config.command() {
env::Command::Remove => {
if let Err(e) = active.async_impl.rm().await {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why containers are always removed as reported in #742 (comment)

I'm using sync Containers and its Drop impl isn't properly forwarded to the async impl.

})?;

if let Some(container_id) = client
.get_running_container_id(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this can't handle the case where an existing container is stopped. It would then failed with:

called `Result::unwrap()` on an `Err` value: Client(CreateContainer(DockerResponseServerError { status_code: 409, message: "Conflict. The container name \"/postgres\" is already in use by container \"5d2940fb2bd042cf859598932aae34aa5004b19d38797165806282b4ada66157\". You have to remove (or rename) that container to be able to reuse that name." }))
thread 'test_simple_pubsub' panicked at tests/toolkit/src/container.rs:150:10:
called `Result::unwrap()` on an `Err` value: Client(CreateContainer(DockerResponseServerError { status_code: 409, message: "Conflict. The container name \"/postgres\" is already in use by container \"5d2940fb2bd042cf859598932aae34aa5004b19d38797165806282b4ada66157\". You have to remove (or rename) that container to be able to reuse that name." }))

I resolve this issue in my bollard based solution by removing a stopped container with the same name, but not sure how to make it more smoothly in testcontainers.

Anyway, this can be a follow-up to improved:

async fn maybe_remove_container(&self, docker: &Docker, container_name: &str) {
    let options = Some(RemoveContainerOptions {
        v: true,
        force: true,
        link: false,
    });

    // best effort to remove existing container; ignore errors
    let _ = docker.remove_container(container_name, options).await;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopped container

This is common when a lasting test container gets stopped during the dev machine reboot.

Copy link
Contributor

@DDtKey DDtKey Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a good follow-up!
However need to decide which policy should be applicable, because it looks like stopped container can be reused too - just restarted on demand.
Thanks!

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
Copy link
Contributor Author

tisonkun commented Dec 1, 2025

Let me fix the format issue.

... also it seems that tests rely on selecting by labels without container name, let me try to fine tune the impl.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
Copy link
Contributor Author

tisonkun commented Dec 1, 2025

@DDtKey failure resolved. Please help in triggering the CI.

@tisonkun
Copy link
Contributor Author

tisonkun commented Dec 1, 2025

Besides, we may later drop the "reusable-containers" flag and just deliver it by default. There is no extra dependency requirement.

@the-wondersmith
Copy link
Contributor

@tisonkun I need to dig in to confirm when I get back to my desk, but I think there's some overlap here with the fix branch I'd previously asked you to test. Come to think of it actually, I need to actually get that branch up to date with main 🤔😅.

tl;dr I'll block some time out today to give this PR a closer look, and some time this week to get that branch fixed up and get a proper PR opened.

@tisonkun
Copy link
Contributor Author

tisonkun commented Dec 3, 2025

@the-wondersmith 👍🏻

I think this PR can resolve my remaining issue now. If @DDtKey or other maintainer can get it reveiwed, you may put your changes upon this one.

That said, I'm glad to learn any concrete suggestion to improve the patch in place. And there is still a pending issue (#887 (comment)) we need to decide before testcontainers-rs can go back to my testkit ><

@tisonkun
Copy link
Contributor Author

tisonkun commented Dec 8, 2025

Reminder @DDtKey maybe retrigger the CI workflow?

@DDtKey DDtKey merged commit b0a3d28 into testcontainers:main Dec 19, 2025
28 of 31 checks passed
@DDtKey
Copy link
Contributor

DDtKey commented Dec 19, 2025

Sorry for the delay with review @tisonkun!
That looks like a valid fix regardless of reuse container functionality and helps to reuse the code

@github-actions github-actions bot mentioned this pull request Dec 19, 2025
DDtKey added a commit that referenced this pull request Dec 19, 2025
## 🤖 New release

* `testcontainers`: 0.26.0 -> 0.26.1

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

## [0.26.1] - 2025-12-19

### Details
#### Bug Fixes
- Reuse container requires name
([#887](#887))
- Respect `TESTCONTAINERS_COMMAND`
([#891](#891))

#### Miscellaneous Tasks
- Update russh requirement from 0.54.4 to 0.55.0
([#888](#888))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Artem Medvedev <i@ddtkey.com>
@tisonkun
Copy link
Contributor Author

Great! Thanks for your reviewing.

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.

Reuse containers

3 participants