Remake https://docs.docker.com/compose/rails/ for MySQL.
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs build-essential libpq-dev
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# 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"]
Create a Gemfile in the project root
source 'https://rubygems.org'
gem 'rails', '~>5'
Create Gemfile.lock
touch Gemfile.lock
#!/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 "$@"
Create volume as db_data
to make DB data persistent
version: "3"
services:
db:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: admin
MYSQL_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
volumes:
db_data:
Create a Rail project.
Run rails new on the web to initialize rails. Gemfile is updated automatically.
docker-compose run --no-deps web rails new . --force --database=mysql
The Gemfile has been updated, so build the image again.
docker-compose build
Write DB settings to config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: root
host: db
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
username: app
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
docker-compose up -d
docker-compose run web rake db:create
Connect to http: // localhost: 3000 and check the operation.
Recommended Posts