Run Docker container on AWS ECS-upload Docker image to ECR

You can download this article in PDF format via the link below to support us.
Download the guide in PDF formatturn off

This article is part 2 of a 3-part guide to running Docker containers on AWS ECS. ECS stands for Elastic Container Service. It is a managed container service that can run docker containers. Although AWS also provides container management through Kubernetes (EKS), it also has its own proprietary solution (ECS).

This is part 1 of the article: Using ECS ​​to run Docker containers on AWS-Part 1

The guide will cover:

  • Create ECS cluster
  • Set up the image registry (ECR) and push the docker image to the registry.
  • Deploy containers to the cluster using task and service definitions
  • Create a pipeline to update the services running on the ECS cluster

Part 2 of this guide will explain how to create an image registry (ECR) and push the image to the registry.

For this demonstration, we will use a simple hello world image. We will extract the image from docker and push it to our container registry (ECR).

Set up requirements/prerequisites

Before continuing to read this guide, the user/reader should ensure that the following requirements are met. This includes:

  • An AWS account.
  • A user is created on the account, and the user has permission to provision resources on the account.
  • An S3 bucket to store our CodeBuild artifacts.
  • A CloudWatch log group or S3 bucket to store your build project logs.

Create AWS ECR (Elastic Container Registry)

The Elastic Container Registry (ECR) is an AWS hosted container registry for storing Docker images. We will use the CloudFormation template below to create the registry.

AWSTemplateFormatVersion: "2010-09-09"
Description: 'Template to create an ECR repository'

Parameters:
  ECR:
    Type: String
    Description: "The Name of your ECR registry"

Resources:
  ECRRepository1:
    Type: "AWS::ECR::Repository"
    Properties:
      RepositoryName: test-ecr
      ImageScanningConfiguration:
        scanOnPush: "true"
      Tags: 
        - Key: Name
          Value: !Ref ECR
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-ecs

Outputs:
  RegistryName:
    Value: !Ref ECR
    Description: Name of the ECR Registry

Readers/users should customize the template to reflect their specific requirements. The specific aspects of the template to be customized are:

  • label.
  • Repository properties.

To manually create a registry on the ECR console, do the following: Click Create Repository.Create a repository

Then configure the repository settings.Run Docker container on AWS ECS-upload Docker image to ECRConfigure repository settings

When finished, click Create. Now you will create your ECR repository.

Upload images to AWS ECR using AWS CodeBuild

Once you have an image repository, it’s time to upload images to the repository. We will use CodeBuild to extract the image from the Docker hub and push it to the ECR registry.

On the CodeBuild console, click “Create Build Project”.Run Docker container on AWS ECS-upload Docker image to ECRCreate, build project

Next, in the project and source configuration, enter your project name and description. In addition, you can add tags to build projects.

For the source, select No source. If your docker file is in a repository or s3 bucket, select the source as the repository. Source options include:

  • GitHub
  • Bit bucket
  • Code submission
  • GitHub Enterprise Edition
  • S3 bucket

Run Docker container on AWS ECS-upload Docker image to ECRProject and source configuration

Then, in that environment, select the environment where you want to execute the build project. Since our image is a Linux image, we chose the Linux environment.Run Docker container on AWS ECS-upload Docker image to ECRChoose build environment

For the role, select the new service role. N/B: Ensure that the role has an additional AmazonEC2ContainerRegistryFullAccess policy. Otherwise, CodeBuild will not work properly.Run Docker container on AWS ECS-upload Docker image to ECR

Next, under buildspec, select the insert build command and paste one of the following two templates:

version: 0.2
phases:
  install:
    runtime-versions:
       docker: 19
    commands:
      - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
      - unzip awscliv2.zip
      - ./aws/install
  pre_build:
    commands:
      - echo logging to ecr
      - $(aws ecr get-login --no-include-email --region eu-central-1)
  build:
    commands:
      - docker pull hello-world
      - docker tag hello-world:latest 429758582529.dkr.ecr.eu-central-1.amazonaws.com/hello-world:latest
      
  post_build:
    commands:
      - echo build completed on 'date'
      - echo pushing to repo
      - docker push 429758582529.dkr.ecr.eu-central-1.amazonaws.com/hello-world:latest
      - echo Writing definitions file...
      - printf '[{"name":"Hello_World","imageUri":"%s"}]' 429758582529.dkr.ecr.eu-central-1.amazonaws.com/hello-world:latest > HelloWorldtaskdefinition.json
artifacts:
  files: HelloWorldtaskdefinition.json

In case you want to build an image from a docker file, and you can submit the code. Use the following buildspec.

version: 0.2
phases:
  install:
    runtime-versions:
       docker: 19
    commands:
      - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
      - unzip awscliv2.zip
      - ./aws/install
  pre_build:
    commands:
      - echo logging to ecr
      - $(aws ecr get-login --no-include-email --region eu-central-1)
  build:
    commands:
      - echo starting build on 'date'
      - cd ./
      - docker build -t hello-world .
      - docker tag hello-world:latest 429758582529.dkr.ecr.eu-central-1.amazonaws.com/hello-world:latest
  post_build:
    commands:
      - echo build completed on 'date'
      - echo pushing to repo
      - docker push 429758582529.dkr.ecr.eu-central-1.amazonaws.com/hello-world:latest
      - echo Writing definitions file...
      - printf '[{"name":"Hello_World","imageUri":"%s"}]' 429758582529.dkr.ecr.eu-central-1.amazonaws.com/hello-world:latest > HelloWorldtaskdefinition.json
artifacts:
  files: HelloWorldtaskdefinition.json

Then, under the component, select the previously created s3 bucket according to the requirements list.

Run Docker container on AWS ECS-upload Docker image to ECRBuild artifacts

Finally, under “Logs”, you can choose between CloudWatch Log Group or S3 bucket.Run Docker container on AWS ECS-upload Docker image to ECRSelect recording options

When finished, click Create to build the project. After creating the project, you can now click to start building.Run Docker container on AWS ECS-upload Docker image to ECR

As the build project runs, you can check the progress under the “Build Log” and “Phase” details. See the build project below.Run Docker container on AWS ECS-upload Docker image to ECREstablish project status

Run Docker container on AWS ECS-upload Docker image to ECRThe picture has been pushed to the registry

Now, we have successfully extracted the image from docker and pushed it to our ECR registry. In the next article, we will study how to deploy the image to an AWS ECS cluster.

  • Using ECS ​​to run Docker containers on AWS-part 1
  • AWS ECR guide

Other AWS guidelines:

  • How to mount AWS EFS file system on EC2 instance
  • Create AWS IAM users and groups using AWS CLI

Happy Building! ! !

You can download this article in PDF format via the link below to support us.
Download the guide in PDF formatturn off

Sidebar