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

【Mac】Docker+rails6+MySQLでの環境構築完全版

Last updated at Posted at 2021-09-06

環境

MacOS Big Sur 11.5.2
ruby 3.0.2
rails 6.1.4
Docker 20.10.8
MySQL 8.0.23
ディレクトリ myapp
全て現時点(2021/09/06)での最新バージョンです。

1.用意するファイル

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

Dockerfile
FROM ruby:3.0.2
RUN apt-get update -qq && apt-get install -y nodejs

# yarnパッケージ管理ツールをインストール
# https://classic.yarnpkg.com/en/docs/install/#debian-stable
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn

WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# 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"]

ここでyarnをインストールしておくのがポイントです。しないと後でwebpackerがインストールできずエラーになります。

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

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.yml
version: "3"

services:
  db:
    image: mysql:8.0

    command: mysqld --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    ports:
        - 3306:3306
    volumes:
      - ./tmp/db:/var/lib/mysql
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      MYSQL_HOST: db
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

2.実行

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

$ docker-compose build

ここでdatabase.ymlを編集します

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

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

パスワードなど設定した後データベースと接続します。

$ docker-compose run web rake db:create

$ docker-compose up

終わったらlocalhost:3000とネットで検索すればこの画面が出てきます。
そしたら環境構築成功です!

スクリーンショット 2021-09-06 13.25.32.jpeg

おわりに

未経験でしかも初心者なのでかなり苦労しましたが必要な情報は以上だけでした
一人でも多くの方の役に立てば嬉しいです
【Mac】Docker+rails6+postgreSQLでの環境構築完全版
PostgreSQLバージョンも作ったのでよかったら参考にしてください!

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