skip to Main Content

Good afternoon, folks. I am attempting to run a Gitlab-CI pipeline on a DotNet project that utilizes the nuget package https://github.com/HofmeisterAn/dotnet-testcontainers to manage docker containers during test execution. Over the past few days, I have tried many different configuration variations, including custom docker images incorporating Dotnet and docker. However, likely due to my lack of knowledge in the CI/CD area, I have yet to get a successful build. The net result is that I cannot get all of the dockers to talk to each other appropriately. I am reaching out for any help in getting this build system setup.

Thank you.

Current Configuration:

  • Docker Desktop (4.5.1) running on windows with Expose daemon enabled in the settings.

  • Gitlab-Runner registered and running locally

    [[runners]]
      name = "Siegeon Docker Runner"
      url = "https://gitlab.com/"
      token = "withheld"
      executor = "docker"
      [runners.custom_build_dir]
      [runners.cache]
        [runners.cache.s3]
        [runners.cache.gcs]
        [runners.cache.azure]
      [runners.docker]
        tls_verify = false
        image = "docker:stable"
        privileged = true
        disable_entrypoint_overwrite = false
        oom_kill_disable = false
        disable_cache = false
        volumes = ["/cache"]
        shm_size = 0
    
  • Gitlab-ci.yml

    stages:
      - tests
    
    test-with-coverage:
      image: mcr.microsoft.com/dotnet/sdk:6.0
      stage: tests
      services:
        services:
        # DinD service is required for Testcontainers
        - name: docker:dind
        command: ["--tls=false"]
      before_script:
        - docker info
      variables:
        # Instruct Docker not to start over TLS.
        DOCKER_TLS_CERTDIR: ""
        # Improve performance with overlayfs.
        DOCKER_DRIVER: overlay2
        DOCKER_HOST: tcp://docker:2375
        CONFIGURATION: "Debug"
        COVERAGE_FLAG: "XPlat Code Coverage"
        LOGGER_FLAG: "junit;LogFilePath=$CI_PROJECT_DIR/junit/junit-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
      script:
        - 'dotnet test
                -c $CONFIGURATION
                -r $CI_PROJECT_DIR/cobertura
                --collect:"$COVERAGE_FLAG"
                --test-adapter-path:.
                --logger:"$LOGGER_FLAG"'
        - cd scripts
        - chmod +x print-dotnet-coverage.sh
        - ./print-dotnet-coverage.sh $CI_PROJECT_DIR/cobertura
      coverage: /TOTAL_COVERAGE=(d+.d+)/
      artifacts:
        paths:
          - $CI_PROJECT_DIR/cobertura/*/coverage.cobertura.xml
          - $CI_PROJECT_DIR/junit/junit-test-result.xml
        reports:
          cobertura:
            - $CI_PROJECT_DIR/cobertura/*/coverage.cobertura.xml
          junit:
            - $CI_PROJECT_DIR/junit/junit-test-result.xml
    

Gitlab logs

```
Running with gitlab-runner 14.7.0 (98daeee0)
  on Siegeon Docker Runner TdwgDJWA
Preparing the "docker" executor
00:50
Using Docker executor with image mcr.microsoft.com/dotnet/sdk:6.0 ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Using docker image sha256:1a42336ff683d7dadd320ea6fe9d93a5b101474346302d23f96c9b4546cb414d 
for docker:dind with digest docker@sha256:6f2ae4a5fd85ccf85cdd829057a34ace894d25d544e5e4d9f2e7109297fedf8d ...
Waiting for services to be up and running...
*** WARNING: Service runner-tdwgdjwa-project-33678908-concurrent-0-82673b37ef5d9ae1-docker-0 probably didn't start properly.
Health check error:
service "runner-tdwgdjwa-project-33678908-concurrent-0-82673b37ef5d9ae1-docker-0-wait-for-service" timeout
Health check container logs:
Service container logs:
2022-03-02T17:34:46.439281600Z time="2022-03-02T17:34:46.438975100Z" level=info msg="Starting up"
2022-03-02T17:34:46.440522500Z time="2022-03-02T17:34:46.440396800Z" level=warning msg="could not change group 
/var/run/docker.sock to docker: group docker not found"
```

9/27/2022 For completeness
During the initialization of build I reconfigured the docker host
enter image description here

2

Answers


  1. I’m not sure it’s 100% compatible, but Testcontainers-java’s example config for Gitlab has a few differences:

    Start docker with TLS disabled:

    services:
        # explicitly disable tls to avoid docker startup interruption
        command: ["--tls=false"]
    

    Also the DOCKER_HOST string is different:

    # your config has DOCKER_HOST: tcp://localhost:2375
    DOCKER_HOST: tcp://docker:2375
    

    Hope with these config changes it works just like for the Testcontainers-java builds.

    Login or Signup to reply.
  2. This example did work for me when using testcontainers-dotnet in combination with .net6.0, ef core and the gitlab pipeline. Hope it can help someone.

    Application Code to initialize Testcontainers:

    namespace Tests;
    
    public class IntegrationTestFactory :
        WebApplicationFactory<Program>, IAsyncLifetime
    {
        //mssql container definition
        private readonly Testcontainers.MsSql.MsSqlContainer _dbContainer;
    
        public IntegrationTestFactory()
        {
            //build container
            _dbContainer = new MsSqlBuilder()
                .WithCleanUp(true)
                .Build();
        }
    
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureTestServices(services =>
            {
                //Remove possibly added dbcontextoptions
                var descriptor = services
                    .SingleOrDefault(d => 
                        d.ServiceType == typeof(DbContextOptions<AppDbContext>));
    
                if (descriptor != null)
                {
                    services.Remove(descriptor);
                }
    
                services.AddDbContext<AppDbContext>(options =>
                {
                    //get connection string from container
                    options.UseSqlServer(_dbContainer.GetConnectionString());
                });
    
                // Ensure database gets created
                var serviceProvider = services.BuildServiceProvider();
                using var scope = serviceProvider.CreateScope();
                var scopedServices = scope.ServiceProvider;
                var context = scopedServices.GetRequiredService<AppDbContext>();
                context.Database.EnsureCreated();
    
            });
        }
    
        public async Task InitializeAsync() => await _dbContainer.StartAsync();
    
        public new async Task DisposeAsync() => await _dbContainer.StopAsync();
    }
    

    gitlab-ci.yaml (test job part)

    test_job:
      image: mcr.microsoft.com/dotnet/sdk:6.0
      stage: build
      only:
        - develop
        - /^feature.*$/
      services:
      - name: docker:dind
        # explicitly disable tls to avoid docker startup interruption
        command: ["--tls=false"]
      variables:
        # Instruct Testcontainers to use the daemon of DinD.
        DOCKER_HOST: "tcp://docker:2375"
        # Instruct Docker not to start over TLS.
        DOCKER_TLS_CERTDIR: ""
        # Improve performance with overlayfs.
        DOCKER_DRIVER: overlay2
      script:
        - dotnet restore mySolution.sln -s 'https://api.nuget.org/v3/index.json' -s $NUGET_SOURCE
        - dotnet test src/Tests/Tests.csproj
      tags:
        - docker
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search