How to run docker-compose on AWS CodeBuild?
Asked Answered
K

3

28

I'm trying to setup automated Rails tests on AWS CodeBuild using docker-compose, but it errors out.

In 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

Presumably I need to install docker and start the service, but that would be running Docker inside Docker and would require the build server to be started with privileged permission. Only examples I can see are for building Docker images, but I'm just trying to use it to setup the environment to run the test in.

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

Thanks to @mferre for answering this. Docker-compose is indeed completely supported without doing anything special. The key is to choose a Docker image in the "environment" section when setting up inside AWS CodeBuild console (or same via the API):

enter image description here

Or can also be specified for an existing project - from Build / Build Projects, select the project, and Environments from the Edit menu. This lets you specify the image:

enter image description here

You could use any other image and script the Docker setup in buildspec.yml, but the easiest way is to use the official Docker image as above. With this as the container, docker and docker-compose are pre-installed, so docker-compose "just works". If the project has a docker-compose.yml file in its root, the buildspec.yml can be as simple as running it immediately:

version: 0.2
phases:
  build:
    commands:
      - docker-compose up -d
Kt answered 23/10, 2018 at 12:43 Comment(4)
did you ever solve this issue? Running into the same errors here with the aws provided node 8.11 containerVarney
World's greatest unsolved mystery 😭Kt
... just got solved 🍾Kt
@Kt Links to Screenshots are unreliable. Some working reference to your solution would be great.Marque
C
9

Are you using the Docker runtime for your build environment?

I am working with this repo: https://github.com/mreferre/yelb

That dockercompose isn't building anything (it's rather meant to deploy) but I believe you are hitting a problem much before that. Your compose up should work if you use the Docker runtime.

This is what my buildspec looks like:

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

And this is the output:

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


Cloistral answered 20/2, 2019 at 13:55 Comment(4)
Thanks for the output, it at least shows it's possible. Yes, using Docker in development too.Kt
Sorry when I asked if you were using the Docker runtime I meant if you had configured your CodeBuild environment to use the Docker runtime image. @Varney mentioned the Node image and you mentioned that you should install docker and start the service. None of this is required if you use the Docker container and that <should> work out of the gate. Just checking.Cloistral
Thanks. That was it. There's so many config options, but now I see I had to set this up in the "environment" section of CodeBuild.Kt
Unfortunately this does not work for Windows images. Nor does the privileged alternative.Gossipmonger
V
16

Okay, I figured out the issue!

You need to enable 'Privileged Access' on the CodeBuild container. This will allow you to interact with the docker cli.

Then add these two lines to the 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" `

ex:

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!"
Varney answered 21/2, 2019 at 3:21 Comment(7)
this may work but it seems over complicated. If you just choose the Docker runtime in the Codebuild environment configuration it will just work.Cloistral
I am currently doing the same with a custom image and docker-compose. However the docker-compose commands does literally nothing (a.k.a no console output) and I do not get why... I also get the following message: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?Egwan
This is the "AWS" documented way for use for containers that are not the "docker" default, such as the code build provided images like nodejs:10.14.1 and ruby:2.5.3 Read more here: docs.aws.amazon.com/codebuild/latest/userguide/…Varney
Make sure you are using "privileged mode" on the build, or you will get the "cant connect to docker daemon" error message every single time.Varney
So, I've configured to run as privileged as well as putting in the specific Daemon run commands mentioned above, but I'm still getting the "is the daemon running?" error.. Any help is appreciated.Impressment
I somehow cannot get this to wrk. I am getting nohup: failed to run command '/usr/local/bin/dockerd': No such file or directoryagentCheater
You could try to run which dockerd which should tell you where is your dockerd located. I found it in /usr/bin/dockerdRouleau
C
9

Are you using the Docker runtime for your build environment?

I am working with this repo: https://github.com/mreferre/yelb

That dockercompose isn't building anything (it's rather meant to deploy) but I believe you are hitting a problem much before that. Your compose up should work if you use the Docker runtime.

This is what my buildspec looks like:

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

And this is the output:

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


Cloistral answered 20/2, 2019 at 13:55 Comment(4)
Thanks for the output, it at least shows it's possible. Yes, using Docker in development too.Kt
Sorry when I asked if you were using the Docker runtime I meant if you had configured your CodeBuild environment to use the Docker runtime image. @Varney mentioned the Node image and you mentioned that you should install docker and start the service. None of this is required if you use the Docker container and that <should> work out of the gate. Just checking.Cloistral
Thanks. That was it. There's so many config options, but now I see I had to set this up in the "environment" section of CodeBuild.Kt
Unfortunately this does not work for Windows images. Nor does the privileged alternative.Gossipmonger
C
0

Image aws/codebuild/standard:7.0 have docker compose installed

Connected answered 23/8, 2023 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.