LoginSignup
3
6

More than 5 years have passed since last update.

Dockerを使ってrailsアプリケーションを開発する1

Last updated at Posted at 2017-08-09

rubyもrailsもローカル環境には入っている必要はありません。all you need is Docker!
こちらを試してみました。
https://docs.docker.com/compose/rails/#connect-the-database

1. 必要な4ファイルの作成

Dockerfile, Gemfile, Gemfile.lock, docker-compose.ymlという4つのファイルを作成します。Gemfile.lockはempty fileで大丈夫です。

FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp
source 'https://rubygems.org'
gem 'rails', '5.0.0.1'
Gemfile.lock
docker-compose.yml
version: '3'
services:
  db:
    image: postgres
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
$ vim Dockerfile
$ vim Gemfile
$ touch Gemfile.lock
$ vim docker-compose.yml

2. Build the project

以下のコマンドでビルドを行います。

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

3. Connect the database

データベースに接続するために以下のようにdatabase.ymlを変更します。

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

development:
  <<: *default
  database: myapp_development


test:
  <<: *default
  database: myapp_test

その後、下記のコマンドを叩く

$ docker-compose up

これで、サービスが起動するので、別のターミナルを開いて以下のコマンドを実行する

$ docker-compose run web rake db:create

以上で、railsアプリケーションがとりあえず作成できて、http://localhost:3000 にアクセスできるようになっています。
スクリーンショット 2017-08-09 12.20.08.png

続きはこちら。
Dockerを使ってrailsアプリケーションを開発する2

3
6
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
3
6