LoginSignup
0
0

DockerでRails環境構築

Posted at

スクリーンショット 2023-06-23 162947.png
フォルダ内構造は画像の通り
docker-compose.yaml
Dockerfile
srcフォルダ内にGemfile

Gemfile

source 'https://rubygems.org'
gem 'rails', '~> 7.0.4'

Dockerfile

FROM ruby:3.1
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 \
  && apt-get update -qq \
  && apt-get install -y nodejs yarn
WORKDIR /app
COPY ./src /app
RUN bundle config --local set path 'vendor/bundle' \
  && bundle install

docker-compose.yaml

version: '3'
services:
  db:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./src/db/mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./src:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

Dockerfileのあるディレクトリ内で以下コマンド

rails 雛形作成

docker compose run web rails new . --force --database=mysql

ファイル修正 src\config\database.yml

password: password
host: db

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

再度イメージ作成

docker compose build

データベース作成

docker compose run web rails db:create

コンテナ作成

docker compose up -d

完成

あとは以下で操作&削除

コンテナ内に入る

docker compose exec web /bin/bash

コンテナ削除

docker compose down
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