AWS Continuous deployment (CD) pipeline with Amazon ECR and AWS CodePipeline

Create docker images from AWS codecommit and push images to AWS ECR repository

In this article, We will see how to create a complete, end-to-end continuous deployment (CD) pipeline with Amazon ECR and AWS CodePipeline. It walks you through setting up a pipeline to build your images when the upstream base image is updated.

Prerequisites

To follow along, you must have these resources in place:

1). CodeCommit - A source control

A source control repository with your base image Dockerfile and a Docker image repository to store your image. In this walkthrough, we use a simple Dockerfile for the base image:

2). A Dockerfile

FROM ubuntu RUN apt-get update RUN apt-get install nginx -y COPY . /var/www/html/ EXPOSE 8080 CMD ["nginx","-g","daemon off;"]

3). buildspec.yml

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" - echo Getting the docker verson.... - docker --version pre_build: commands: - echo Logging in to Amazon ECR.... - aws --version # update the following line with your own region - aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 1234567890.dkr.ecr.ap-south-1.amazonaws.com/hello-world - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) - IMAGE_TAG=${COMMIT_HASH:=latest} - REPOSITORY_URI=01234567890.dkr.ecr.ap-south-1.amazonaws.com/hello-world build: commands: - echo Build started on 'date' - echo Building the Docker image... # update the following line with the name of your own ECR repository - echo Get content - ls -alh - echo Getting docker images1 - docker images - docker build -t hello-world123 . - echo Getting docker images2 - docker images # update the following line with the URI of your own ECR repository (view the Push Commands in the console) - docker tag $REPOSITORY_URI:latest post_build: commands: - echo Build completed on 'date' - echo Pushing to repo....... # update the following line with the URI of your own ECR repository - docker push $REPOSITORY_URI:latest - echo Image is pushed....... - echo Writing image definitions file... #- printf '[{"ImageURI":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imageDetail.json - printf '{"ImageURI":"%s"}' $REPOSITORY_URI:latest > imageDetail.json artifacts: files: - imageDetail.json

4). A website index.html

5). ECR repo to push docker images.

A source control repository with your application Dockerfile and source code and a Docker image repository to store your image. For the application Dockerfile, we use our base image and then add our application code:

This step uses AWS CodeCommit for the source control repositories and Amazon ECR for storing the Docker images.

In this article, we have seen how to create a complete, end-to-end continuous deployment (CD) pipeline with Amazon ECR and AWS CodePipeline.