LoginSignup
15

More than 5 years have passed since last update.

windows + docker-compose + rails環境 (rails 5.0.0) を構築する

Posted at

windows + docker-compose + rails環境

docker-composeなど入ってる前提です。

1 最低限準備するファイル

  • Dockerfile
  • docker-compose.yml
  • Gemfile
  • Gemfile.lock(空)

2 それぞれのファイルの内容

  • Dockerfile (docker-hubの公式railsにある onbuildを改良)

    ※ 公式の物に関しては ONBUILD で bundlerを利用している為、docker-composeにあるチュートリアルのような
    形でrails new などを行おうとすると bundle in bundle 固有のエラーがおこる。※参照

# FROM rails:onbuild

FROM ruby:2.3

# throw errors if Gemfile has been modified since Gemfile.lock
# RUN bundle config --global frozen 1

# ------- add
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client

# ------ add and modify
WORKDIR /tmp
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install

RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app

# ONBUILD COPY Gemfile /usr/src/app/
# ONBUILD COPY Gemfile.lock /usr/src/app/
# ONBUILD RUN bundle install


# COPY . /usr/src/app

RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*

# pos
# RUN apt-get update && apt-get install -y mysql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*

# EXPOSE 3000
# CMD ["rails", "server", "-b", "0.0.0.0"]
いくつか、ゴミっぽいなと思うファイルとか残ってますが、とりあえずはこの状態で動作するので良しとします。
  • docker-compose.yml

    onbuildの中にあったrailsのビルトインサーバーをこちらに記述しています。
    また、postgresコンテナとのリンクを記述してあります。

version: '2'
services:
  db:
    image: postgres
    expose:
      - "5432"
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000"
    depends_on:
      - db

この設定でrails new まで行えた場合はrailsプロジェクト内にある
config/database.ymlの developmentに

username: postgres
password: password
host: 生成したdbコンテナの名前

の設定が必要になります。
  • Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.6'
とか。5.0.0以降の場合は gem 'rails', '>= 5.0.0.beta3', '< 5.1' とか
  • Gemfile.lock(空)

3 コンテナ作成からプロジェクト作成までの流れ

3-1 コンテナイメージの作成

docker-compose build

3-2 rails new . / railsプロジェクトの作成

※これでエラーが出る場合
docker-compose run -d web rails new . --force --database=postgresql --skip-bundle
↓
※こちらで対処
docker-compose run -d web bundle exec rails new . --force --database=postgresql --skip-bundle

3-3 rails new したあとで、再度 docker-compose buildし直す

このままだと gemが色々無いよと言われるので。

3-4 /config/database.ymlの修正

config/database.ymlの developmentに

username: postgres
password: password
host: 生成したdbコンテナの名前

コンテナ名のパターンは、 プロジェクトフォルダの名前に依存してます。
railstest フォルダなら -> railstest_db_1

3-5 docker-compose up

docker-compose up

これで一度コンテナを立ち上げる。まだdatabaseのcreateとmigrateを実行してないのでもちろん
エラーにはなるが。

docker-compose run -d web bundle exec rake db:setup

3-6 finish!

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
15