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.


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.

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:

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.

Originally published by Vaibhav Raizada at https://dzone.com

Learn more

☞ Docker Mastery: The Complete Toolset From a Docker Captain

☞ Learn DevOps: The Complete Kubernetes Course

☞ Docker for the Absolute Beginner - Hands On

☞ Docker Technologies for DevOps and Developers

☞ Docker Swarm Mastery: DevOps Style Cluster Orchestration

WordPress in Docker. Part 1: Dockerization

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

AWS Training Online Course - Best AWS Certification - Intellipaat

AWS Training Online Course - Best AWS Certification - Intellipaat

Enroll for best AWS training course with AWS Certification online and placement support. Learn Amazon web services AWS course from certified AWS experts. Enroll Now!

Enroll for best AWS training course with AWS Certification online and placement support. Learn Amazon web services AWS course from certified AWS experts. Enroll Now!

List all containers in Docker(Docker command)

List all containers in Docker(Docker command)

We can get a list of all containers in docker using `docker container list` or `docker ps` commands.

We can get a list of all containers in docker using docker container list or docker ps commands.

List Docker Containers

To list down docker containers we can use below two commands

  • docker container list
  • docker ps

docker container ls command introduced in docker 1.13 version. In older versions we have to use docker ps command.

List all Containers in docker, using docker ls command

The below command returns a list of all containers in docker.

docker container list -all

or

docker container ls -all

List all containers in docker, using docker ps command

In older version of docker we can use docker ps command to list all containers in docker.

$ docker ps -all

or

$ docker ps -a

List all Running docker containers

The default docker container ls command shows all running docker containers.

$ docker container list

or

$ docker container ls

or

To get list of all running docker containers use the below command

$ docker ps

List all stopped docker containers command

To get list of all stopped containers in docker use the below commands

$ docker container list -f "status=exited"

or

$ docker container ls -f "status=exited"

or you can use docker ps command

$ docker ps -f "status=exited"

List all latest created docker containers

To list out all latest created containers in docker use the below command.

$ docker container list --latest

Show n last created docker containers

To display n last created containers in docker use the below command.

$ docker container list --last=n