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

How to run docker-compose on AWS CodeBuild?

我正在尝试使用 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 并启动服务,但这将是 运行ning Docker inside Docker 并且需要启动构建服务器有特权的许可。我能看到的只有构建 Docker 图像的例子,但我只是想用它来设置环境以 运行 进行测试。

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

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

或者也可以为现有项目指定 - 从构建/构建项目、select 项目和编辑菜单中的环境。这使您可以指定图像:

您可以使用任何其他图像并在 buildspec.yml 中编写 Docker 设置脚本,但最简单的方法是使用上面的官方 Docker 图像。以此为容器,预装了docker和docker-compose,所以docker-compose "just works"。如果项目在其根目录中有一个 docker-compose.yml 文件,buildspec.yml 可以像 运行 一样简单立即将其安装:

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

您的构建环境是否使用 Docker 运行时?

我正在使用这个存储库:https://github.com/mreferre/yelb

dockercompose 没有构建任何东西(它更像是部署),但我相信您在此之前就遇到了问题。如果您使用 Docker 运行时,您的撰写应该有效。

这是我的构建规范的样子:

version: 0.2
phases:
  build:
    commands:
        - cd deployments/platformdeployment/Docker
        - docker-compose up -d

这是输出:

[Container] 2019/02/20 13:48:02 Waiting for agent ping 
[Container] 2019/02/20 13:48:04 Waiting for DOWNLOAD_SOURCE 
[Container] 2019/02/20 13:48:07 Phase is DOWNLOAD_SOURCE 
[Container] 2019/02/20 13:48:07 CODEBUILD_SRC_DIR=/codebuild/output/src292484508/src/github.com/mreferre/yelb 
[Container] 2019/02/20 13:48:07 YAML location is /codebuild/readonly/buildspec.yml 
[Container] 2019/02/20 13:48:07 Processing environment variables 
[Container] 2019/02/20 13:48:07 Moving to directory /codebuild/output/src292484508/src/github.com/mreferre/yelb 
[Container] 2019/02/20 13:48:07 Registering with agent 
[Container] 2019/02/20 13:48:07 Phases found in YAML: 1 
[Container] 2019/02/20 13:48:07  BUILD: 2 commands 
[Container] 2019/02/20 13:48:07 Phase complete: DOWNLOAD_SOURCE Success: true 
[Container] 2019/02/20 13:48:07 Phase context status code:  Message:  
[Container] 2019/02/20 13:48:07 Entering phase INSTALL 
[Container] 2019/02/20 13:48:07 Phase complete: INSTALL Success: true 
[Container] 2019/02/20 13:48:07 Phase context status code:  Message:  
[Container] 2019/02/20 13:48:08 Entering phase PRE_BUILD 
[Container] 2019/02/20 13:48:08 Phase complete: PRE_BUILD Success: true 
[Container] 2019/02/20 13:48:08 Phase context status code:  Message:  
[Container] 2019/02/20 13:48:08 Entering phase BUILD 
[Container] 2019/02/20 13:48:08 Running command cd deployments/platformdeployment/Docker 

[Container] 2019/02/20 13:48:08 Running command docker-compose up -d 
Creating network "docker_yelb-network" with driver "bridge" 
Pulling redis-server (redis:4.0.2)... 
4.0.2: Pulling from library/redis 
Pulling yelb-db (mreferre/yelb-db:0.3)... 
0.3: Pulling from mreferre/yelb-db 
Pulling yelb-appserver (mreferre/yelb-appserver:0.3)... 
0.3: Pulling from mreferre/yelb-appserver 
Pulling yelb-ui (mreferre/yelb-ui:0.3)... 
0.3: Pulling from mreferre/yelb-ui 
Creating docker_redis-server_1 ...  
Creating docker_yelb-db_1      ...  
·[2A·[2K 
Creating docker_redis-server_1 ... ·[32mdone·[0m 
·[2B·[1A·[2K 
Creating docker_yelb-db_1      ... ·[32mdone·[0m 
·[1BCreating docker_yelb-appserver_1 ...  
·[1A·[2K 
Creating docker_yelb-appserver_1 ... ·[32mdone·[0m 
·[1BCreating docker_yelb-ui_1        ...  
·[1A·[2K 
Creating docker_yelb-ui_1        ... ·[32mdone·[0m 
·[1B 
[Container] 2019/02/20 13:49:00 Phase complete: BUILD Success: true 
[Container] 2019/02/20 13:49:00 Phase context status code:  Message:  
[Container] 2019/02/20 13:49:00 Entering phase POST_BUILD 
[Container] 2019/02/20 13:49:00 Phase complete: POST_BUILD Success: true 
[Container] 2019/02/20 13:49:00 Phase context status code:  Message:  


好的,我想通了!

您需要在 CodeBuild 容器上启用 'Privileged Access'。这将允许您与 docker cli 交互。

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

- 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" `

例如:

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!"