LoginSignup
2
2

More than 3 years have passed since last update.

rails6の環境をdockerとmysqlで構築(メモ)

Last updated at Posted at 2020-08-10

はじめに

個人用のメモです。
業務でほとんどdocker使わないので、忘れてしまいそうなので。。。
また、パーフェクトRuby on Railsを参考にしています。

ファイル構成

/app
|--- docker-compose.yml
|--- Dockerfile
|--- Gemfile
|--- Gemfile.lock
|--- .env

Rails用のイメージを作成

ファイル

Dockerfile
FROM ruby:2.7.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 \
    && apt-get install -y build-essential nodejs yarn

RUN gem install bundler
WORKDIR /app
RUN bundle config set path vendor/bundle

CMD ["bash"]

コマンド(コンテナが作れるか確認)

$ docker build .
$ docker images
$ docker volume create app_bundle
$ docker volume create app_node_modules
$ docker colume ls
$ docker run -it -v <現在のディレクトリの絶対パス>:/app \
  -v app_bundle:/app/vendor/bundle \
  -v app_node_modules:/app/node_modules \
  -p 3000:3000 <IMAGE ID>

### コンテナの中 ###
# bundle install
# bundle exec rails new . --force --webpack=vue --skip-turbolinks --skip-test -d mysql
# bin/rails webpacker:install
# bin/rails s -b 0.0.0.0
# エラーになるが繋がったのを確認

コンテナが作れるの確認して作成したコンテナとイメージを削除し、新しくイメージを作成しました。
DockerfileDockerfile.devに変更しています。

docker-composeの作成

ファイル

docker-compose.dev.yml
version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - '3000:3000'
    volumes:
      - .:/app
      - app_bundle:/app/vendor/bundle
      - app_node_modules:/app/node_modules
    tty: true
    stdin_open: true

volumes:
  app_bundle:
    driver: local
  app_node_modules:
    driver: local
.env
COMPOSE_FILE=<docker-compose.dev.ymlのファイルのパス>
Gemfile
### bundle initで作成 railsのバージョンのみ変更###

# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'rails', '~> 6'

コマンド

$ docker-compose up -d
$ docker-compose ps
$ docker-compose exec web bash

### コンテナ内 ###
# bundle
# bundle exec rails new . --force --webpack=vue --skip-turbolinks --skip-test -d mysql
# bin/rails s -b 0.0.0.0

Mysql用のコンテナ作成

docker-compose.ymlに追記。

ファイル

docker-compose.yml
version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    environment:
      MYSQL_USERNAME: app_dev_user
      MYSQL_PASSWORD: password
      MYSQL_HOST: mysql
    ports:
      - '3000:3000'
    volumes:
      - .:/app
      - app_bundle:/app/vendor/bundle
      - app_node_modules:/app/node_modules
    tty: true
    stdin_open: true
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    ports:
      - 3306:3306
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  app_bundle:
    driver: local
  app_node_modules:
    driver: local
  mysql-data:
    driver: local

コマンド

$ docker-compose up -d mysql
$ docker-compose exec mysql mysql -u root -p

### コンテナ内(mysql) ###
mysql> create user app_dev_user@"%" identified by 'password';
mysql> grant all on app_development.* to app_dev_user@"%";
mysql> grant all on app_test.* to app_dev_user@"%";

RailsとMysqlを繋ぐ

database.ymlの修正

database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("MYSQL_USERNAME") { app_dev_user } %>
  password: <%= ENV.fetch("MYSQL_PASSWORD") { password } %>
  host: <%= ENV.fetch("MYSQL_HOST") { mysql } %>

DBの作成

$ docker-compose exec web bash

### コンテナ内(Rails) ###
# bin/rails db:create
# bin/rails s -b 0.0.0.0

いつもの画面が出れば完成。

2
2
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
2
2