How to run docker-compose on AWS CodeBuild?

mahemoff picture mahemoff · Oct 23, 2018 · Viewed 10.8k times · Source

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

Answer

NickKampe picture NickKampe · Feb 21, 2019

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