6

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 ?

Eric
  • 477
  • 3
  • 17

4 Answers4

8

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

Eric
  • 477
  • 3
  • 17
  • Please edit your question and include a summary at least of the solution. The link might die at anytime making this post useless. – Enissay Sep 12 '22 at 18:22
  • 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.) – Jacob Joy Jun 23 '23 at 13:43
2

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

  • 2
    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](/help/how-to-answer). – Community Sep 16 '22 at 12:26
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"

tylerwgrass
  • 656
  • 4
  • 19
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."
nobjta_9x_tq
  • 1,205
  • 14
  • 16