[Workaround] Fix Testcontainers.SQL error “Docker.DotNet.DockerApiException : Docker API responded with status code=Conflict”

On Friday, Sep 20, 2024 at 11:33 AM (GMT+2) the Azure DevOps build pipeline of one of my projects I’m currently working on started to fail with the following error.

Error Message:
   Docker.DotNet.DockerApiException : Docker API responded with status code=Conflict, response={"message":"container 23c2232d1303cbbcbb9bb4d5b150ffff76de7e8bfac5f930256f71758aa3b836 is not running"}

  Stack Trace:
     at Docker.DotNet.DockerClient.HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response, IEnumerable`1 handlers)
   at Docker.DotNet.DockerClient.MakeRequestAsync(IEnumerable`1 errorHandlers, HttpMethod method, String path, IQueryString queryString, IRequestContent body, IDictionary`2 headers, TimeSpan timeout, CancellationToken token)
   at Docker.DotNet.ExecOperations.ExecCreateContainerAsync(String id, ContainerExecCreateParameters parameters, CancellationToken cancellationToken)
   at DotNet.Testcontainers.Clients.DockerContainerOperations.ExecAsync(String id, IList`1 command, CancellationToken ct) in /_/src/Testcontainers/Clients/DockerContainerOperations.cs:line 150
   at Testcontainers.MsSql.MsSqlBuilder.WaitUntil.UntilAsync(MsSqlContainer container) in /_/src/Testcontainers.MsSql/MsSqlBuilder.cs:line 146
   at DotNet.Testcontainers.Containers.DockerContainer.CheckReadinessAsync(WaitStrategy waitStrategy, CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 534
   at DotNet.Testcontainers.Configurations.WaitStrategy.<>c__DisplayClass24_0.<<WaitUntilAsync>g__UntilAsync|0>d.MoveNext() in /_/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs:line 184
--- End of stack trace from previous location ---

The pipeline task that triggers this error only executes all unit and integration tests that are implemented with xUnit v2.9.0 and Testcontainers.MsSql v3.10.0. The task basically looks as follows.

task: DotNetCoreCLI@2
        inputs:
          command: test
          projects: ${{parameters.testProjects}}
          arguments: '--configuration ${{parameters.buildConfiguration}} --collect "Code coverage"'

I was wondering why all integration tests suddenly failed from one run to the next.

A big thank you to my friend Amr Farooq, who saved me a few hours of research by making me aware of a problem with Testcontainers.Sql. He mentioned to me that Testcontainers.MsSql recently stopped working on ubuntu due to its usage of an outdated docker image. If the failing Azure DevOps pipeline runs on vmImage: ubuntu-latest and local development is done on Windows, the issue can be solved respectively workarounded as follows:

// Workaround to fix issue with Testcontainers.MsSql on Linux
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
	_msSqlContainer = new MsSqlBuilder()
		.WithImage(
			"mcr.microsoft.com/mssql/server:2022-latest"
		)
		.WithPortBinding(1433, true)
		.Build();
}
else
{
	_msSqlContainer = new MsSqlBuilder()
		.WithPortBinding(1433, true)
		.Build();
}

3 thoughts on “[Workaround] Fix Testcontainers.SQL error “Docker.DotNet.DockerApiException : Docker API responded with status code=Conflict”

Add yours

  1. Thank you!! I just spent hours trying to figure this out. How did you diagnose this was the issue?

    Like

Leave a comment

Website Powered by WordPress.com.

Up ↑