[RUBY] Rails API server environment construction using docker-compose

About the author

I went through Progate's Ruby and Rails courses. After that, I made an environment with Docker and made an API server with Rails.

About this article

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.

Premise

Understand how to use Docker and docker-compose

environment

Ruby 2.7 MySQL 5.7 VSCode Mac 10.14.5 For viewing the Sequel Pro database

Until Rails new

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. スクリーンショット 2020-09-26 20.12.16.png

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.

スクリーンショット 2020-09-26 20.23.52.png

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

Write a configuration file to connect to mysql.

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.

reference

Docker Official Quick Start English Please note that the content is old in Japanese.

Rails Guide API-only application by Rails

Recommended Posts

Rails API server environment construction using docker-compose
Rails Docker environment construction
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Django development environment construction using Docker-compose (personal memorandum)
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
Stable development environment construction manual for "Rails6" with "Docker-compose"
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
[Docker] Rails 5.2 environment construction with docker
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
[Rails / MySQL] Mac environment construction
Rails + MySQL environment construction with Docker
[Rails 6] API development using GraphQL (Query)
Ruby on Rails 6.0 environment construction memo
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
Troublesome Rails environment construction flow [Windows 10]
[Environment construction] Uninstall rails from local
[Rails] AWS EC2 instance environment construction
Rails API
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
Visualize Rails server processing time using Server Timing
Rails environment construction with Docker (personal apocalypse)
Try using the Rails API (zip code)
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Rails & React & Webpacker & MySQL Environment Construction Manual
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
CentOS8.2 (x86_64) + ruby2.5 + Rails5.2 + MariaDB (10.3.17) environment construction
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Prepare Nuxt (web) + Laravel (API) development environment in the same repository using docker-compose
How to link Rails6 Vue (from environment construction)
Environment construction procedure for using PowerMock with JUnit
[Java] Environment construction
Kaggle environment construction using official Docker and vscode
[Ubuntu 18.04] Environment construction for using PyTorch with RTX3090
Java environment construction
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
[Personal memo] Ruby on Rails environment construction (Windows)
Try deploying Rails application to EC2-Part 2 (Server construction)-
[Note] Struts2 environment construction using Gradle in Eclipse
500 Internal Server Error occurs in Rails production environment
Ruby on Rails development environment construction on M1 Mac
NFS server construction
[Spring] Environment construction
Docker environment construction
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
[Rails] Nginx, Puma environment deployment & server study [AWS EC2]
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Windows10 + WSL2 + DockerDesktop + docker-compose + GPU (Nvidia) + Jupyterlab environment construction
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
[Environment construction Mac] Ruby on Rails (+ Webpacker handles errors)
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Using PAY.JP API with Rails ~ Card Registration ~ (payjp.js v2)
Construction of data analysis environment using Docker (personal memorandum)
Penronse environment construction [Windows]
[Environment construction] Eclipse installation
[Flutter] Ubuntu 20.04 environment construction
About rails application server
Server construction procedure Ubuntu18.04 server