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.
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):
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:
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
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.
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)
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.
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.
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/…
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.
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!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With