5
1

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 1 year has passed since last update.

今年の振り返りAdvent Calendar 2023

Day 11

dockerでrails7(API)を構築

Posted at

はじめに

この記事は2023年度の振り返りです。

今回、electronアプリ構築に伴いのバックエンドをrails(API)で新規で構築したので、それを備忘録です。

ディレクトリ構成

compose.yml
api(ディレクトリ)
 ├ Dockerfile
 ├ Gemfile
 └ Gemfile.lock

準備

ファイル生成
touch compose.yml
mkdir api/
touch api/Dockerfile.dev
touch api/Gemfile
touch api/Gemfile.lock
compose.yml
services:
  api:
    build:
      context: ./api
      dockerfile: Dockerfile.dev
    container_name: api
    ports:
      - 3000:3000
    environment:
      RAILS_ENV: development
      DB_HOST: mysql
      DB_NAME: root
      DB_PASSWORD: password
    depends_on:
      - mysql
    volumes:
      - ./api:/api

  mysql:
    image: public.ecr.aws/docker/library/mysql:8.1
    container_name: mysql
    volumes:
      - mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      TZ: "Asia/Tokyo"

volumes:
  mysql_data:
Dockerfile.dev
FROM public.ecr.aws/docker/library/ruby:3.2.2-slim as base

WORKDIR /api
ENV LANG=C.UTF-8 \
  TZ=Asia/Tokyo


#################
# build
#################
FROM base as build

RUN apt-get update -qq && \
  apt-get install --no-install-recommends -y build-essential default-libmysqlclient-dev git libvips pkg-config && \
  apt-get clean && \
  rm -rf /var/cache/apt/archives/* && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY Gemfile Gemfile.lock /api/

RUN gem update --system \
  && gem install bundler
RUN bundle install

COPY . /api

#################
# development
#################
FROM base as development

RUN apt-get update -qq && \
  apt-get install --no-install-recommends -y build-essential default-libmysqlclient-dev git libvips pkg-config && \
  apt-get clean && \
  rm -rf /var/cache/apt/archives/* && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY Gemfile Gemfile.lock /api/

COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /api /api

EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Gemfile
source 'https://rubygems.org'
gem 'rails'

プロジェクト構築

コマンド
docker compose build
rails newのコマンド
docker compose run --rm api rails new . --force --database=mysql --api \
--skip-action-mailer \
--skip-action-mailbox \
--skip-action-text \
--no-skip-active-job \
--skip-action-cable
database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("DB_NAME") %>       # ←この行を修正
  password: <%= ENV.fetch("DB_PASSWORD") %>   # ←この行を修正
  host: <%= ENV.fetch("DB_HOST") %>           # ←この行を修正

以下のコマンドでrailsのトップページが表示されるか確認(一部過剰ですが)

コマンド
docker compose build
docker compose run --rm api bundle exec rails db:drop db:create db:migrate db:seed
docker compose up

さいごに

dockerに関しては、railsで自動生成されるので開発環境については、さらっとした感じに作成しました。
今後の記事はrails多めになります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?