みなさま、ごきげんよう!今回はDocker ComposeでのRailsアプリのDocker化方法についてごく簡単にまとめてみました。前提として、Docker、Docker ComposeやRuby on Railsの基礎的な知識をお持ちの方が対象です。
条件
- Docker未使用の環境で動作するRailsのwebアプリを用意。
- RailsアプリはRubyのバージョン3.3.0、dbとしてpostgresのバージョン12を使用。
docker-compose up
でRailsプロセスとdbが起動し、http://localhost:3000 でこのWebアプリでアクセスできるようにする。- ホストのファイルシステムとコンテナのファイルシステムを同期。
Docker化の手順
1.必要なファイル類の準備
- Railsアプリのためアプリケーション用とdb用のコンテナを使用。複数コンテナを使うため、Dockerfileとdocker-compose.ymlを用意。
- WebアプリのソースがGitHub等リモートのリポジトリで公開されている場合クローンしてくる。
- 土台となるDockerfileとdocker-compose.ymlを作成。
- railsのdatabase.yml等に記述が不足している場合は必要な内容を追記。
FROM ruby:3.3.0RUN apt-get update && apt-get install -y \build-essential \libpq-dev \nodejs \postgresql-client \yarnWORKDIR /rails-dockerCOPY Gemfile Gemfile.lock /rails-docker/RUN bundle install
Dockerfile Dockerfile
version: '3'
volumes:
db-data:
services:
db:
image: postgres:12
volumes:
environment:
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
web:
build: .
command: >
- '3000:3000'
volumes:
environment:
- 'DATABASE_PASSWORD=postgres'
tty: true
stdin_open: true
depends_on:
- db
links:
- db
gem 'bundler', '~> 2.5', '>= 2.5.3'
# The original asset pipeline for Rails https://github.com/rails/sprockets-rails
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use the Puma web server https://github.com/puma/puma
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps https://github.com/rails/importmap-rails
# Hotwire's SPA-like page accelerator https://turbo.hotwired.dev
# Hotwire's modest JavaScript framework https://stimulus.hotwired.dev
# Build JSON APIs with ease https://github.com/rails/jbuilder
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"
# Use Kredis to get higher-level data types in Redis https://github.com/rails/kredis
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org
/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use Sass to process CSS
# gem "sassc-rails"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.
html#transforming-images]
# gem "image_processing", "~> 1.2"
group :development, :test do
html#debugging-with-the-debug-gem
end
group :development do
# Use console on exceptions pages https://github.com/rails/web-console
gem "web-console"
# Add speed badges https://github.com/MiniProfiler/rack-mini-profiler
# gem "rack-mini-profiler"
# Speed up commands on slow machines / big apps https://github.com/rails/spring
# gem "spring"
end
group :test do
# Use system testing https://guides.rubyonrails.org/testing.html#system-testing
gem "capybara"
gem "webdrivers"
end
GEM
specs:
actioncable (7.0.8)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.8)
activejob (= 7.0.8)
activestorage (= 7.0.8)
mail (>= 2.7.1)
net-pop
actionmailer (7.0.8)
activejob (= 7.0.8)
mail (~> 2.5, >= 2.5.4)
net-pop
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
actiontext (7.0.8)
activestorage (= 7.0.8)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
builder (~> 3.1)
erubi (~> 1.4)
activejob (7.0.8)
globalid (>= 0.3.6)
activemodel (7.0.8)
activemodel (= 7.0.8)
activestorage (7.0.8)
activejob (= 7.0.8)
marcel (~> 1.0)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
bindex (0.8.1)
bootsnap (1.17.1)
msgpack (~> 1.2)
builder (3.2.4)
capybara (3.39.2)
addressable
matrix
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
crass (1.0.6)
date (3.3.4)
debug (1.9.1)
reline (>= 0.3.8)
erubi (1.12.0)
globalid (1.2.1)
io-console (0.7.1)
rdoc
reline (>= 0.4.2)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
net-pop
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
minitest (5.21.1)
msgpack (1.7.2)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-protocol
nio4r (2.7.0)
racc (~> 1.4)
racc (~> 1.4)
racc (~> 1.4)
racc (~> 1.4)
racc (~> 1.4)
racc (~> 1.4)
pg (1.5.4)
psych (5.1.2)
stringio
public_suffix (5.0.4)
puma (5.6.8)
nio4r (~> 2.0)
racc (1.7.3)
rack (2.2.8)
rack-test (2.1.0)
rack (>= 1.3)
actioncable (= 7.0.8)
actionmailbox (= 7.0.8)
actionmailer (= 7.0.8)
actiontext (= 7.0.8)
activejob (= 7.0.8)
activemodel (= 7.0.8)
activestorage (= 7.0.8)
bundler (>= 1.15.0)
minitest
nokogiri (>= 1.6)
loofah (~> 2.21)
nokogiri (~> 1.14)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.1.0)
rdoc (6.6.2)
psych (>= 4.0.0)
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
rubyzip (2.3.2)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sprockets (4.2.1)
rack (>= 2.2.4, < 4)
sprockets (>= 3.0.0)
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
activejob (>= 6.0.0)
tzinfo (2.0.6)
web-console (4.2.1)
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
webdrivers (5.3.1)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
websocket (1.2.10)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
nokogiri (~> 1.8)
zeitwerk (2.6.12)
PLATFORMS
DEPENDENCIES
bootsnap
capybara
debug
pg (~> 1.5, >= 1.5.4)
puma (~> 5.0)
tzinfo-data
web-console
webdrivers
BUNDLED WITH
2.5.3
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
host: db
username: postgres
password: postgres
timeout: 5000
development:
<<: *default
test:
<<: *default
production:
<<: *default
2.docker-compose upで動作確認
最後にdocker-compose up
を入力してRailsプロセスとdb起動まで完了するか確認。エラーなくコンテナが起動し、http://localhost:3000 でWebアプリへアクセスできたらDocker化完了。
最後に
Rails bundlerは相互に複雑な依存関係にあるため各々のバージョンを揃えることに個人的には苦心しました。
Docker Composeのみならず、Ruby on Railsの知見も高まりますので良かったら皆様もお試しください!
ではまた👋