0
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でRailsの開発環境を作成してみた

0
Posted at

はじめに

仕事でDockerを使用することが多いのだが正直よく理解出来ていない。手順書のコードをコピペするだけ状態なので、勉強も兼ねてRuby on Railsの開発環境を作成してみました。その時の手順を記載していきます。

参考:
Quickstart: Compose and Rails

作業環境

  • OS: macOS Mojave 10.14.4
  • Docker: 18.09.2

手順

ルートディレクトリを作成する

$ mkdir app

MySQLのデータをマウントするディレクトリを作成する

$ mkdir -p db/mysql

作成したルートディレクトリに移動する

$ cd app

Dockerfileを作成する

Dockerfile
FROM ruby:2.6.2-stretch
RUN apt-get update && apt-get install -y nodejs mysql-client
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

Gemfileを作成する

Gemfile
source 'https://rubygems.org'
gem 'rails', '5.2.2'

空のGemfile.lockを作成する

touch Gemfile.lock

docker-compose.ymlを作成する

docker-compose.yml
version: '3'
services:
  mysql:
    image: mysql:8.0.15
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - "./db/mysql:/var/lib/mysql"
    environment:
      MYSQL_ROOT_PASSWORD: root
  app:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ".:/app"
    ports:
      - "3000:3000"  
    tty: true
    depends_on:
      - mysql

Railsのプロジェクトを作成する

docker-compose run app rails new . --force --database=mysql

ビルドを実行

docker-compose build

config/database.ymlを修正する

database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: root
  host: mysql

コンテナを起動する

docker-compose up

データベースを作成する

docker-compose run app rake db:create

http://localhost:3000 にアクセスする

下記のような画面が表示されれば完了です。
スクリーンショット 2019-04-14 16.38.19.png

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