1
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.

rails + docker + fargateでのアプリデプロイへの道

1
Posted at

各ファイルの設定

ファイルのディレクトリ-

src(appを格納)
Dockerfile
docker-compose.yml
entrypoint.sh
start.sh

Dockerfile

FROM ruby:3.0.1

ENV RAILS_ENV=production

RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn

RUN apt-get install -y nodejs npm && npm install n -g && n 14.19.1


WORKDIR /app
COPY ./src /app
RUN bundle config --local set path 'vendor/bundle' \
  && bundle install

RUN yarn install --check-files
RUN bundle exec rails webpacker:compile

COPY start.sh /start.sh
RUN chmod 777 /start.sh
CMD ["sh", "/start.sh"]

docker-compose.yml

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./src/db:/var/lib/postgres
    environment:
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=postgres
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./src:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: development
    depends_on:
      - db

entrypoint.sh

#!/bin/bash
set -e

rm -f /task3_juhatyu/tmp/pids/server.pid

exec "$@"

start.sh

#!/bin/sh

if [ "${RAILS_ENV}" = "production" ]
then
    bundle exec rails assets:precompile
fi

bundle exec pumactl start

datebase.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: password
  host: db

development:
  <<: *default
  database: app_name_development

test:
  <<: *default
  database: app_name_test

production:
  <<: *default
  database: app_name_production
  username: app_name
  password: <%= ENV['TIMER_APP2_DATABASE_PASSWORD'] %>

terminalでの操作

docker image build .
docker compose run web rails db:create
docker compose run web rails db:migrate
docker compose run web rails db:seed

AWSで作成するもの

1.VPCとサブネットの作成
2.internet gatewayとルートテーブルの設定
3.ECRにインスタンスを作成
4.ECSのクラスターとタスクの作成

1.VPCとサブネットの作成

VPC:インターネット上のどこを使うか
サブネット:VPC内を分ける

VPCの設定
Name tag - optional: app_name
IPv4 CIDR: 192.168.1.0/24
サブネットの設定
VPC ID: VPCで作成したものを設定
subnet name: app_name-subnet-01
Availability Zone: 1aなどを選ぶ
IPv4 CIDR block: 192.168.1.0/24

2.internet gatewayとルートテーブルの設定

Internet gateway
name: app_name-ing

1.設定したingを選択
2.actionのattach to VPCで作成したVPCを選択
3.route table の作成
4.vpcとサブネットの関連付け

3.ECRにインスタンスを作成

ECR
Repository name: app_name

1.view push commandsを実行
(2は上記でbuildしているので省略)
1
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
1
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?