The official Docker site has a Docker compose tutorial for Rails applications (https://docs.docker.com/compose/rails/), but it's a bit old and didn't work with Rails 6, so Rails 6 Here's how to get it working with. Since the basics follow the steps of the official tutorial, I've added supplements focusing on the changes made for Rails 6.
DB describes how to use MySQL instead of Postgres.
mkdir myrailsapp
cd myrailsapp
Dockerfile
FROM ruby:2.5
##nodejs and yarn are required when installing webpack
#Install yarn package management tool
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn
RUN apt-get update -qq && apt-get install -y nodejs yarn
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
RUN yarn install --check-files
RUN bundle exec rails webpacker:compile
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
Rails 6 requires yarn to run Webpacker, so add the procedure for installing yarn to the Dockerfile. If you normally install with apt-get yarn
with the image of ruby: 2.5
, a strange version of 0.32 + git
will be installed and an error will occur later, so I added the repository according to the instructions on the yarn official website. Later apt-get install yarn
.
Reference: https://qiita.com/shunichi_com/items/4dca141d8b9342c51a04
Also, in the Production environment, it is necessary to compile Webpacker in advance, so I added yarn package update and webpacker: compile
.
Excluded because postgres is not used.
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
Specify Rails 6 series in Gemfile. This Gemfile will be overwritten with the contents of the Rails project after the Rails project is created.
touch Gemfile.lock
entrypoint.sh
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
This area is still an official sample, so explanation is omitted.
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: root
ports:
- "3306:3306"
volumes:
- ./tmp/db:/var/lib/mysql
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Change the setting to use MySQL.
docker-compose run web rails new . --force --no-deps --database=mysql
Create a project with the DB setting set to MySQL with --database = mysql
.
docker-compose build
database.yml
development:
<<: *default
database: myapp_development
host: db
username: root
password: password
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: myapp_test
host: db
username: root
password: password
Described the setting so that development
and test
use the MySQL image db
started by docker-compose.
docker-compose run web rails db:create
docker-compose run web rails webpacker:install
This step is necessary because Rails 6 has replaced Sprockets with Webpacker.
docker-compose up
Scaffold
docker-compose run web rails g scaffold article title:string body:text published_at:timestamp
docker-compose run web rails db:migrate
When developing using docker-compose, execute generate commands including scaffold and migration with docker-compose run web
.
docker-compose up
Recommended Posts