gpt4 book ai didi

ruby-on-rails - 如何在 AWS CodeBuild 上运行 docker-compose?

转载 作者:IT老高 更新时间:2023-10-28 12:46:28 25 4
gpt4 key购买 nike

我正在尝试使用 docker-compose 在 AWS CodeBuild 上设置自动化 Rails 测试,但它出错了。

在 buildspec.yml 中:

phases:
build:
commands:
- docker-compose up -d

[Container] 2018/10/23 11:27:56 Running command docker-compose up -d
Couldn't connect to Docker daemon at http+docker://localhost - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
[Container] 2018/10/23 11:27:56 Command did not exit successfully docker-compose up -d exit status 1
[Container] 2018/10/23 11:27:56 Running command echo This always runs even if the install command fails
This always runs even if the install command fails
[Container] 2018/10/23 11:27:56 Phase complete: BUILD Success: false
[Container] 2018/10/23 11:27:56 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: docker-compose up -d. Reason: exit status 1

大概我需要安装 docker 并启动服务,但这将在 Docker 中运行 Docker,并且需要以特权权限启动构建服务器。我只能看到用于构建 Docker 镜像的示例,但我只是尝试使用它来设置运行测试的环境。

回答:在 CodeBuild 的环境部分设置 Docker 镜像

感谢@mferre 回答这个问题。 Docker-compose 确实是完全支持的,不需要做任何特别的事情。关键是在 AWS CodeBuild 控制台(或通过 API 进行设置)时在“环境”部分选择一个 Docker 镜像:

enter image description here

也可以为现有项目指定 - 从 Build/Build Projects 中选择项目,然后从 Edit 菜单中选择 Environments。这使您可以指定图像:

enter image description here

您可以使用任何其他镜像并在 buildspec.yml 中编写 Docker 设置脚本,但最简单的方法是使用上述官方 Docker 镜像。以此为容器,docker 和 docker-compose 已预先安装,因此 docker-compose “正常工作”。如果项目在其根目录中有 docker-compose.yml 文件,则 buildspec.yml 可以像立即运行一样简单:

version: 0.2
phases:
build:
commands:
- docker-compose up -d

最佳答案

好的,我想通了!

您需要在 CodeBuild 容器上启用“特权访问”。这将允许您与 docker cli 交互。

然后将这两行添加到安装命令中:

- nohup/usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver =覆盖2&
- 超时 15 sh -c "直到 docker info;做回声。; sleep 1;完成"
`

例如:

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"
pre_build:
commands:
- docker build -t helloworld .
build:
commands:
- docker images
- docker run helloworld echo "Hello, World!"

关于ruby-on-rails - 如何在 AWS CodeBuild 上运行 docker-compose?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949413/

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