LoginSignup
0
1

More than 1 year has passed since last update.

[有料級] Rails7(Turbo), Docker, Bootstrap5を使ったtodoリストの作り方 第0章-1 (環境構築編)

Last updated at Posted at 2023-03-30

はじめに

こちらの記事はシリーズの記事になります。
随時更新していくのでよろしくお願いします

バージョン確認

まずはバージョンを確認します。
バージョンについては以下の通りです

  • Docker: 20.10.23
  • Ruby: 3.1.3
  • Rails: 7.0.4.2
  • MySQL: 5.7

Dockerの環境構築

プロジェクトの作成

まずはプロジェクトの作成をします
任意の場所にディレクトリを作りまし、対象のディレクトリに移動します
※自分の場合ははdesktopに作成します

mkdir rails_todo_list
cd rails_todo_list

Dockerに必要なファイルの生成

次にDockerに必要なファイルを作りましょう
今回はDockerfile, Gemfile, Gemfile.lock, docker-compose.yml, entrypoint.shの5つのファイルを作ります。

touch Dockerfile Gemfile Gemfile.lock docker-compose.yml entrypoint.sh

各ファイルは次のように記述します

Dockerfile
FROM ruby:3.1
RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/' /etc/ssl/openssl.cnf
RUN 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
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs libxml2-dev libxslt1-dev yarn

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

RUN gem update --system
RUN bundle update --bundler

RUN bundle install
COPY . /myapp

# RUN yarn install --check-files

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>7.0.1'

Gemfile.lock
docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
    volumes:
       - ./db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: passw0rd
      MYSQL_DATABASE: myapp_development
    ports:
      - "3356:3306"

  app:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    stdin_open: true
    tty: true
    volumes:
      - .:/myapp
    ports:
      - "3050:3000"
    depends_on:
      - db

entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

こちらで一旦dockerの準備は終了です。

Dockerを使ったRailsの環境構築

Railsアプリの生成

下記コマンドを入力してDocker経由でrailsのアプリを作ります。

docker-compose run app rails new . --no-deps --force --database=mysql
  • オプションの説明
    --no-deps 
    Railsアプリケーションで指定されたGemの依存関係を解決せずに、指定されたコマンドを実行する

    --force
    強制的な上書きや削除を許可

    --database=mysql
    データベースをMysqlに指定

ビルドコマンドの実行

ビルドしておきます

docker-compose build

DBの設定

docker-compose.ymlにてDBの名前とパスワードを指定しているため下記ファイルで設定を変更します

config/database.yml 12から18行目
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: passw0rd
  host: db

コンテナの起動

docker-compose up -d

railsのDBの作成

docker-compose exec app rails db:create

ここまでできたので一度動作確認します
http://127.0.0.1:3050/
こちらに一度アクセスしてみてください

こちらのような画面が出てきたら成功です。
スクリーンショット 2023-03-30 18.45.37.png

今回はここまで!

続けて読んでもらうとありがたいです!

[有料級] Rails7(Turbo), Docker, Bootstrap5を使ったtodoリストの作り方第0章-2(Bootstrap5導入編)

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