0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker Compose ECSデプロイを試す

Last updated at Posted at 2021-03-05

はじめに

この記事の環境は以下のGitHubにあります。
https://github.com/bocci-farm/docker-rails

AWSにテスト環境を構築

テスト環境用のVPC, パブリックネットワーク, ECRリポジトリを作成します。

% cd terraform
% terraform init
% terraform plan
% terraform apply
% cd -

ECR用のdocker-compose.ecr.ymlを作成

envsubstを使ってテンプレートから実ファイルを作成します。
ここではECRを使っていますが、通常のDockerHubでもECSデプロイは可能です。

% export AWS_ACCOUNT_ID=$(aws sts get-caller-identity | jq -r '.Account')
% export AWS_VPC_ID=$(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=example' | jq -r '.Vpcs[].VpcId')
% envsubst < docker-compose.ecr.yml.envsubst > docker-compose.ecr.yml 

Dockerイメージをビルド

% DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose -f docker-compose.ecr.yml build --build-arg RAILS_MASTER_KEY=<RAILS_MASTER_KEY>

DockerイメージをECRへプッシュ

% aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com
% docker-compose -f docker-compose.ecr.yml push

Docker Contextを作成

context名、プロファイルは環境に合わせて指定してください。

% docker context create ecs bocci-ecs --profile bocci-farm

ECSへデプロイ

% docker compose --context bocci-ecs up -f docker-compose.ecr.yml  # しばらく待ちます

確認

すべてのサービスのSTATEがRunningになっていることを確認。

% docker compose --context bocci-ecs ps
NAME                                                 SERVICE             STATE               PORTS
task/docker-rails/19ce049d7e8f48b499200544ea94c5d4   app                 Running
task/docker-rails/2cdc1ad531744f1b8070e851f896347a   web                 Running             docke-LoadB-1W7QCVZLGWYJX-630195466.ap-northeast-1.elb.amazonaws.com:80->80/http
task/docker-rails/97497a911a0247cfba86e3fe7278ece4   db                  Running

ブラウザから確認します。(URLは上記コマンドのwebサービスのPORTS欄にあります)
50xエラーになる場合は、LBの設定がまだ終わっていないかもしれないのでAWSコンソールより確認する。
大体はしばらく待てば解消されると思います。

http://docke-loadb-1w7qcvzlgwyjx-630195466.ap-northeast-1.elb.amazonaws.com
スクリーンショット 2021-03-05 18.10.57.png

後片付け

% docker compose --context bocci-ecs down
% terraform -chdir=terraform destroy

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?