[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8

Introduction

I had a hard time building the environment with Rails6 and MySQL with Docker, so I will leave a memorandum in the article as well.

As a stumbling block Unlike Rails5, Rails6 has required the introduction of Webpacker, a gem package for using Webpack with Ruby on Rails, which strongly supports modern front-end development.

Environment / version

Procedure to complete environment construction

  1. Prepare a project folder
  2. Prepare 5 files
  3. Describe each file
  4. Start Rails project (rails new)
  5. Build Docker image
  6. Edit database.yml
  7. Create DB (db: create)
  8. Start the container

Actually build the environment

1. Prepare a project folder

Create a folder with any name (it doesn't have to be a command)

mkdir dockerSampleApp 

2. Prepare 5 files

Create a file in the folder created in 1.

3. Describe each file

Dockerfile

Regardless of the folder name of the project, the part of myapp can be left as myapp.

Dockerfile


FROM ruby:2.7

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 \
    && apt-get update -qq \
    && apt-get install -y nodejs yarn \
    && mkdir /myapp
    
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
volumes:
  mysql-data:
    driver: local

Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails', '~>6'

Gemfile.lock

Gemfile.lock


#Do not write anything in this file

entrypoint.sh

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 "$@"

4. Start Rails project (rails new)

Execute command

$ docker-compose run web rails new . --force --no-deps --database=mysql --skip-test --webpacker --api

About options used this time

-- --force Overwrite existing Gemfile -- --no-deps Do not start the linked service -- --database = mysql Specify MySQL for the database -- --skip-test Skip the installation of Minitest (because the test will introduce RSpec) --"--webpackerInstall webpacker (required package management tool for Rails 6) ―― --api`` Since I want to make only API, execute it in API mode. As a result, unnecessary View / UI related libraries will not be installed.

5. Build Docker image

Image build means installing various dependent libraries and middleware, and installing and setting your own applications.

Execute command

$ docker-compose build

6. Edit database.yml

Corrected the relevant part of database.yml. Corrected the relevant part according to the settings of services and MYSQL_ROOT_PASSWORD in docker-compose.yml.

config/database.yml


# ~abridgement~

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password  # ->Correct the blank to password
  host: db  # ->Fixed from localhost to db

# ~abridgement~

7. Create DB (db: create)

Execute the command to create a DB

$ docker-compose run web rails db:create

8. Start the container

Finally execute the command to start the container

$ docker-compose up

It may take some time, but it's OK if such a log flows in the terminal

web_1  | => Booting Puma
web_1  | => Rails 6.0.3.4 application starting in development 
web_1  | => Run `rails server --help` for more startup options
web_1  | Puma starting in single mode...
web_1  | * Version 4.3.6 (ruby 2.7.2-p137), codename: Mysterious Traveller
web_1  | * Min threads: 5, max threads: 5
web_1  | * Environment: development
web_1  | * Listening on tcp://0.0.0.0:3000
web_1  | Use Ctrl-C to stop

When I access localhost: 3000, the page is displayed safely. Should be! !!

Finally

I managed to build an environment! In the future, I would like to learn about infrastructure such as Docker and deepen my understanding!

Reference material

I was very helpful!

Thank you very much! !!

Recommended Posts

[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
[Docker] Rails 5.2 environment construction with docker
Ruby on Rails development environment construction on M1 Mac
[Rails / MySQL] Mac environment construction
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Docker the development environment of Ruby on Rails project
Ruby on Rails 6.0 environment construction memo
Rails on Docker environment construction procedure
Docker environment construction
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
Rails environment construction with Docker (personal apocalypse)
Laravel development environment construction with Docker (Mac)
Sapper × Go (echo) × Docker development environment construction
Create Rails 6 + MySQL environment with Docker compose
Spring Boot + Docker Java development environment construction
Laravel + MySQL + phpMyadmin environment construction with Docker
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
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
java development environment construction
[Rails] Development with MySQL
Build a development environment for Docker + Rails6 + Postgresql
[Personal memo] Ruby on Rails environment construction (Windows)
A reminder of Docker and development environment construction
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
WSL2 + VSCode + Docker development environment
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Build a Ruby on Rails development environment on AWS Cloud9
BEAR application Docker development environment construction example (docker-sync, Mutagen)
Stable development environment construction manual for "Rails6" with "Docker-compose"
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Redmine (Docker) environment construction memo
[Environment construction Mac] Ruby on Rails (+ Webpacker handles errors)
Offline environment construction Ruby edition
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
Docker × Spring Boot environment construction
[Super easy] Ruby environment construction
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Ruby on Rails] From MySQL construction to database change
[Docker] postgres, pgadmin4 environment construction