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

问题描述 投票:9回答:2

我正在尝试使用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镜像的例子,但我只是想用它来设置环境以运行测试。

ANSWERED: Set up Docker image in CodeBuild's Environment section

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

enter image description here

或者也可以为现有项目指定 - 从“构建/构建项目”,“编辑”菜单中选择项目和“环境”。这允许您指定图像:

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
ruby-on-rails amazon-web-services docker docker-compose aws-codebuild
2个回答
2
投票

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

我正在使用这个回购:https://github.com/mreferre/yelb

dockercompose没有构建任何东西(它意味着部署),但我相信你在此之前遇到了一个问题。如果您使用Docker运行时,您的构图应该可以工作。

这就是我的buildspec看起来像:

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:  



6
投票

好的,我想出了这个问题!

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

然后将这两行添加到install命令:

`- 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!"
© www.soinside.com 2019 - 2024. All rights reserved.