Gitlab-CI Docker In Docker with Dotnet Testcontainers

发布于 2025-01-10 20:46:23 字数 3324 浏览 5 评论 0原文

下午好,各位。我正在尝试在使用 nuget 包的 DotNet 项目上运行 Gitlab-CI 管道 https://github .com/HofmeisterAn/dotnet-testcontainers 用于在测试执行期间管理 docker 容器。在过去的几天里,我尝试了许多不同的配置变化,包括结合 Dotnet 和 docker 的自定义 docker 镜像。然而,可能由于我缺乏 CI/CD 领域的知识,我尚未获得成功的构建。最终结果是我无法让所有码头工人适当地相互交谈。我正在寻求任何帮助来设置此构建系统。

谢谢。

当前配置:

  • 在 Windows 上运行 Docker Desktop (4.5.1),并在设置中启用 Expose 守护进程。

  • Gitlab-Runner 在本地注册并运行\

    <前><代码>[[跑步者]] name =“Siegeon Docker Runner” url =“https://gitlab.com/” 令牌=“保留” 执行者=“码头工人” [runners.custom_build_dir] [跑步者缓存] [runners.cache.s3] [runners.cache.gcs] [runners.cache.azure] [跑步者.docker] tls_verify = false 图像=“码头工人:稳定” 特权=真 禁用_入口点_覆盖=假 oom_kill_disable = false 禁用缓存=假 卷= [“/缓存”] shm_大小 = 0
  • Gitlab-ci.yml

    <前><代码>阶段: - 测试 测试覆盖率: 图片:mcr.microsoft.com/dotnet/sdk:6.0 阶段:测试 服务: 服务: # 测试容器需要 DinD 服务 - 名称:docker:dind 命令:[“--tls = false”] 之前的脚本: - 码头工人信息 变量: # 指示 Docker 不要通过 TLS 启动。 DOCKER_TLS_CERTDIR:“” # 使用overlayfs 提高性能。 DOCKER_DRIVER:覆盖2 DOCKER_HOST: tcp://docker:2375 配置:“调试” COVERAGE_FLAG:“XPlat 代码覆盖率” LOGGER_FLAG:“junit;LogFilePath=$CI_PROJECT_DIR/junit/junit-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose” 脚本: - '点网测试 -c $配置 -r $CI_PROJECT_DIR/cobertura --collect:"$COVERAGE_FLAG" --测试适配器路径:。 --logger:"$LOGGER_FLAG"' - 光盘脚本 - chmod +x print-dotnet-coverage.sh - ./print-dotnet-coverage.sh $CI_PROJECT_DIR/cobertura 覆盖范围:/TOTAL_COVERAGE=(\d+.\d+)/ 文物: 路径: - $CI_PROJECT_DIR/cobertura/*/coverage.cobertura.xml - $CI_PROJECT_DIR/junit/junit-test-result.xml 报告: 科贝图拉: - $CI_PROJECT_DIR/cobertura/*/coverage.cobertura.xml 朱尼特: - $CI_PROJECT_DIR/junit/junit-test-result.xml

Gitlab 日志

```
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 为了完整性 在构建的初始化过程中,我重新配置了 docker 主机 输入图片此处描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2025-01-17 20:46:23

当使用 testcontainers-dotnet 与 .net6.0 结合使用时,此示例确实对我有用,ef核心和 gitlab 管道。希望它可以帮助别人。

初始化测试容器的应用程序代码:

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:
  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

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
十级心震 2025-01-17 20:46:23

我不确定它是否 100% 兼容,但是 Testcontainers-java 的 Gitlab 示例配置 有一些差异:

在禁用 TLS 的情况下启动 docker:

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

此外,DOCKER_HOST 字符串也不同:

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

希望这些配置会改变它工作方式与 Testcontainers-java 构建类似。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文