Summary of Docker understanding by beginners ⑥ ~ Until automatic deployment of docker container to EC2 instance using CodeDeploy and CodePipeline ~

Introduction

Now that I've finally started learning Docker, I'll summarize my understanding.

What i did

And this time too, docker has nothing to do with it. ..

Premise

At the time of two times before, the EC2 instance was started and docker and docker-compose were installed. スクリーンショット 2020-10-26 14.05.06.png

procedure

Install Code Deploy Agent on your EC2 instance

Here and Here .com / ja_jp / codedeploy / latest / userguide / resource-kit.html # resource-kit-bucket-names)

#First, log in to EC2
ssh -i "ec2-key.pem" [email protected]

#Installation
sudo yum update
sudo yum install ruby
sudo yum install wget
cd /home/ec2-user

#Here is a bucket for each environment-name and region-Change identifier
wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install

chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status

By the way, if the above is described in the user data of the instance startup setting, it will be automatically executed when the instance is created such as AutoScalling. It doesn't matter this time.

ʻCreate an IAM role with the AWSCodeDeployRole` policy attached and attach it to your EC2 instance

First select Create Role from IAM Console

スクリーンショット 2020-10-26 14.32.48.png

Select EC2 as the entity

スクリーンショット 2020-10-26 14.33.52.png

ʻAttach AWS CodeDeployRole` policy

スクリーンショット 2020-10-26 14.36.52.png

Create a role with an appropriate tag and role name

スクリーンショット 2020-10-26 14.44.15.png

Attach the role created on the EC2 instance

スクリーンショット 2020-10-26 15.04.28.png スクリーンショット 2020-10-26 15.08.38.png

CodeDeploy settings

First of all, CodeDeploy console

スクリーンショット 2020-10-26 15.11.27.png

Application settings

Select EC2 as the platform スクリーンショット 2020-10-26 15.13.19.png

Create role for CodeDeploy

Same procedure as creating a role for EC2 earlier. スクリーンショット 2020-10-26 15.17.19.png スクリーンショット 2020-10-26 15.18.27.png スクリーンショット 2020-10-26 15.21.05.png

Create a deployment group in the created application

The deployment group name is appropriate. For the service role, attach the role for CodeDeploy created earlier. スクリーンショット 2020-10-26 15.23.08.png

Select in-place as the deployment type. Set the environment setting EC "instance. Here, tags are used to determine which EC2 to deploy to. Deployed to all EC2 with the selected tag. Convenient for deploying to multiple EC2s at the same time.

スクリーンショット 2020-10-26 15.24.48.png

Specify the default deployment settings. I don't use Load balancer this time.

スクリーンショット 2020-10-26 15.30.06.png

Creating ʻappspec.yml and ʻApplicationStart.bash

I deploy the code in conjunction with the git repository, but in addition to the source code, I need to save a file that describes how to deploy in the root directory of the git repository. And the file name is fixed as ʻappspec.yml`. For specific writing, see here and [here] I referred to the article (https://dev.classmethod.jp/articles/code-deploy-appspec/). Thank you very much.

appspec.yml


version: 0.0
os: linux
files:
  - source: /
    destination: /home/ec2-user/simple-docker-pjt
    runas: ec2-user
permissions:
  - object: /
    pattern: '**'
    owner: ec2-user
    group: ec2-user
hooks:
  ApplicationStart:
    - location: scripts/ApplicationStart.bash
      runas: ec2-user

Also, you need to actually do docker-compose up after copying and installing the code on EC2 according to ʻappspec.yml. Describe it in ʻApplicationStart.bash. Please be careful about the -d part of docker-compose up -d. If you don't run it in the background, the deployment will never end.

./scripts/ApplicationStart.bash


#!/bin/bash -e
cd /home/ec2-user/simple-docker-pjt
sudo systemctl start docker
docker-compose up -d

Create a deployment

Select the application and click "Create Deployment". スクリーンショット 2020-10-26 15.35.34.png Select github for "Revision type" and enter your account name as the token name. And connect. Also, specify the repository name of the connection destination and the commit ID at any time you want to deploy. スクリーンショット 2020-10-26 15.37.32.png

If you create a deployment with this, you should be able to deploy without problems. I'm here. スクリーンショット 2020-10-26 17.12.02.png

Code Pipeline settings

Create a pipeline

Create a new pipeline from the CodePipeline console. If you set the pipeline name appropriately, the service role name will also be set automatically. スクリーンショット 2020-10-26 17.16.52.png

Select github (ver.1) for the source stage and connect to your account. スクリーンショット 2020-10-26 17.18.54.png

Select a repository and branch. スクリーンショット 2020-10-26 17.21.23.png

Skip the build stage (because this is a build-free language) スクリーンショット 2020-10-26 17.23.29.png

The deployment provider selects CodeDeploy and selects the application and deployment group you just created. スクリーンショット 2020-10-26 17.26.51.png

Add access to S3 to EC2.

In order for EC2 to use Code Pipeline, it needs access to S3. Therefore, add the ʻAmazonS3FullAccess` policy to the role that was attached to EC2 earlier. スクリーンショット 2020-10-26 17.30.21.png

The settings are now complete. Pushing the code to git will automatically deploy it to EC2. スクリーンショット 2020-10-26 17.09.52.png

Reference site

Kurokawa's youtube. I always refer to it. https://youtu.be/8mPm7jolnVk

Recommended Posts

Summary of Docker understanding by beginners ⑥ ~ Until automatic deployment of docker container to EC2 instance using CodeDeploy and CodePipeline ~
Summary of Docker understanding by beginners ⑤ ~ Until deploying docker container on EC2 instance ~
Summary of Docker understanding by beginners ④ ~ Until EC2 instance is started and docker is installed ~
Summary of Docker understanding by beginners ③ ~ Until proxying API using nginx ~
Summary of Docker understanding by beginners ② ~ docker-compose ~
Summary of Docker understanding by beginners ① ~ docker run -p ~
[Summary of technical books] Summary of reading "Introduction to Docker / Kubernetes Practical Container Development"