LoginSignup
0
0

More than 3 years have passed since last update.

Rails 開発/実行用の Docke-compose と Dockefile (2019 Dec 最新)

Posted at

はじめに

rails 開発用の Dockerfile, Docker-compose が毎回互換性問題で動かなくなるので現時点で動作するモノを覚え書きしておく。

細かい解説は
https://qiita.com/saitoeku3/items/b1aa2ae143624e551aea
https://qiita.com/azul915/items/5b7063cbc80192343fc0
あたりを参照

このページではこれら読んだ人が利用可能であろう、 Dockerfile と Docker-composeを提供する

デフォルトでとりあえず動くモノ

dockerfile

FROM ruby:2.6

ENV LANG C.UTF-8

RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       libpq-dev
# 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
# Node.jsをインストール
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - && \
    apt-get install -y nodejs

# 作業ディレクトリの設定
RUN mkdir /app
ENV APP_ROOT /app
WORKDIR $APP_ROOT

# gemfileを追加する
ADD ./src/Gemfile $APP_ROOT/Gemfile
ADD ./src/Gemfile.lock $APP_ROOT/Gemfile.lock

# gemfileのinstall
RUN bundle install
ADD ./src/ $APP_ROOT

docker-compose

version: '3'

services:
  db:
    image: mysql:5.7
    volumes:
      - mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: root
    ports:
      - "3306:3306"

  web:
    build: .
    command: rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./src:/app
      - bundle:/usr/local/bundle
    ports:
      - "3000:3000"
    links:
      - db
volumes:
  bundle:
  mysql_data:

利用手順

mkdir src
# make Gemfile
cat <<'EOF' > src/Gemfile
source 'https://rubygems.org'
gem 'rails'
EOF
touch src/Gemfile.lock

docker-compose build
docker-compose run web bundle exec rails new . --force --database=mysql

# BSD sed の場合(Macなど) GNU sed なら '.bak'を消す
sed -i '.bak' 's/password:$/password: root/' src/config/database.yml
sed -i '.bak' 's/host: localhost/host: db/'  src/config/database.yml

docker-compose run web rake db:create
# webpackのinstall
docker-compose run web rails webpacker:install

docker-compose up

webpack不要の場合

今回作りたいのが純粋なAPIサーバで、フロントエンドを作る必要が無かったので、webpack周りを全部無くしてみた。

Dockerfile

FROM ruby:2.6

ENV LANG C.UTF-8

RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       libpq-dev

# 作業ディレクトリの設定
RUN mkdir /app
ENV APP_ROOT /app
WORKDIR $APP_ROOT

# gemfileを追加する
ADD ./src/Gemfile $APP_ROOT/Gemfile
ADD ./src/Gemfile.lock $APP_ROOT/Gemfile.lock

# gemfileのinstall
RUN bundle install
ADD ./src/ $APP_ROOT

docker-compose

変更なし

利用手順

mkdir src
# make Gemfile
cat <<'EOF' > src/Gemfile
source 'https://rubygems.org'
gem 'rails'
EOF
touch src/Gemfile.lock

docker-compose build
# --api を追加。 apiモードにしたくなければ、 --skip-javascript
docker-compose run web bundle exec rails new . --force --database=mysql --api

# BSD sed の場合(Macなど) GNU sed なら '.bak'を消す
sed -i '.bak' 's/password:$/password: root/' src/config/database.yml
sed -i '.bak' 's/host: localhost/host: db/'  src/config/database.yml

docker-compose run web rake db:create

docker-compose up
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