0
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 3 years have passed since last update.

DockerでRails + MySQLの環境構築

Posted at

概要

https://docs.docker.com/compose/rails/ をMySQL用に作り替える。

手順:

  1. 構成ファイルの準備
    1. Dockerfile作成
    2. Gemfile作成
    3. entrypoint.sh作成
    4. docker-compose.yaml作成
  2. ビルド
    1. railsプロジェクトの初期化
    2. imageの更新
  3. DBと接続
  4. 表示確認

構成ファイルの準備

Dockerfile作成

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs build-essential libpq-dev
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

# 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作成

プロジェクトルートにGemfileを作っておく

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

Gemfile.lock を作っておく

touch Gemfile.lock

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-compose.yaml作成

db_data としてvolumeを作って、DBデータを永続化

version: "3"
services:
  db:
    image: mysql:8.0
    volumes:
      - db_data:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password
    ports: 
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: root
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  db_data:

ビルド

Railプロジェクトつくる。

railsプロジェクトの初期化

webで rails new を実行して、railsを初期化。
Gemfileが自動的に更新される。

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

imageの更新

Gemfileが更新されたので、イメージを再度ビルド。

docker-compose build

DBと接続

config/database.yml にDB設定を書き込む

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

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

表示確認

コンテナ立ち上げ

docker-compose up -d

DB作成

docker-compose run web rake db:create

http://localhost:3000 に接続して動作確認。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?