Now that I've finally started learning Docker, I'll summarize my understanding.
git clone
the repository of github and git pull
+ docker-compose up
every time it was updated.CodeDeploy
CodePipeline
to automate the above sequence of steps
And this time too, docker has nothing to do with it. ..
At the time of two times before, the EC2 instance was started and docker
and docker-compose
were installed.
CodeDeploy Agent
on your EC2 instanceCodeDeploy
settings and ʻApplicationStart.bash
CodePipeline
settingsCode Deploy Agent
on your EC2 instanceHere 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.
CodeDeploy
settingsSelect EC2 as the platform
Same procedure as creating a role for EC2 earlier.
The deployment group name is appropriate. For the service role, attach the role for CodeDeploy created earlier.
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.
Specify the default deployment settings. I don't use Load balancer this time.
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
Select the application and click "Create Deployment". 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.
If you create a deployment with this, you should be able to deploy without problems. I'm here.
Code Pipeline
settingsCreate a new pipeline from the CodePipeline console. If you set the pipeline name appropriately, the service role name will also be set automatically.
Select github (ver.1) for the source stage and connect to your account.
Select a repository and branch.
Skip the build stage (because this is a build-free language)
The deployment provider selects CodeDeploy
and selects the application and deployment group you just created.
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.
The settings are now complete. Pushing the code to git will automatically deploy it to EC2.
Kurokawa's youtube. I always refer to it. https://youtu.be/8mPm7jolnVk
Recommended Posts