testcontainers-dotnet
testcontainers-dotnet copied to clipboard
Running tests in parallel using a docker container errors due to multiple processes accessing the same .tar file
When trying to run tests in parallel using a custom docker file using the ImageFromDockerfileBuilder to build the image, the processes try to access the same .Tar file.
To Reproduce Steps to reproduce the behavior:
- Create project with Xunit
- Add 2 test classes
- Build the image from custom docker file
- Run all the tests Xunit should run the tests in parallel by default.
Create multiple of the same docker container, one for each Xunit test class and run the tests against the relevant container.

- Windows 10
- Dotnet 6
- Xunit 2.4.1
- Testcontainers 2.1.0
This is my practice project:
https://github.com/haridasnykiel/TryTestContainers/tree/main/Weather.API.IntegrationTests
You have to build the image first, then reuse the id. I assume you do not await the task and build the image twice, see:
https://github.com/testcontainers/testcontainers-dotnet/blob/f7c0fc6bed67d706ad03c2d89ca19767b323ed92/tests/Testcontainers.Tests/Unit/Images/ImageFromDockerfileTest.cs#L82
BTW, I cannot access your example.
Thanks for that, yea I realise my example is in a private repo. Your suggestion has given me an idea, Ill try it first.
FYI - I am building the image and waiting for the task to complete, I think I have it in the wrong place.
I am also experiencing this issue. I need to create an image for the sql database that publishes the dacpac to server as part of the image build. So for each test class this image will be built and used to create the database container. However, every time I try to run these in parallel I get this issue. I'm not entirely sure how to get around it. Our test set up is rather complex and uses multiple containers. I have been trying to get this to work for a while now. Any advice would be very appreciated.
Make sure you are building the image just once and reuse the id.
I realise that I am clearly missing an integral piece of the puzzle here, so could you please help explain how I could achieve this when using the ImageFromDockerfile builder? These are set up in the test fixture InitializeAsync, along with the containers. How can I assure that it is only built once? How do I get the Id of the first image built and share it amongst the other tests that are running in parallel?
Which testing framework do you use? Here is an example that uses xUnit.net (see the C# snippet): https://github.com/testcontainers/testcontainers-dotnet/issues/582#issuecomment-1247921642. There are a couple of ways to share fixtures in xUnit.net.
Yes, I use xUnit. That will solve the problem, using collection definition. I am working on someone else's test set up and it's very over engineered and complex. I will rewrite and implement this. I can't thank you enough for your time and help. This package is great.
Re-using the image instance worked for me thanks.