Hello. This time, I summarized the procedure to build a Rails application development environment with Docker. I haven't studied enough yet, so I would appreciate it if you could point out any corrections or improvements.
Ruby: 2.5.8 Rails: 5.2.4.3 MySQL: 5.7.31 Docker: 19.03.8 Docker Compose: 1.25.4
terminal
$ mkdir test-app
First, create the root directory of your project.
terminal
$ cd test-app
$ touch Dockerfile docker-compose.yml Gemfile Gemfile.lock
Create four files, Dockerfile
, docker-compose.yml
, Gemfile
, and Gemfile.lock
, directly under the created root directory.
The contents of each file are as follows. (Leave Gemfile.lock empty.)
Dockerfile
FROM ruby:2.5
RUN apt-get update && apt-get install -y \
build-essential \
nodejs
WORKDIR /test-app
COPY Gemfile Gemfile.lock /test-app/
RUN bundle install
docker-compose.yml
version: '3'
volumes:
mysql-data:
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- '3000:3000'
volumes:
- '.:/test-app'
tty: true
stdin_open: true
depends_on:
- db
links:
- db
db:
image: mysql:5.7
volumes:
- 'mysql-data:/var/lib/mysql'
environment:
- 'MYSQL_ROOT_PASSWORD=test-app'
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5.2'
terminal
$ docker-compose run --rm web rails new . --force --database=mysql --skip-bundle --skip-test
Run rails new
inside a web container.
I was planning to use RSpec for testing this time, so I've also added --skip-test
.
Edit the config / database.yml
created by Rails setup as follows.
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: test-app #docker-compose.yml MYSQL_ROOT_Set the value of PASSWORD
host: db #docker-compose.Match with yml service name
development:
<<: *default
database: test-app_development
terminal
$ docker-compose up --build -d
$ docker-compose run --rm web rails db:create
Now, when you visit http: // localhost: 3000, you should see the Rails home screen.
-Too polite Docker-compose rails5 + MySQL on Docker environment construction (Docker for Mac) -[Rails] Procedure for creating Rails + MySQL development environment with Docker -Docker x Ruby on Rails x MySQL environment construction --Dockerfile Best Practices
Recommended Posts