4
10

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でRuby On Rails + Nginx + Unicorn + MySQLの環境を作る

4
Posted at

この記事のゴール

docker-composeでweb用、db用の2つのコンテナーを作成して一括管理できる状態をゴールとします。
ホストPCにはRailsもNginxなど何もインストールしないで開発可能となります。

前提環境

・mac OS X 10.9.5
・docker-compose を使える。
・プロジェクトのルートは「/Users/user/develop/docker_rails」とします。
以下、適宜読み替えて下さい。

構築手順

1.下記5つのファイルを準備
Gemfile
source 'https://rubygems.org'
gem 'rails'
Gemfile.lock
(空でOK)
Dockerfile
FROM rails:4.2.6
ENV LANG C.UTF-8

# nginx,vim をインストール
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs vim nginx

# nginx に unicornの設定ファイルをコピー
ADD ./container/nginx/unicorn.conf /etc/nginx/conf.d/unicorn.conf

# Gemfile bundle install
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install

ENV APP_HOME /var/www/docker_rails
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
ADD . $APP_HOME
docker-compose.yml
version: '2'
services:
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: password
    privileged: true
  web:
    build: .
    command: /bin/bash -c "nginx && bundle exec unicorn -c config/unicorn.rb -D && while true; tail -f /dev/null"
    volumes:
      - .:/var/www/docker_rails
    ports:
      - "80:80"
    depends_on:
      - db
    privileged: true
container/nginx/unicorn.conf
upstream unicorn {
  server unix:/var/www/unicorn.sock fail_timeout=0;
}

server {
  listen 80;
  server_name 192.168.99.100;

  root /var/www/docker_rails/public;

  try_files $uri @unicorn;

  location @unicorn {
    proxy_set_header Host $http_host;
    proxy_pass http://unicorn;
  }
}
2.rails new してファイルを作成。
$ docker-compose run web rails new . --force --database=mysql --skip-bundle

dockerコンテナー内にRailsファイルが作成されホストPCにコピーされている事を確認する。

3.docker-compose run で作ったコンテナー/イメージを削除

3でdocker-compose run を行ったのはホストPCにrails newしたファイルをコピーする事が目的だったので、製造されたimageとcontainerは削除します。

4.ファイルを修正

Gemfile
# 一番下の下記2行を追記
gem 'unicorn'
gem 'therubyracer', platforms: :ruby
config/database.yml
# password,hostを修正

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: password
  host: db

development:
  <<: *default
  database: docker_rails_development

test:
  <<: *default
  database: docker_rails_test

production:
  <<: *default
  database: docker_rails_production
  username: docker_rails
  password: <%= ENV['DOCKER_RAILS_DATABASE_PASSWORD'] %>
config/unicorn.rb
# 下記内容で作成

pid '/var/www/docker_rails/tmp/unicorn.pid'
listen '/var/www/unicorn.sock'
stderr_path '/var/www/docker_rails/log/unicorn.log'
stdout_path '/var/www/docker_rails/log/unicorn.log'
worker_processes 2
timeout 30

5.下記コマンドでimageを作成

$ docker-compose build

web用、db用2つのimageが作成されたか確認

$ docker images

6.下記コマンドでimageからcontainerを作成

docker-compose up

※webコンテナーが立ち上がらない時は/tmp/unicorn.pid を削除する

7.docker-compose up が途中で止まるのでターミナルの別タブからDBを作成

web用、db用2つのcontainerが動いているか確認

$ docker ps

動いていない場合はcontainer IDを調べる

$ docker ps -a

containerを起動する。

$ docker start {container ID}

webのcontainerにログイン

docker exec -it {webのcontainer ID} /bin/bash

ログイン後、下記コマンドでDBを作成

rake db:create

8.ブラウザからアクセス

ホストPCのコンソールから下記でIPを確認

$ docker-machine ip

下記URLでブラウザからアクセス
http://{ip}

RAILSのhomeが表示されれば完了です。

4
10
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
4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?