testcontainers / testcontainers/testcontainers-dotnet

[Enhancement]: clean up old reuse containers

Open
#1,166 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
4.4k
Forks
357
Avg merge
14h 42m
Merged PRs (30d)
16

Description

Problem

I'm not entirely sure, if this is an indented use-case or not.

In one of my projects, we have a huge number of database migrations and also a lot of seeding data.
So my goal was, to use the new reuse-feature ( #1051 ), so that I once start the database in a testcontainer, apply all migrations and seed some data, and then reuse that container.

_container = new MsSqlBuilder()
    ...
    .WithCleanUp(true)
    .WithReuse(true)
    .WithLabel("reuse-id", hash_of_migrations_and_seeding)
    .Build();

Whenever a new migration is added, or some seeding data changes, the hash changes, and a new container is created and used.
When it stays the same, the existing container is reused.

That actually works, however the old containers are never cleaned up.
New containers are created, but the old containers (with different reuse-id labels) either keep running (is .Dispose() call is disabled), or keep existing in a stopped state (if .Dispose() is used).
In both variants, more and more resources of the system are used, until you manually clean it up.

Solution

I would prefer it, that if the reuse-id changes, all old containers with the same image and different reuse-id are deleted automatically.

Benefit

Less resources used on your system.

Alternatives

We keep it as is.

Would you like to help contributing this enhancement?

Yes

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the reuse feature from issue #1051 and tracing how WithCleanUp(true), WithReuse(true), and the reuse-id label affect container creation, disposal, and stopped containers. Define done as automatically removing older containers using the same image but a different reuse-id, with coverage for both running and stopped cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, docker
Domain
devops, testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.