Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force Clean Containers on build before start #242

Closed
mubeda opened this issue May 8, 2020 · 7 comments · Fixed by #413
Closed

Force Clean Containers on build before start #242

mubeda opened this issue May 8, 2020 · 7 comments · Fixed by #413
Labels
enhancement New feature or request
Milestone

Comments

@mubeda
Copy link

mubeda commented May 8, 2020

.WithCleanUp(true) works well if you don't have any crash on your application. But after a crash you cannot build the container again. For Postgresql you get this exception ("Message = "An item with the same key has already been added. Key: 5432/tcp"). Basically you need to go to the docker app and remove the container manually.
It will be really nice that everytime you build a container with the WithCleanUp(true) it forces the delete of the container if it already exist.

Thanks!

@HofmeisterAn
Copy link
Collaborator

@mubeda There is already a mechanism to delete remaining containers at the end. See: https://github.com/HofmeisterAn/dotnet-testcontainers/blob/d2469b35d8f7496411ac5ca8d3486b4ab65b8957/src/DotNet.Testcontainers/Clients/TestcontainersClient.cs#L56-L57

Either the event is not triggered or finished in time. The total execution time of all finalizers is limited in .NET Framework. I'm aware that this implementation has some disadvantages. However, it is simple and has always worked in my use cases.

  • Which framework do you use?
  • Do you always use the same container name?
  • Can you test if the event is fired at all and provide some more information?

In addition to that. I guess you are mapping the host port 5432 to the container port 5432. It's recommended to use a random host port and assign that to the default container port. This is the default behavior and good practice, see: https://github.com/HofmeisterAn/dotnet-testcontainers/blob/4acf978e59578c1b99590eafef74edefb10791ff/src/DotNet.Testcontainers/Containers/Configurations/Abstractions/TestcontainerDatabaseConfiguration.cs#L10

It will prevent mapping the same host port multiple times (of course, it's just a workaround and not a proper fix).

@mubeda
Copy link
Author

mubeda commented May 11, 2020

Hey Andre!
Thank you for the response. I use .net core 3.1, i always use use the same container name and fixed ports, i am trying to implement automatic test cases for all dbms i support in my application.
. This problem happens to me once when i stopped the execution of the process, then the code of PurgeOrphanedContainers is never executed. Basically i had to go to docker and manually delete the running instance. So i was courious if this Purge can be done by instance name on app startup. Feel free to close this ticked if you thing it's not needed.

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented May 11, 2020

If you kill the process (includes canceling tests), the event is not triggered at all. This is another drawback of the implementation I was talking about. The official implementation uses an independent container as ResourceReaper to kill orphaned containers. This would be a great and very useful feature.

If someone starts with the implementation I am happy to jump in. I just can't bring myself to do it at the moment, because I don't need it urgently.

@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Jun 9, 2020
@HofmeisterAn HofmeisterAn added this to the 1.3.0 milestone Jun 9, 2020
@HofmeisterAn HofmeisterAn removed this from the 1.3.0 milestone Jul 3, 2020
HofmeisterAn added a commit that referenced this issue Feb 19, 2021
…rphanedContainersArgs'

{Remove PurgeOrphanedContainersArgs.}
@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Mar 2, 2021

Containers with status exited will be removed in the next test cycle (#369).

@savalik
Copy link

savalik commented Jun 17, 2021

I'm afraid it's not helpful for this issue - because contrainers remain online.

@HofmeisterAn
Copy link
Collaborator

I'm afraid it's not helpful for this issue - because contrainers remain online.

What status do the containers have? The implementation will only remove containers with status exited. It won't remove running containers from previous runs.

PSanetra added a commit to PSanetra/dotnet-testcontainers that referenced this issue Dec 21, 2021
… function: ResourceReaper'

{Add ResourceReaper.}
PSanetra added a commit to PSanetra/dotnet-testcontainers that referenced this issue Dec 21, 2021
… function: ResourceReaper'

{Add ResourceReaper.}
PSanetra added a commit to PSanetra/dotnet-testcontainers that referenced this issue Dec 21, 2021
… function: ResourceReaper'

{Add ResourceReaper.}
@HofmeisterAn HofmeisterAn added this to the 1.6.0 milestone Jan 31, 2022
HofmeisterAn pushed a commit to PSanetra/dotnet-testcontainers that referenced this issue Feb 3, 2022
… function: ResourceReaper'

{Add ResourceReaper.}
@HofmeisterAn
Copy link
Collaborator

Resource Reaper added for Linux containers in 1.6.0-beta.2085, see #413.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants