LoginSignup
36
40

More than 3 years have passed since last update.

DockerでNginx+unicorn+rails+Mysqlの開発環境を作ってみた

Last updated at Posted at 2020-04-25

はじめに

今回はタイトル通りの開発環境を構築したので備忘録的にまとめる為に記事にしました。
既存のrailsプロジェクトにこの開発環境を導入する記事になります。新しいrailsプロジェクトの場合は少し手順が変わりますが同じように環境を構築できると思います。

参考させていただいた記事等

世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで
こちらの記事を参考にAWSにアプリをデプロイしていたのでunicoronの設定を同じものにしています。
nginxの設定ファイルはこちらを参考にさせていただきました。
Nginx設定のまとめ
nginxについてまとめ(設定編)

構成

コンテナはrailsアプリ用、Mysql用、nginx用の3つを作成します。
rails+Mysqlの環境についてはこちらの記事で紹介してます。
Dockerを使用して既存のRailsプロジェクト開発環境構築してみた
ファイル構成はこのようになります。

.(既存railsプロジェクト)
├── Dockerfile
├── docker-compose.yml
├── Gemfile
├── Gemfile.lock
├── config
|   └──datebase.yml
|   └──unicorn.conf.rb
└── nginx
     ├──Dockerfile 
     └──nginx.conf

1.Dockerfile(rails)

#既存のプロジェクトのrubyのバージョンを指定
FROM ruby:2.6.3 
dockerizeパッケージダウンロード用環境変数
ENV DOCKERIZE_VERSION v0.6.1

#パッケージの取得
RUN apt-get update && \
    apt-get install -y --no-install-recommends\
    nodejs  \
    mariadb-client  \
    build-essential  \
    wget \
    && wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /myproject

COPY Gemfile /myproject/Gemfile
COPY Gemfile.lock /myproject/Gemfile.lock

RUN gem install bundler
RUN bundle install

COPY . /myproject

dbが立ち上がる前にunicornコマンドを行わないようにdockerizeをdocker-composeで使用するのでここでダウンロードしています。こちらの記事も参照いただければと思います。
dockerizeでコンテナが立ち上がる順番を制御してみた

2.dockerfileと設定ファイル(nginx用)

FROM nginx:stable
#デフォルトのnginxファイルを削除して作成したものコンテナないにコピー
RUN rm -f /etc/nginx/conf.d/*

COPY nginx.conf /etc/nginx/conf.d/myapp.conf

#-c以降の設定ファイルを指定して起動 daemon offでフォアグラウンドで起動
CMD /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
nginx.conf
upstream unicorn {
  #ユニコーンソケットの設定
  server unix:/myproject/tmp/sockets/.unicorn.sock fail_timeout=0;
}

server {
  #IPとポートの指定
  listen 80 default;
  #サーバーネームの指定
  server_name localhost;

  access_log /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log;
  #ドキュメントルートの指定
  root /myproject/public;

  client_max_body_size 100m;
  error_page 404             /404.html;
  error_page 505 502 503 504 /500.html;
  try_files  $uri/index.html $uri @unicorn;
  keepalive_timeout 5;

  location @unicorn {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://unicorn;
  }
}

Gemファイルにunicorn追加

gem 'unicorn'

3.各種設定(database.yml,unicorn.conf.rb)

config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: password

development:
  <<: *default
  host: db
  database: docker_development
config/unicorn.conf.rb
$worker  = 2
  $timeout = 30
  $app_dir = "/myproject" #自分のアプリケーションまでのpath
  $listen  = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir
  $pid     = File.expand_path 'tmp/pids/unicorn.pid', $app_dir
  $std_log = File.expand_path 'log/unicorn.log', $app_dir
  # set config
  worker_processes  $worker
  working_directory $app_dir
  stderr_path $std_log
  stdout_path $std_log
  timeout $timeout
  listen  $listen
  pid $pid
  # loading booster
  preload_app true
  # before starting processes
  before_fork do |server, worker|
    defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
    old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
      begin
        Process.kill "QUIT", File.read(old_pid).to_i
      rescue Errno::ENOENT, Errno::ESRCH
      end
    end
  end
  # after finishing processes
  after_fork do |server, worker|
    defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
  end

3.docker-compose.yml

docker-compose.yml
version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: dockerize -wait tcp://db:3306 -timeout 20s bundle exec unicorn -p 3000 -c /myproject/config/unicorn.conf.rb 
    tty: true #pry-byebugを使えるようにする
    stdin_open: true
    depends_on:
      - db 
    ports:
      - "3000:3000"
    volumes:
      - .:/myproject:cached 
      #ソケット通信用ファイルをnginxコンテナと共有
      - tmp-data:/myproject/tmp/sockets
      #画像データとかをnginxと共有
      - public-data:/myproject/public

  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
    ports: 
      - '3306:3306'
    environment:
      MYSQL_DATABASE: docker_development
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: root
      MYSQL_PASSWORD: password
    #dbのデータを永続化しておく
    volumes:
      - mysql-data:/var/lib/mysql

  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 80:80
    restart: always #明示的にstopさせるまでリスタートする。(失敗するたび遅延あり)
    volumes:
      - tmp-data:/myproject/tmp/sockets
      - public-data:/myproject/public
    depends_on:
      - web 

volumes:
  public-data:
  tmp-data:
  mysql-data:

名前つきvolumeを作成してdbのデータの永続化と、nginxとunicornのソケット通信用ファイルを共有化しています。
次のコマンドを実行してコンテナを立ち上げてください。以上で環境の構築は終わりです。

#imageの作成
docker-compose build 
#コンテナの立上げ
dokcer-compose up -d

docker-compose exec web bundle exec rails db:migrate

終わりに

今回の環境を作成する上で困った点はdbが立ち上がる前にunicornコマンドを実行しようとして、コネクションエラーがよく起きたことです。dockerizeを利用することによって解決することができました。
また、データ永続化にするときに/myproject/tmp/で最初に指定していたせいで、tmp/pidsも永続化されてコンテナのbuildやupを何度か繰り返しているとうまくサービスが終了しなかった時とかに、pidファイルが残ってエラーを起こしてました。volumeを削除して立ち上げなおすことが何度かありました。
以前書いたこちらの記事も組み合わせればRSpecのテスト用の環境も追加できます。
Docker+Rails+HeadlessChromeでRSpecのSystem Testしてみた
次回はRSpecテスト用に2個目のdocker-compose.ymlを用意して、CircleCIで自動テストをする環境構築について記事にしたいと思います。
ここまでお読みいただきありがとうございました。

36
40
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
36
40