You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since the reuse feature became available I started using Testcontainers to automate setting up a development database. I have created a hosted service that goes like this:
usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;usingMicrosoft.EntityFrameworkCore;usingMicrosoft.Extensions.Hosting;usingMicrosoft.Extensions.Logging;usingTestcontainers.MsSql;namespaceSampleCode;internalclassDockerDatabaseService:IHostedService{privatereadonlyIDbContextFactory<MyDbContext>_dbContextFactory;privatereadonlyMsSqlContainer_container;publicDockerDatabaseService(IHostEnvironmentenvironment,IDbContextFactory<MyDbContext>dbContextFactory,ILoggerFactoryloggerFactory){if(!environment.IsDevelopment()){thrownewNotSupportedException($"{nameof(DockerDatabaseService)} must only be used in the development environment but the current environment is \"{environment.EnvironmentName}\"");}_dbContextFactory=dbContextFactory;_container=newMsSqlBuilder().WithLogger(loggerFactory.CreateLogger("Testcontainers")).WithReuse(true).WithName(environment.ApplicationName).WithPortBinding(hostPort:1433,containerPort:1433).Build();}publicasyncTaskStartAsync(CancellationTokencancellationToken){await_container.StartAsync(cancellationToken);awaitusingvardbContext=await_dbContextFactory.CreateDbContextAsync(cancellationToken);awaitdbContext.Database.MigrateAsync(cancellationToken);}publicasyncTaskStopAsync(CancellationTokencancellationToken){await_container.StopAsync(cancellationToken);}}
The Testcontainers.MsSql package is included conditionally for debug builds only:
It worked so well that I used it in several projects. But then the same container was reused across different projects. Since I explicitly set a container name (WithName(environment.ApplicationName)) I assumed that the reuse mechanism would use it but it does not.
Solution
I think the container name (just like network and volume names) should be part of the reuse hash.
Benefit
Container reuse would work out of the box by just setting an explicit container name.
Alternatives
Currently I'm working around this issue by configuring the container with a reuse-id label:
_container=newMsSqlBuilder().WithLogger(loggerFactory.CreateLogger("Testcontainers")).WithReuse(true).WithLabel("reuse-id",environment.ApplicationName)// 👈 goes into the reuse hash.WithName(environment.ApplicationName).WithPortBinding(hostPort:1433,containerPort:1433).Build();
Would you like to help contributing this enhancement?
Problem
Since the reuse feature became available I started using Testcontainers to automate setting up a development database. I have created a hosted service that goes like this:
The
Testcontainers.MsSql
package is included conditionally for debug builds only:And the hosted service is also registered conditionally for debug builds in the development environment:
It worked so well that I used it in several projects. But then the same container was reused across different projects. Since I explicitly set a container name (
WithName(environment.ApplicationName)
) I assumed that the reuse mechanism would use it but it does not.Solution
I think the container name (just like network and volume names) should be part of the reuse hash.
Benefit
Container reuse would work out of the box by just setting an explicit container name.
Alternatives
Currently I'm working around this issue by configuring the container with a
reuse-id
label:Would you like to help contributing this enhancement?
Yes: #1162
The text was updated successfully, but these errors were encountered: