I found a video of building a Rails environment using Docker Compose on the channel of Kiyoto Programming University, which is sent to beginners, and it was very easy to understand, so I would like to summarize it as a memorandum.
If you want to create a Rails environment with Docker, you can watch this video. https://youtu.be/ltDdZAJli8c
A tool that allows you to operate multiple applications at once. For example, when developing an application with Rails, a web server and a database server are required at a minimum, but usually operations such as building and connecting each server are required. With docker-compose, you can launch and connect them with a single command, so you can build a development environment very easily.
--Prepare Docker related files
① Dockerfile Here is the description to create an image of rails.
Dockerfile
#Specify base image
FROM ruby:2.7
#Install JavaScript related libraries to install nodejs and yarn
RUN 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 \
#Install nodejs and yarn
&& apt-get update -qq \
&& apt-get install -y nodejs yarn
#Specify working directory
WORKDIR /app
COPY ./src /app
#Ruby related libraries(gem)Installation of
RUN bundle config --local set path 'vendor/bundle' \
&& bundle install
② Gemfile Next, prepare a Gemfile.
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6.1.0'
As mentioned above, if you execute rails new
with the rails
library installed, you can create a template of rails files all at once. At that time, a new Gemfile will be created to replace it.
③ docker-compose.yml Finally, prepare docker-compose.yml.
docker-compose.yml
ersion: '3'
services:
db:
image: mysql:8.0
# mysql8.The authentication format has changed from 0, and it is set to return to the 5 system authentication format. Failure to do this will result in an error
command: --default-authentication-plugin=mysql_native_password
volumes:
- ./src/db/mysql-data:/var/lib/mysql
#Setting environment variables
#MySQL will give an error if you don't set a password
environment:
MYSQL_ROOT_PASSWORD: password
web:
build: .
#Settings to enable standard I / O
#If you don't do this, you can't debug
tty: true
stdin_open: true
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./src:/app
#Settings that do not need to be rebuilt when gem is inserted
- bundle:/usr/local/bundle
ports:
- "3000:3000"
#When connecting from rails to MySQL, it is necessary to specify the IP address of the db service as the connection information. Settings that allow the IP address of the connection destination to be connected with db
depends_on:
- db
volumes:
bundle:
driver: local
During development, I sometimes get addicted to the A server is already running. Check /app/tmp/pids/server.pid.
error. This happens when the presence or absence of server.pid determines whether the server is running, and for some reason the server is not running but the server.pid file remains. To prevent this, delete server.pid at startup.
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
Run the rails new command when the file is ready.
$ docker-compose run web rails new . --force --database=mysql
Image rebuild due to changes to Gemfile
$ docker-compose build
Refer to config/database.yml and modify the connection destination information to the database.
database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
# docker-compose.Enter the password set in yml
password: password
# docker-compose.yml depends_Enter the value set in on
host: db
#The following is omitted
$ docker-compose run --rm web rails db:create
$ docker-compose up -d
Access localhost: 3000 with a browser, and if the lower screen is displayed, it is successful!
--Build image
$ docker-compose build
--Creating and starting a container
$ docker-compose up -d
--Stop and delete the container
$ docker-compose down
--Display the list of containers
$ docker-compose ps
--View log
docker-compose logs
--Create a container and execute a command
$ docker-compose run <service> <command>
--Command execution on the running container
$ docker-compose exec <service> <command>
Recommended Posts