Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run docker-compose on AWS CodeBuild?

Tags:

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 
like image 716
mahemoff Avatar asked Oct 23 '18 12:10

mahemoff


People also ask

Can I run Docker compose in AWS?

It is now even easier for a developer to take a containerized microservices-based application from their workstation and deploy it straight to the AWS Cloud. Developers can now run docker compose up and deploy their existing Docker Compose files straight to Amazon ECS, as previously shown here.

Does CodeBuild run on Docker?

AWS CodeBuild manages the following Docker images that are available in the CodeBuild and AWS CodePipeline consoles. The base image of the Windows Server Core 2019 platform is only available in the following regions: US East (N. Virginia)

Does docker compose work with AWS?

In the future, Docker plans to add docker compose support for the default Docker context (i.e. local Docker Engine) experience as well. For the most part, this integration will try to use sensible defaults and completely abstract the details of the AWS implementation.

How to use AWS codebuild with Docker Hub?

Follow the steps in Run CodeBuild directly to create a build environment, run the build, and view related build information. Confirm that AWS CodeBuild successfully pushed the Docker image to the repository. Sign in to Docker Hub, go to the repository, and choose the Tags tab. The latest tag should contain a very recent Last Updated value.

Can I use codebuild with Docker containers?

If you just choose the Docker runtime in the Codebuild environment configuration it will just work. 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/…

How does CodePipeline work with docker compose?

Copying the ZIP file into the S3 bucket will trigger the CodePipeline; after a few seconds the pipeline will transition to the first stage, building the container image via CodeBuild. When the Docker Compose file is ran locally with docker compose up, Docker Compose is building the container image and starting the container.


1 Answers

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!" 
like image 66
NickKampe Avatar answered Sep 20 '22 16:09

NickKampe