gpt4 book ai didi

javascript - AWS CodeBuild : How to run tests that require starting Docker?

转载 作者:行者123 更新时间:2023-12-05 00:32:25 31 4
gpt4 key购买 nike

我有一个带有 Testcontainers 的 Node.js 项目用于盯着 Redis 和 Prostgress 进行测试。
我想将这些测试作为 CI 的一部分运行。为此,我使用了一个 Node.js 的 Alpine 镜像,上面安装了 Docker。我可以在那里运行不需要 Docker 的测试,但如果它涉及 Docker,我会从 Testcontainers 收到此错误:

No Docker client strategy found


这是我在日志中得到的:
2022-05-25T13:09:11.494Z testcontainers DEBUG Found applicable Docker client strategy: UnixSocketStrategy
2022-05-25T13:09:11.531Z testcontainers DEBUG Testing Docker client strategy URI: unix:///var/run/docker.sock
2022-05-25T13:09:11.539Z testcontainers DEBUG No registry auth locator found for registry: "https://index.docker.io/v1/"
2022-05-25T13:09:11.543Z testcontainers WARN Docker daemon is not reachable: Error: connect ENOENT /var/run/docker.sock
2022-05-25T13:09:11.543Z testcontainers WARN Docker client strategy UnixSocketStrategy is not reachable
2022-05-25T13:09:11.544Z testcontainers ERROR Failed to list images: Error: No Docker client strategy found
2022-05-25T13:09:11.544Z testcontainers ERROR Failed to pull image "postgres:14.2-alpine": Error: No Docker client strategy found
当我在本地启动它们时,测试运行良好。
我在 CodeBuild 文档中没有找到任何解释从构建器容器内部连接到 Docker 引擎的内容。我找到了 these instructions在 Testcontainers 文档中,但我不明白如何在 AWS Codebuild 中应用它。
问:如何在 AWS CodeBuild 中运行需要启动 Docker 的测试?

最佳答案

WARN Docker daemon is not reachable: Error: connect ENOENT /var/run/docker.sock


默认情况下的 CodeBuild 环境:
  • 无权与 Docker 守护进程交互,dockerd
  • 没有运行 Docker 守护进程

  • 该错误通过声明它无法访问守护程序来暗示这一点。
    为了解决上述问题:
  • 设置您的buildspec version至 0.2
  • 使用 privilegedMode 运行您的build设置为 true , 授予对守护进程的访问权限
  • 确保守护程序正在运行

  • 这里要注意的关键点是使用 AWS CodeBuild 时提供的 official Docker images , Docker 守护进程通过 dockerd-entrypoint.sh 自动启动.您无需手动执行此操作。
    但是,在使用自定义图像时,您必须手动启动守护程序。

    您可以在构建的安装阶段手动初始化 Docker 守护程序,方法是将以下命令添加到您的 buildspec.yml文件。
    对于基于 Alpine 的图像:
    version: 0.2

    phases:
    install:
    commands:
    - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
    - timeout -t 15 sh -c "until docker info; do echo .; sleep 1; done"
    ...
    对于基于 Ubuntu 的镜像:
    version: 0.2

    phases:
    install:
    commands:
    - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
    - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
    ...
    您现在应该能够在构建中运行 Docker 容器。
    如果这仍然不起作用,它将与 Testcontainers 有关。
    根据您链接的文档,确保您的 docker run ...运行容器的命令包括 -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock .

    关于javascript - AWS CodeBuild : How to run tests that require starting Docker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72278034/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com