I went through Progate's Ruby and Rails courses. After that, I made an environment with Docker and made an API server with Rails.
At Progate I learned how to write rails including views, but I wanted to use Rails only as an API server. Also, since I wanted to develop with docker, I created a Rails development environment by referring to the official Quick Start. Summarize the know-how gained in the process.
I will write about Gem used in API server development in another article.
Understand how to use Docker and docker-compose
Ruby 2.7 MySQL 5.7 VSCode Mac 10.14.5 For viewing the Sequel Pro database
First, create a project folder. Here, it is rails-sample. And create a Dockerfile directly under this
FROM ruby:2.7
RUN apt-get update -qq && apt-get install -y
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
# 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"]
Next, prepare a Gemfile to install rails
source 'https://rubygems.org'
gem 'rails', '~>5'
Create an empty Gemfile.lock
touch Gemfile.lock
Create ʻentrypoint.sh` described in Dockerfile
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 "$@"
See Docker Official Quick Start for why these files are created.
Create docker-compose.yml
docker-compose.yml
version: '3'
services:
server:
build: .
tty: true
volumes:
- .:/myapp
working_dir: /myapp
ports:
- "3000:3000"
depends_on:
- mysql
command: bash
# command: >
# bash -c "rm -f tmp/pids/server.pid &&
# bundle exec rails s -p 3000 -b '0.0.0.0'"
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: mysql
volumes:
- "./mysql/db-data/:/var/lib/mysql" #Data persistence
- "./mysql/my.cnf:/etc/mysql/conf.d/my.cnf" #Necessary to use Japanese as data
For MySQL data persistence and Japanese support
Create rails-sample> mysql> db-data folder.
Create my.cnf
under mysql folder
my.cnf
[mysqld]
character-set-server=utf8
Up to this point, this is the configuration.
If you can do that, do rails new
.
docker-compose run server rails new . -MC --force --api --database=mysql --skip-active-storage
Look up the options in the rails command and command help, and omit the ones you don't think you need. The important options are:
--force //Overwrite files on rails new
--api //Do not generate unnecessary views for api development
--database=myql //The default is sqlite, but specify mysql
You can make various things with this.
So, here, Gemfile
and Gemfile.lock
are overwritten and new.
In order to reflect this in the image
docker-compose build
There is a need to.
If you know the gem you need in advance, write it in the Gemfile here.
This completes rails new
config > database.yml
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: mysql
development:
<<: *default
host: mysql
database: myapp_development
...
test:
<<: *default
host: mysql
database: myapp_test
Since production is unnecessary, I commented it out completely
The host name is the service name of docker-compose.yml
password is also specified in docker-compose.yml
If you can, start the container with docker-compose up -d
and go inside with VS Code's Attach Shell.
rails db:create
Create a DB with
root@dc075120af8a:/myapp# rails db:create
...
Created database 'myapp_development'
Created database 'myapp_test'
Now you have a database. If you connect with Sequel Pro, you can confirm that the database is created
After that, as you learned in Progate, you can create a table by creating a model and migrating.
Docker Official Quick Start English Please note that the content is old in Japanese.
Rails Guide API-only application by Rails
Recommended Posts