How can I build a Docker image with Codebuild?
Asked Answered
D

4

8

I want to build a docker image from a Dockerfile, and publish this image to an AWS ECR (Docker registry).

In order to succeed, I need to have access to docker build and docker push from within the Codebuild container, which is problematic (installing docker inside a docker container causes errors such as Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?).

How would one proceed to do such a thing please? Is there any way to use codebuild to build Docker images from a Dockerfile ?

Dutchman answered 25/6, 2020 at 15:58 Comment(0)
D
11

I found a solution. Follow this link for the code, and don't forget to enable "Priviledged" in the "Environment" configuration screen.

Dutchman answered 26/6, 2020 at 7:52 Comment(2)
Please edit your question and include a summary at least of the solution. The link might die at anytime making this post useless.Stefansson
If someone is trying to find the settings, It's Currently under Build projetcs --> Edit -->Edit Environment -->Override image --> And select Privileged (Enable this flag if you want to build Docker images or want your builds to get elevated privileges.)Cyrie
I
2

Just clic the 'privileged mode' button in the CodeBuild settings privileged mode

Idolize answered 12/9, 2022 at 18:17 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Fireweed
S
1

The Docker sample for CodeDeploy lists the following steps:

  • For Image, choose aws/codebuild/standard:4.0.
  • Because you use this build project to build a Docker image, select Privileged.

and provides the following note:

Note

By default, Docker containers do not allow access to any devices. Privileged mode grants a build project's Docker container access to all devices. For more information, see Runtime Privilege and Linux Capabilities on the Docker Docs website.

If you are still having trouble, the PriviledgedMode section of the CloudFormation docs for AWS::CodeBuild::Project Environment has the following steps for initializing the Docker daemon:

You can initialize the Docker daemon during the install phase of your build by adding one of the following sets of commands to the install phase of your buildspec file:

If the operating system's base image is Ubuntu Linux:

- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 --storage-driver=overlay&

- timeout 15 sh -c "until docker info; do echo .; sleep 1; done"

If the operating system's base image is Alpine Linux and the previous command does not work, add the -t argument to timeout:

- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 --storage-driver=overlay&

- timeout -t 15 sh -c "until docker info; do echo .; sleep 1; done"

Schacker answered 6/10, 2021 at 20:56 Comment(0)
S
0
  1. chose image ubuntu following: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html
  2. Check on "Enable this flag if you want to build Docker images or want your builds to get elevated privileges."
Squarely answered 18/6, 2023 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.