Procedure for migrating Rails application development environment to Docker even if you are inexperienced (Rails5 + MySQL8.0 + docker-compose)

Introduction

The author, who has no practical experience with Docker at all, summarized the process of migrating the development environment of his own Rails application to Docker.

The period until the transition was about a week including basic learning (if there were no errors, it could have been done in about half ...)

I hope it will be helpful for those who want to touch Docker and are stuck with errors.

environment

**※Caution! ** ** MySQL has different default authentication formats for 5.0 series and 8.0 series. (As of August 18, 2020) As a result, the description of the file (docker-compose.yml) required to create Docker differs depending on the version **. ** ** Therefore, please make sure that the version of MySQL you are using matches the version of MySQL described in the literature.

Basic learning

Teaching materials: Introduction docker

Introduction Docker is a hands-on free site where you can easily learn terms and structures related to Docker.

If you don't know anything about Docker, even if you do all the content, it will take about half a day to a day, so study roughly at Introduction Docker. recommend.

Premise

Files required to use Docker

The following three files are added to the existing application and need to be modified.

The file structure is as follows.

sampleApp ---- Dockerfile #Files to add
           |-- docker-compose.yml  #Files to add
           |-- Gemfile
           |-- Gemfile.lock
           |-- README.md
           |-- Rakefile
           |-- app
           |-- bin
           |-- config ---- application.rb
           |           |-- boot.rb
           |           |-- cable.yml
           |           |-- credentials.yml.enc
           |           |-- database.yml     #File to modify
           |           |-- environment.rb
           |           |-- environments
           |           |-- initializers
           |           |-- locales
           |           |-- master.key
           |           |-- puma.rb
           |           |-- routes.rb
           |           |-- spring.rb
           |           |-- storage.yml
           |
           |-- config.ru
           |-- db
           |-- lib
           |-- log
           |-- package.json
           |-- public
           |-- storage
           |-- test
           |-- tmp
           |-- vendor

Dockerfile

Dockerfile



#Specify the Docker Image. Specify the version of the app you are using.
FROM ruby:2.5.1 

#Installation of required packages. node.Regarding js, an error occurred in the original description, so it was corrected.
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client --no-install-recommends && rm -rf /var/lib/apt/lists/*

#Create and set working directory
RUN mkdir /workdir
WORKDIR /workdir

#Created the host side (local) Gemfile above/Add to workdir
ADD Gemfile /workdir/Gemfile
ADD Gemfile.lock /workdir/Gemfile.lock

#Gemfile bundle install
#I got an error when I ran it without ENV. BUNDLER_Avoided by specifying VERSION.
ENV BUNDLER_VERSION 2.1.4
RUN gem install bundler
RUN bundle install

#All directories on the host side (local) of the Docker container/Added under workdir.
ADD . /workdir

docker-compose.yml

docker-compose.yml



# docker-Specifies the compose version. This time'3'use.
version: '3'

#Define the container to start. Db in this file, db-test ,It defines three of the web.
services:
  db:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password #The default authentication format is mysql_native_Change to password. MySQL5.Not required for 0 series.
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: appname_development
      MYSQL_USER: yuki #Any user
      MYSQL_PASSWORD: password
      TZ: Asia/Tokyo
    volumes:
      - ./mysql/mysql_data:/var/lib/mysql
      - ./logs:/var/log/mysql
      - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    ports:
      - "4306:3306"

  db-test:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: appname_test
      MYSQL_USER: yuki
      MYSQL_PASSWORD: password
      TZ: Asia/Tokyo
    volumes:
      - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    tmpfs:
      - /var/lib/mysql
      - /var/log/mysql
      
  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: /bin/sh -c "rm -f /workdir/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    tty: true
    stdin_open: true
    depends_on:
      - db
    ports:
      - "3000:3000"
    volumes:
      - .:/workdir

database.yml

database.yml



default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: appname_development
  username: yuki
  password: password
  host: db
  socket: /tmp/mysql.sock

test:
  <<: *default
  database: appname_test
  host: db-test
  username: yuki
  password: password
  socket: /tmp/mysql.sock

production:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOSTNAME'] %>

Start Docker / Create DB

python


$ docker-compose build #Build container
$ docker-compose up -d #Simultaneous start of container

$ docker-compose run web bundle exec rake db:create  #DB creation
$ docker-compose run web bundle exec rake db:migrate #migration

If you can do this, you should be able to access it at http: // localhost: 3000.

Recommended Posts

Procedure for migrating Rails application development environment to Docker even if you are inexperienced (Rails5 + MySQL8.0 + docker-compose)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
Complete roadmap for building environment up to Docker + rails6 + MySQL + bootstrap, jquery
Build a development environment for Docker + Rails6 + Postgresql
Introduce docker to the application you are creating
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Stable development environment construction manual for "Rails6" with "Docker-compose"
<Dot installation> What to do if you cannot proceed due to an error when building a development environment for Rails learning.
Web application development environment construction in Java (for inexperienced people)
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
Ruby on Rails --From environment construction to simple application development on WSL2
If you want to change the Java development environment from Eclipse
How to install Pry after building Rails development environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build a development environment for Django + MySQL + nginx with Docker Compose
Rails + MySQL environment construction with Docker
Environment construction for Servlet application development
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Creating a java web application development environment with docker for mac part1
I tried to build a Firebase application development environment with Docker in 2020
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Create a java web application development environment with docker for mac part2
I tried migrating the portfolio created on Vagrant to the Docker development environment
[For beginners] Laravel Docker AWS (EC2) How to easily deploy Web application (PHP) from 0 (free) ②-Docker development environment construction-
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)
How to build Rails 6 environment with Docker
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
[Rails5.2] Support for emoji of Mysql 5.7 in Docker (change character code to utf8mb4)