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

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

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
postgreSQL 13.4
ディレクトリ myapp
全て現時点(2021/09/06)での最新バージョンです。

1.用意するファイル

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

Dockerfile
FROM ruby:3.0.2
  # yarnパッケージ管理ツールをインストール
  RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
  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 yarn
  RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
  RUN mkdir /myapp
  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: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
       - POSTGRES_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:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

2.実行

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

$ docker-compose build

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

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

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

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

$ docker-compose run web rake db:create

$ docker-compose up

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

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

おわりに

【Mac】Docker+rails6+MySQLでの環境構築完全版
MySQLバージョンも作ったのでぜひ参考にしてください!

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