In this example, we will consider creating a repository in CodeCommit and building a simple Docker image using CodeBuild and launching it in ECR.
Create a repository in CodeCommit. My repository name is "artem-test"
In order to work with the repository, make sure that your user has an SSH key loaded. If it is already loaded, look at its ID, it will be needed.
We clone our repository:
git clone ssh://[email protected]/v1/repos/artem-test
Do not forget to change the region in which the repository is created.
Add to it for the test an example of a simple Dockerfile.
Dockerfile:
FROM php:7.1-apache-jessie RUN apt update && \ apt install curl net-tools && \ apt-get clean CMD ["apache2-foreground"]
For the build we will use: buildspec.yml
buildspec.yml:
version: 0.2 env: variables: AWS_ACCOUNT_ID: "XXXXXXXXXXXX" AWS_DEFAULT_REGION: "us-east-1" IMAGE_REPO_NAME: "artem-test" IMAGE_TAG: "latest" phases: install: runtime-versions: docker: 18 pre_build: commands: - echo Logging in to Amazon ECR... - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION) build: commands: - echo Build started on `date` - echo Building the Docker image... - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG . - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG post_build: commands: - echo Build completed on `date` - echo Pushing the Docker image... - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
Send local changes to the server:
git add . git commit -am "git init" git push
Create a project in CodeBuild, specifying as a source repository in CodeCommit.
We give for CodeBuild rights in ECR
Open IAM -> CodeBuild
Looking for "codebuild-artem-test-service-role"
And we add the following Policy to this role:
AmazonEC2ContainerRegistryPowerUser
To display the build logs, go to the "CloudWatch" service and create a group. You can also create an S3 Bucket to store log archives.
You can try to build an image in CodeBuild.