Docker ComposeでのRailsアプリのDocker化方法

みなさま、ごきげんよう!今回はDocker ComposeでのRailsアプリのDocker化方法についてごく簡単にまとめてみました。前提として、Docker、Docker ComposeやRuby on Railsの基礎的な知識をお持ちの方が対象です。

 条件

 

 Docker化の手順

 1.必要なファイル類の準備

  • Railsアプリのためアプリケーション用とdb用のコンテナを使用。複数コンテナを使うため、Dockerfileとdocker-compose.ymlを用意。
  • WebアプリのソースがGitHub等リモートのリポジトリで公開されている場合クローンしてくる。
  • 土台となるDockerfileとdocker-compose.ymlを作成。
  • railsのdatabase.yml等に記述が不足している場合は必要な内容を追記。          




    FROM ruby:3.3.0
    RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    postgresql-client \
    yarn
    WORKDIR /rails-docker
    COPY Gemfile Gemfile.lock /rails-docker/
    RUN bundle install
  Dockerfile Dockerfile


 
version: '3'

volumes:
db-data:

services:
db:
image: postgres:12
volumes:
- 'db-data:/var/lib/postgresql/data'
environment:
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'

web:
build: .
command: >
bash -c "rm -f tmp/pids/server.pid && rails db:create
&& rails db:migrate && rails s -b 0.0.0.0"
- '3000:3000'
volumes:
- '.:/rails-docker'
environment:
- 'DATABASE_PASSWORD=postgres'
tty: true
stdin_open: true
depends_on:
- db
links:
- db

 

git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.3.0"



gem 'concurrent-ruby', '1.1.9'




# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.6"
gem 'bundler', '~> 2.5', '>= 2.5.3'

gem 'rubygems-update', '~> 3.5', '>= 3.5.3'
# The original asset pipeline for Rails https://github.com/rails/sprockets-rails
gem "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"

gem "importmap-rails"

# Hotwire's SPA-like page accelerator https://turbo.hotwired.dev
gem "turbo-rails"

# Hotwire's modest JavaScript framework https://stimulus.hotwired.dev
gem "stimulus-rails"

gem "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
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Sass to process CSS
# gem "sassc-rails"

html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
# Use console on exceptions pages https://github.com/rails/web-console
gem "web-console"

# gem "rack-mini-profiler"

# Speed up commands on slow machines / big apps https://github.com/rails/spring
# gem "spring"
end

group :test do
gem "capybara"
gem "selenium-webdriver"
gem "webdrivers"
end

 

 

GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.8)
actionpack (= 7.0.8)
actionview (= 7.0.8)
activejob (= 7.0.8)
activesupport (= 7.0.8)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.8)
actionview (= 7.0.8)
activesupport (= 7.0.8)
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.8)
actionpack (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.8)
activesupport (= 7.0.8)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.8)
activesupport (= 7.0.8)
globalid (>= 0.3.6)
activemodel (7.0.8)
activesupport (= 7.0.8)
activerecord (7.0.8)
activemodel (= 7.0.8)
activesupport (= 7.0.8)
activestorage (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
activerecord (= 7.0.8)
activesupport (= 7.0.8)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.8)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
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
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
concurrent-ruby (1.2.3)
crass (1.0.6)
date (3.3.4)
debug (1.9.1)
irb (~> 1.10)
reline (>= 0.3.8)
erubi (1.12.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
io-console (0.7.1)
irb (1.11.1)
rdoc
reline (>= 0.4.2)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.5)
minitest (5.21.1)
msgpack (1.7.2)
net-imap (0.4.9.1)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-smtp (0.4.0.1)
net-protocol
nio4r (2.7.0)
nokogiri (1.16.0-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.0-arm-linux)
racc (~> 1.4)
nokogiri (1.16.0-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.0-x86-linux)
racc (~> 1.4)
nokogiri (1.16.0-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.0-x86_64-linux)
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)
rails (7.0.8)
actioncable (= 7.0.8)
actionmailbox (= 7.0.8)
actionmailer (= 7.0.8)
actionpack (= 7.0.8)
actiontext (= 7.0.8)
actionview (= 7.0.8)
activejob (= 7.0.8)
activemodel (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
bundler (>= 1.15.0)
railties (= 7.0.8)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.1.0)
rdoc (6.6.2)
psych (>= 4.0.0)
regexp_parser (2.9.0)
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
rubyzip (2.3.2)
selenium-webdriver (4.10.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
stimulus-rails (1.3.3)
railties (>= 6.0.0)
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
turbo-rails (1.5.0)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webdrivers (5.3.1)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0, < 4.11)
websocket (1.2.10)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.12)

PLATFORMS
aarch64-linux
arm-linux
arm64-darwin

DEPENDENCIES
bootsnap
capybara
concurrent-ruby (= 1.2.3)
debug
importmap-rails
pg (~> 1.5, >= 1.5.4)
puma (~> 5.0)
rails (~> 7.0.6)
selenium-webdriver
sprockets-rails
stimulus-rails
turbo-rails
tzinfo-data
web-console
webdrivers

RUBY VERSION
ruby 3.3.0

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
# On macOS with MacPorts:
# 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
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: postgres
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: rails-docker_development

test:
<<: *default
database: rails-docker_test

production:
<<: *default
database: rails-docker_production



 

 2.docker-compose upで動作確認

最後にdocker-compose upを入力してRailsプロセスとdb起動まで完了するか確認。エラーなくコンテナが起動し、http://localhost:3000 でWebアプリへアクセスできたらDocker化完了。



最後に

Rails bundlerは相互に複雑な依存関係にあるため各々のバージョンを揃えることに個人的には苦心しました。
Docker Composeのみならず、Ruby on Railsの知見も高まりますので良かったら皆様もお試しください!
ではまた👋



 参考