testcontainers-dotnet
testcontainers-dotnet copied to clipboard
Exception while using Testcontainers to run tests in a container
Running tests using Testcontainers from within a container results in exceptions being thrown. The same tests pass when run locally (i.e. not in a container).
The following is a typical stack trace of the exceptions thrown:
System.TypeInitializationException : The type initializer for 'DotNet.Testcontainers.Configurations.TestcontainersSettings' threw an exception.
---- System.InvalidOperationException : Sequence contains no elements
Stack Trace:
at DotNet.Testcontainers.Configurations.TestcontainersSettings.get_OS()
...
Is additional config required to run Testcontainers within a container? Or is this a bug?
To Reproduce Steps to reproduce the behavior:
- Set up an xUnit project containing a single test class, using the PostgreSqlTest test class shown on the main README for testcontainers-dotnet, using the following nuget packages:
- Testcontainers 2.1.0
- Npgsql 6.0,.6
- xUnit 2.4.2
- Add a Dockerfile to build and run the tests (using mcr.microsoft.com/dotnet/sdk:6.0).
- Build & run the test project in a container.
Screenshots
[xUnit.net 00:00:00.71] Tests.PostgreSqlTest.ExecuteCommand [FAIL]
Failed Tests.PostgreSqlTest.ExecuteCommand [1 ms]
Error Message:
System.TypeInitializationException : The type initializer for 'DotNet.Testcontainers.Configurations.TestcontainersSettings' threw an exception.
---- System.InvalidOperationException : Sequence contains no elements
Stack Trace:
at DotNet.Testcontainers.Configurations.TestcontainersSettings.get_OS()
at Tests.PostgreSqlTest..ctor() in /Tests/PostgreSqlTest.cs:line 15
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
----- Inner Stack Trace -----
at System.Linq.ParallelEnumerable.GetOneWithPossibleDefault[TSource](QueryOperator`1 queryOp, Boolean throwIfTwo, Boolean defaultIfEmpty)
at System.Linq.ParallelEnumerable.First[TSource](ParallelQuery`1 source)
at DotNet.Testcontainers.Configurations.TestcontainersSettings..cctor()
Expected behavior The test should pass.
Desktop (please complete the following information):
- Running on Windows 10 (Build 19044.1865).
- Docker Desktop For Windows 4.11.0 (83626). Using linux containers & WSL2.
- .Net SDK 6.0.302
Testcontainers cannot find the Docker endpoint, see: #528, #529, https://github.com/testcontainers/testcontainers-dotnet/discussions/543#discussioncomment-3299997. Add the env variable DOCKER_HOST e.g.
What should DOCKER_HOST be set to for Docker running locally? host.docker.internal doesn't resolve the problem.
Is additional config required to run Testcontainers within a container? Or is this a bug?
This is not a bug. Docker is not running inside your container. Either use a different Docker host or use something like Docker in Docker (DinD).
What should DOCKER_HOST be set to for Docker running locally? host.docker.internal doesn't resolve the problem.
This can be any exposed Docker API endpoint. Maybe this helps to understand your issue too.
Thanks for taking the time to help. I'm afraid it's just not working with DinD. I'll have to find something else.
I'm afraid it's just not working with DinD.
It is working. I am using it too. Here are some more information: https://github.com/testcontainers/testcontainers-dotnet/issues/492#issuecomment-1171283803.
@AndrewPearson did the comment help? Do you need any further help or can we close this issue?
@HofmeisterAn Sorry for the slow reply, I've been offline for a couple of weeks. I'm afraid the previous comment was interesting but it didn't help. There are lots of suggestions in the various links, but they're all fragments of a working solution and there's nothing I have seen that I can really use to learn how this is done.
It's ok to close this issue.
No worries. The linked comment contains everything to setup an example with Docker.
I worked it out finally. Thanks for your help.
I worked it out finally. Thanks for your help.
@AndrewPearson how did you end up resolving this? getting the same error after updating the lib for my local tests
@pdevito3 My initial requirement was to get this working in a container running locally on Docker Desktop for Windows. In the end all that was required was to map my local /var/run/docker.sock as a volume in the container.
On Windows, docker.sock is available as //var/run/docker.sock. This differs slightly from the path available on other OS's.
If you're using docker-compose then add a volume entry like the following to your service declaration:
volumes:
- //var/run/docker.sock:/var/run/docker.sock
If you're using docker run then map the same volumes with the -v option.
For me that was all that was required to get things working.