Skip to content

Instantly share code, notes, and snippets.

@pocari
Last active January 8, 2018 23:38
Show Gist options
  • Save pocari/26a7058b0eb89166977afd743c24d092 to your computer and use it in GitHub Desktop.
Save pocari/26a7058b0eb89166977afd743c24d092 to your computer and use it in GitHub Desktop.
dockerでrails5環境構築 ref: https://qiita.com/pocari/items/456052a291381895f8b3
<%
database_host = ENV['DATABASE_HOST'] || 'localhost'
database_port= ENV['DATABASE_PORT'] || 3306
%>
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASS'] %>
port: <%= database_port %>
host: <%= database_host %>
development:
<<: *default
database: <%= ENV['DATABASE_NAME'] %>
# testとproductionは省略
db:
container_name: db
build: ${DB_DOCKERFILE_DIR}
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
web:
container_name: web
build: ${WEB_DOCKERFILE_DIR}
ports:
- "3000:3000"
links:
- db:db
volumes:
- ${WEB_APP_ROOT_DIR}:/var/myapp
environment:
DATABASE_NAME: ${MYSQL_DATABASE}
DATABASE_USER: ${MYSQL_USER}
DATABASE_PASS: ${MYSQL_PASSWORD}
DATABASE_HOST: db
command: sh -c "bundle install --path=vendor/bundle && rm tmp/pids/server.pid; bin/rails s -b 0.0.0.0"
FROM ruby:2.3
############################################################
# ruby setting bundlerだけはglobalに入れておく
RUN gem install bundler
#####################################################
# misc settings
RUN usermod -u 1000 www-data
RUN \cp -fp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
VOLUME /var/myapp
WORKDIR /var/myapp
EXPOSE 3000
CMD ["bash"]
% tree
.
├── app # railsアプリケーションディレクトリ
└── docker # dockerの関連ディレクトリ
├── bin # docker-compose用のディレクトリ
├── db # mysql用のdockerディレクトリ
└── web # rails用のdockerディレクトリ
docker/bin/run-docker-compose.sh up
app/tmp/*
app/vendor/*
git init
git add -A
git commit -m "initial commit"
# hello コントローラ作成
bundle exec bin/rails g controller hello
class HelloController < ApplicationController
def hello
@msg = "world"
end
end
Hello, <%= @msg %>
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
# Serve websocket cable requests in-process
# mount ActionCable.server => '/cable'
get 'hello' => 'hello#hello'
end
class HelloController < ApplicationController
def hello
@msg = "docker"
end
end
config.file_watcher = ActiveSupport::FileUpdateChecker
class HelloController < ApplicationController
def hello
@msg = "docker docker docker"
end
end
sudo VBoxControl guestproperty set "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 5000
sudo VBoxControl guestproperty set "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 5000
config.web_console.whitelisted_ips = %w( 0.0.0.0/0 ::/0 )
docker/bin/run-docker-compose.sh build
docker run --rm -it -v $(pwd)/app:/var/myapp bin_web bash #docker-composeでイメージを作成するとbin_${yamlの定義名}になります。
# 以降このコードブロックの終わりまでコンテナ内での作業
# Gemfile初期化
bundle init
# この記事を作成中の最新の5.0.0.beta3を指定します。
echo "gem 'rails', '5.0.0.beta3'" >> Gemfile
# プロジェクトローカルにrails5をインストール
bundle install --path=vendor/bundle
# railsのアプリケーションを作成します。
# とりあえず雛形のGemfileを作りたいだけなのであとでこのアプリケーションは全部削除します。
bundle exec rails new sample_app --skip-bundle
#できたGemfileを持ってきます。
mv sample_app/Gemfile .
#sample_appはもういらないので削除します。
rm -rf sample_app
# 次にrailsのアプリケーションを作っていきますがファイルの編集はホストの方がやりやすいので一旦コンテナから出ます。
exit
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
# Use sqlite3 as the database for Active Record
gem 'mysql2'
# Use Puma as the app server
gem 'puma'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5.x'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
docker run --rm -it -v $(pwd)/app:/var/myapp bin_web bash
# 以降このコードブロックの終わりまでコンテナ内での作業
# dbmsはmysqlにする。
bundle install && bundle exec rails new . -d mysql
# This method returns the maximum mtime of the files in +paths+, or +nil+
# if the array is empty.
#
# Files with a mtime in the future are ignored. Such abnormal situation
# can happen for example if the user changes the clock by hand. It is
# healthy to consider this edge case because with mtimes in the future
# reloading is not triggered.
def max_mtime(paths)
time_now = Time.now
paths.map {|path| File.mtime(path)}.reject {|mtime| time_now < mtime}.max
end
#!/bin/sh
SCRIPT_DIR=$(cd $(dirname $0) && pwd)
export DB_DOCKERFILE_DIR=${SCRIPT_DIR}/../db
export WEB_DOCKERFILE_DIR=${SCRIPT_DIR}/../web
export WEB_APP_ROOT_DIR=${SCRIPT_DIR}/../../app
export MYSQL_DATABASE=mydb
export MYSQL_USER=dbuser
export MYSQL_PASSWORD=dbuser_pass
export MYSQL_ROOT_PASSWORD=root
YML_FILE=$SCRIPT_DIR/docker-compose.yml
docker-compose -f $YML_FILE $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment