[RUBY] [Environment construction with Docker] Rails 6 & MySQL 8

1.First of all

This is a memorandum article because I built the environment with the intention of creating a new application with Rails. I have less than a year of experience as an engineer and Rails, so please point out any mistakes or better writing.

Operating environment

2. Prepare the necessary files

First, create an arbitrary directory and create a file.

mkdir rails_app
cd rails_app

#Create multiple files at once
touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock,entrypoint.sh,.env}

Dockerfile

Dockerfile


FROM ruby:2.7.2

ENV LANG C.UTF-8
ENV APP_ROOT /app

# install required libraries
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 --no-install-recommends \
  build-essential \
  nodejs \
  yarn && \
  apt-get clean && \
  rm --recursive --force /var/lib/apt/lists/*

# create working directory
RUN mkdir $APP_ROOT
WORKDIR $APP_ROOT

# bundle install
COPY Gemfile $APP_ROOT/Gemfile
COPY Gemfile.lock $APP_ROOT/Gemfile.lock
RUN bundle install --jobs 4 --retry 3

# create app in container
COPY . $APP_ROOT

# script to be executed every time the container starts
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

docker-compose.yml


version: '3.7'
services:
  db:
    image: mysql:8.0.20
    volumes:
      - mysql:/var/lib/mysql:delegated
    ports:
      - '3307:3306'
    command: --default-authentication-plugin=mysql_native_password
    env_file: .env

  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    tty: true
    stdin_open: true
    env_file: .env
    depends_on:
      - db
    ports:
      - '3000:3000'
    volumes:
      - .:/app:cached
      - bundle:/usr/local/bundle:delegated
      - node_modules:/app/node_modules
      - tmp-data:/app/tmp/sockets

volumes:
  mysql:
  bundle:
  node_modules:
  tmp-data:

.env

.env


MYSQL_ROOT_PASSWORD=password
TZ=Japan

Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails', '6.0.3'

Gemfile.lock

Gemfile.lock


#Leave empty

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

3. Create a Rails project

Once you have created the files you need, run the rails new command.

docker-compose run --rm web rails new . --force --no-deps --database=mysql --skip-turbolinks --skip-test
docker-compose run --rm web bin/rails webpacker:install

I think you can freely add options, but it is easier to specify various options here.

4. Edit database.yml

database.yml


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch('MYSQL_USERNAME') { 'root' } %>
  password: <%= ENV.fetch('MYSQL_PASSWORD') { 'password' } %>
  host: <%= ENV.fetch('MYSQL_HOST') { 'db' } %>

development:
  <<: *default
  database: rails_app_dev

test:
  <<: *default
  database: rails_app_test

production:
  <<: *default
  database: rails_app_prd
  username: app
  password: hoge

Describe password etc. so that you can access with the information set in docker-compose.yml. host is a db container.

5. Build the Docker image and create a DB

docker-compose build
docker-compose run web bin/rails db:create
  1. Yay! You’re on Rails!
docker-compose up -d

Start the container, and when the initial Rails screen is displayed, you're done. rails_new.png

reference

-[Rails] Rails 6.0 x Docker x MySQL environment construction --Participating projects, etc.

Recommended Posts

Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails Docker environment construction
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Laravel + MySQL + phpMyadmin environment construction with Docker
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
MySQL 5.7 (Docker) environment construction memo
[Rails / MySQL] Mac environment construction
React environment construction with Docker
React + Django + Nginx + MySQL environment construction with Docker
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Build environment with vue.js + rails + docker
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Build Rails environment with Docker Compose
Rails on Docker environment construction procedure
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Docker environment construction
Easy environment construction of MySQL and Redis with Docker and Alfred
SQL statement learning ~ Environment construction ~ Docker + MySQL
GPU environment construction with Docker [October 2020 version]
Laravel development environment construction with Docker (Mac)
Deploy to heroku with Docker (Rails 6, MySQL)
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
Rails & React & Webpacker & MySQL Environment Construction Manual
How to build Rails 6 environment with Docker
Rails deploy with Docker
[Docker] Connection with MySQL
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Spring Boot environment construction with Docker (January 2021 version)
Environment construction command memo with Docker on AWS
[Rails] How to build an environment with Docker
Wordpress local environment construction & development procedure with Docker
I built a rails environment with docker and mysql, but I got stuck
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Pytorch execution environment with Docker
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails6 (MySQL, Ubuntu environment, Cloud9)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
Run Rails whenever with docker
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
[Docker] Use whenever with Docker + Rails
Stable development environment construction manual for "Rails6" with "Docker-compose"
Database environment construction with Docker in Spring boot (IntellJ)
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
I made a development environment with rails6 + docker + postgreSQL + Materialize.