[AWS] What are ECR and ECS? About each difference and how to operate Docker on AWS.

You can run Docker on AWS using AWS's ECR and ECS.

If you look at the list of all AWS services, it's listed in the far right corner. image.png

Elastic Container Registry is ECR and Elastic Container Service is ECS.

table of contents

  1. What are ECR and ECS?
  2. ECR
  3. Create Repository
  4. Push / Pull Image
  5. ECS
  6. Create Cluster (#クラスターの作成)
  7. Task Definition

## What are ECR and ECS? -ECR is the AWS version of Docker hub. You can create repositories and push and pull images.

・ ECS is the place to start Docker on AWS **. Start the container from the ECR image.

You can push a Docker image onto AWS and share it with the entire team (ECR) instead of Docker hub, or you can launch and operate a container on AWS (ECS) **.


ECR

--Abbreviation for Elastic Container Registry. --Docker registry service. --AWS version of Docker hub.

Elastic is elastic. Here, it means that there is a high degree of freedom in following changes in the procedure. Container is a Docker container. Registry is the storage location. In summary, nuances such as ** highly flexible container storage location **.

You can push the created image to ECR and save it.


## ECR details What you can do with ECR
  1. Creating a repository
  2. Image push / pull
  3. Detailed image confirmation

image.png You can choose Private or Public to create a repository.

image.png You can see the list of created images by clicking the repository name.

image.png Click on the image to see its details.

Creating a repository

Enter the creation screen from the repository creation button at the top right of the screen. Very easy to create. Basically,

--Private or Public selection --Enter the repository name --Creating a repository

Only 3 steps.

image.png image.png

Push the image to the repository

Go into the repository and click ** Show Push Command ** in the upper right to see the steps to push an image into the repository.

image.png

image.png

step1 Log in to the Docker registry server

step1


#Get an AWS authentication token and log in to docker's repository server
aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin [aws_account_id].dkr.ecr.[region].amazonaws.com

· Aws ecr get-login-password --region ap-northeast-1 Get the PW (token) of the specified region with the aws ecr command.

· Command A | Command B When commands are connected with a "|" pipe, the data acquired by command A is passed to command B.

· Docker login [optional] [server] --username AWS: Log in with your username as AWS

--password-stdin: For the password, enter the token obtained earlier as standard input (-stdin). Pass by.

[aws_account_id].dkr.ecr.[region].amazonaws.com Specify the aws account for the server. If no server is specified, connect to Docker hub.

Docker login command


### step2 Create an image from a Docker file If there is a Dockerfile in the directory you are currently working on, execute the following command.

step2


docker build -t [Repository name] .

-Docker build [option] [Docker file path] -t [image name: tag name]: Add a tag to the created image. Here, the repository name is specified as a tag.

If no tag name is specified, it will be latest.

For example For docker build -t test-repository . The image name will be test-repository: latest.

. The path to the directory where the Dockerfile is located. Specify that it is in the current directory.


### step3 Set the repository name for the image

step3


docker tag [Repository name]:latest [aws_account_id].dkr.ecr.[region].amazonaws.com/[Repository name]:latest

Set the repository name for the image.

docker tag [image name] [repository name: tag name] [Image name] can be specified by either the image ID or the image: tag.

Example after setting


$ docker images
REPOSITORY                                                                                TAG              IMAGE ID                 CREATED          SIZE
[aws_account_id].dkr.ecr.[region].amazonaws.com/[Repository name on AWS]                              latest           test-repository:latest   33 minutes ago   802MB

### step4 Push the image to ECR
docker push [aws_account_id].dkr.ecr.[region].amazonaws.com/test-repository:latest

docker push [repository name: tag name] Push the image of the tag name corresponding to the specified repository.

This completes pushing the image.




ECS

--Abbreviation for Elastic Container Service --Docker container execution environment

image.png

(Reference) Docker official overview


## ECS details ECS allows you to launch a container from an image pushed to AWS.

There are two main items in processing: (1) cluster and (2) task definition.

image.png

Simply put, clusters are sites and applications, task definitions are container creation settings.

** ▼ ECS object relationships **

image.png

What is a cluster?

A cluster is ** one or more containers that can execute task requests **

In other words, one application. For example, if the blog site and the corporate site are operated on separate servers, there are two clusters, the blog site and the corporate site.

The image is that multiple containers such as Ruby on Rails and DB such as PostgreSQL are running in the blog site.

image.png

Creating a cluster

step1 Select cluster template

Select a template to easily create a cluster.

image.png

What is AWS Fargate?

Read as Fargate. A service dedicated to ECS (and EKS) that allows you to run containers without having to manage EC2 servers or clusters.


#### step2 Create by specifying the cluster name

image.png

・ What is VPC?

An abbreviation for Amazon Virtual Private Cloud (Amazon VPC), a dedicated space for launching containers.

・ What is Container Insights?

It will be possible to send specific data addition to AWS operation status monitoring service CloudWatch.

There are some data that can be sent, such as CpuUtilized (the number of CPU units in use).

image.png

AWS Official ECS Container Insights

This completes the cluster creation.

image.png


## What is a task definition? The task definition is the definition of basic settings such as the image and CPU for starting the container.

Task definition is required to run Docker container in ECS.

** ▼ Contents of task definition **

--Docker image used by each container in the task --Amount of CPU and memory used by each task or each container within the task --The startup type to use. Determine the infrastructure on which the task will be hosted. --Docker network mode used for task containers --Log configuration used for tasks --Whether to continue running the task if the container terminates or fails --Commands that need to be executed when the container starts --Data volume that needs to be used in the task container --The IAM role that the task must use


### How to create a task #### step1 Selection of startup type

image.png

step2 Setting task and container definitions

Set the task name, CPU and memory amount to be allocated to the container, container name, etc.

** ▼ Main settings **

image.png

image.png

Specify the image to start the container from ECR.

AWS Official Task Definition

Recommended Posts

[AWS] What are ECR and ECS? About each difference and how to operate Docker on AWS.
List how to learn from Docker to AKS on AWS
[Part 1] How to deploy Docker containers and static files with CircleCI + ECS + ECR + CloudFront
How to install and use Composer on an ECS instance on Ubuntu 16.04
To beginners launching Docker on AWS
How to run JavaFX on Docker
[Java] How to use static modifiers (What are static final and static import)
Enable Docker build cache on GitHub Action and deploy to Amazon ECS
[AWS ECR] Save and get Docker images
Try the Docker environment on AWS ECS
Note: [Docker] How to start and stop
How to send push notifications on AWS
How to deploy a container on AWS Lambda
Notes on how to use each JUnit Rule
How to install Ruby on an EC2 instance on AWS
Run Rubocop and RSpec on CircleCI and deploy to ECS
How to hover and click on Selenium DOM elements
(Ruby on Rails6) How to create models and tables
How to launch Swagger UI and Swagger Editor in Docker
How to create an environment that uses Docker (WSL2) and Vagrant (VirtualBox) together on Windows