0
0

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.

Rails6.0.1 + MySQL8.0.18をDockerで動かす(メモ)

Posted at

Rails6.0.1 + MySQL8.0.18Dockerで起動できたので、メモを残したいと思います。
本当に起動しただけなので、問題があるかもしれません。

  • 開発環境
  • macOS Catalina 10.15.1
  • Docker Version 2.1.0.5(40693)

プロジェクトディレクトリの下にファイルを用意する

myapp/
 ├ .env
 ├ docker-compose.yml
 ├ Dockerfile
 ├ Gemfile
 └ Gemfile.lock

.env
MYSQL_PASSWORD=password
docker-compose.yml
version: '3'
services:
  db:
    image: mysql:8.0.18
    command: "--default-authentication-plugin=mysql_native_password"
    environment:
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - '3316:3306'
    volumes:
      - ./db/mysql/volumes:/var/lib/mysql

  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    env_file: .env
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

MySQL8から認証方式が変わり、下記の行が必要に。
command: "--default-authentication-plugin=mysql_native_password"

Dockerfile
FROM ruby:2.6.3
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
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

Rails6からwebpackerが標準になったことで、yarnのインストールが必要に。

Gemfile
source 'https://rubygems.org'
gem 'rails', '6.0.1'
Gemfile.lock

rails new

$ docker-compose run web rails new . --force --database=mysql --no-deps --skip-test --webpacker

--webpackerでwebpackerを入れている。

database.ymlを修正

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

db:create

$ docker-compose run web rake db:create

起動

$ docker-compose up

http://localhost:3000にアクセス
スクリーンショット 2019-12-10 22.46.40.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?