DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Deploy Docker With AWS CodePipeline

Deploy Docker With AWS CodePipeline

Setting up and using AWS's CodePipeline can improve the overall continuous deployment strategy for containerized apps.

Vaibhav Raizada user avatar by
Vaibhav Raizada
CORE ·
Mar. 21, 19 · Tutorial
Like (2)
Save
Tweet
Share
17.46K Views

Join the DZone community and get the full member experience.

Join For Free

Containerized applications are fast becoming a need for most enterprises. Docker is probably the most popular tool for containerizing applications. With non-containerized applications, developers were able to do a continuous deployment on on-prem servers using several well-established processes. However, we have a similar need with containerized applications. Amazon Web Services offer a suite of services which enable continuous deployments of Dockerized applications using AWS CodePipeline. 

AWS CodePipeline uses CodeCommit as a source code repository, CodeBuild for building the Docker image, Elastic Container Service (ECS) for deploying the Docker Image, and Elastic Container Registry (ECR) for storing the Docker image.

This post will help guide you through a step-by-step process of setting up a CodePipeline for your Docker images. A few prerequisites for these steps would be:

  • AWS CodeCommit as a source code repository containing the source code and Dockerfile.

  • AWS ECR as a repository for the Docker image.

  • AWS ECS cluster configured with a Task Definition which references the Docker image stored in ECR repository.

We will accomplish the creation of a pipeline in just three steps:

1. Build a Docker Image using AWS CodeBuild.

2. Use AWS CodePipeline to build a pipeline.

3. Test the pipeline!

Build Docker Image Using AWS CodeBuild

For CodeBuild to build your project source code, you only need to add one configuration file, typically in YAML format, to your project source code. This file is to be named as buildspec.yml  CodeBuild will connect with your source control repository, read the buildspec.yml, and build your project code.

The buildspec.yml file will contain instructions on how to build the source code into a Docker image. Here, you may also mention where to push the newly created image. You can specify the Image Repository.

The buidlspec.yml file has three sections: prebuild, build and post-build. Refer to this sample buildspec.yml file below and its following explanation.

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - $(aws ecr get-login --region us-east-2)
      - REPOSITORY_URI=87324561928.dkr.ecr.us-east-2.amazonaws.com/projectcode
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Writing definitions file...
      - printf '[{"name":"project-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > taskdefinition.json
artifacts:
    files: taskdefinition.json

Pre-build:

In this stage, we set the CodeBuild to be able to log in to ECR as we will save our Docker image in ECR. We set the REPOSITORY_URI   to point to the ECR image and tag it with a Git commit ID for uniqueness.

Build:

We build the Docker image and tag the image as latest and with the image tag created in the previous step.

Post-Build:

After the image has been built, we will now push it to ECR using the specifics set in the pre-build step. We will tag it as  latest  and with the image tag. Here, we also need to output the ECS service's container name and image and tag it to a file. In the sample code, this file is named as taskdefinition.json . This definition is needed in the deployment step of the pipeline, where an updated task definition will be created.

Create an AWS CodePipeline

Go to AWS CodePipeline console and you will see this page with five steps.

CodePipline Console

At step 1, simply provide a name to your pipeline.

At step 2, mention the source of your project code — the repository name and branch name.

On step 3, choose CodeBuild and specify details like "Runtime as Docker."

For step 4, specify the deployment provider as AWS ECS. We will deploy our Docker image on ECS. Mention the ECS Cluster name and Service name.

Test the Pipeline

Let us test the pipeline. Go to the CodePipline console again and pick the pipeline you just created.

Simultaneously, also push a code change to your source code. Now you will be able to see the pipeline progress through various stages. See the representative image below:

CodePipeline Progress

While this pipeline is only to demonstrate AWS CodePipeline, it may be further enhanced to also include extensive test case suites for unit testing, integration testing, regression, and smoke testing. Also, include static code analysis before code build. As you may have observed, it is quite simple to set up a CodePipeline using AWS console. You can manage all your pipelines through the console.

Benefits of CodePipeline

CodePipeline makes the release process streamlined and manageable. In Agile methodology, frequent and periodic releases are common. In such a scenario, the use of the CodePipeline helps quickly release new features to production in a stable manner. It also offers integration with several third-party tools. Many third-party partners offer testing tools for load testing, performance testing, and more. Finally, CodePipeline has a GUI which makes it easy to model the pipeline. 

AWS Docker (software)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • Container Security: Don't Let Your Guard Down
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • How To Build an Effective CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: