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

[Docker]dockerでRails5.2環境構築

Last updated at Posted at 2020-10-10

前提

dockerとdocker-composeインストール済

##1. fileを用意
Gemfile, Gemfile.lock, Dockerfile, Docker-compose.ymlを~/Desktop/product-register下におく。

###1. Dockerfile

FROM ruby:2.5
RUN apt-get update && apt-get install -y \
      build-essential \
      libpg-dev \
      nodejs \
      postgresql-client \
      yarn
WORKDIR /product-register
COPY Gemfile Gemfile.lock /product-register/
RUN bundle install

###2. Docker-compose.yml

version: '3'
services:
  web:
    build: .    // imageとってくる場合は image: イメージ名
    ports:
      - '3000:3000'
    volumes:
      - '.:/product-register'
    tty: true
    stdin_open: true

注意点: インデントやスペースがシビア

###3. Gemfile

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

##2. コンテナを作ってRailsアプリを作成

$ docker-compose up -d 
$ docker-compose exec web bash
:/product-register# rails new . --force --database=postgresql -skip-bundle // gemfileが更新されてしまう
$ docker-compose down // gemfileを更新するためにコンテナを一度落とす
$ docker-compose up --build -d  新しくdockerfileをbuildしたimageを使うために--buildをつける
$ docker-compose exec web bash //再度入る
:/product-register# rails s -b 0.0.0.0 // dbの設定がまだなのでエラーが出る

##3. DBの設定

1. 作成されたアプリのconfig/database.ymlの修正

adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: db                  
  user: postgres       
  port: 5432
  password: <%= ENV.fetch("DATABASE_PASSWORD") %> 

2. docker-compose.yml ファイルのdb部分の追記

version: '3'

volumes:
  db-data:             // docker volumeを作っている
services:
  web:
    build: .
    ports:
      - '3000:3000'
    volumes:
      - '.:/app'
    environment:
      - 'DATABASE_PASSWORD=postgres'
    tty: true
    stdin_open: true
    depends_on:
      - db
    links:
      - db
  
  db:
    image: postgres
    volumes:
      - 'db-data:/var/lib/postgresql/data'    //上で作ったdockervolumeをマウントしてる
    environment:                      
      - 'POSTGRES_PASSWORD=postgres'   //最近必要になったらしい
0
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
0
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?