2
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 5 years have passed since last update.

[Docker知識不要] Ruby on railsの環境をDocker上に作る方法

Last updated at Posted at 2019-08-11

ruby on railsの環境をDocker上に作る方法

こんにちは、ITエンジニアの田中です!
2019年11月30日時点動作することを確認しています!

この記事を読むと下記の事ができるようになります。

  • Docker上でRuby on Railsの環境を作れる。

下記のことが事前に完了していることが前提です。

  • Dockerがインストールされている

インストールされる各アプリのバージョン

Ruby 2.6.1
Rails 5.2.3
nodejs v10.16.3

フォルダ構成

  • 任意フォルダ
    • src
      • Gemfile
      • Gemfile.lock
    • Dockerfile
    • docker-compose.yml

Gemfile

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

Gemfile.lock

空ファイル

Dockerfile

FROM ruby:2.6.1
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*

RUN mkdir /myapp
WORKDIR /myapp
ADD src/Gemfile /myapp/Gemfile
ADD src/Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

docker-compose.yml

version: '2'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"      
    volumes:
      - ./src:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

コマンド

docker-compose run web rails new . --force --database=postgresql
docker-compose build

config/database.ymlの編集

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

データベースの作成

docker-compose run web rake db:create

Dockerの起動

docker-compose up

RubyOnRails.png

注意点

Gemfileを更新した場合は、下記のコマンドを実行してイメージを作り直してください。

docker-compose build

理由としては、docker-compose run web bundle installしても、既存のコンテナーに対して実行されるため、docker-compose upすると新しいコンテナーを生成してしまうため、リセットされてしまいます。

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