Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development

Introduction

When it comes to Docker for the first time, I often don't know what to start with, but that was exactly what I was doing ...

In this article, I will explain the method and flow of introducing Docker into a Rails application (DB: MySQL) that is under development and completed. For reference, I was able to introduce it locally in about 6 hours. I will output it firmly so as not to forget the hardship (laugh) I would appreciate it if you could point out any mistakes!

Introduction of Docker

1) Install Docker

First install the Docker app on your Mac The following article is easy to understand about that! Install Docker on Mac (https://qiita.com/kurkuru/items/127fa99ef5b2f0288b81)

After installation, execute the following command in ** Terminal **!

Terminal


~ % docker run -d -p 80:80 docker/getting-started

2) Confirmation of installation

Terminal


~ % docker -v

If the output is as follows, the installation is successful. Docker version 20.10.0, build 7287ab3

Terminal


~ % docker-compose -v

This is also successful if docker-compose version 1.27.4, build 40524192 is output in the same way.

3) Create a Docker file with the app under development / completed

Next, create a Docker file to install Docker and describe the settings. It is possible with a text editor, but in my case it was smoother to input from the terminal without any errors, so I will describe that method.

① Create Dockerfile

First, create a Dockerfile in the ** root directory ** of the application and describe it. The root directory represents the directory directly under the application as shown in the figure below.

dock_app ----|-- app
            |-- bin
            |-- config
            |-- db
・ ・ ・ ・ ・ ・
・ ・ ・ ・ ・ ・
            |-- Gemfile
            |-- Gemfile.lock
            |-- package.json
            |-- Rakefile
            |-- README.md
            |-- Dockerfile  ←「Dockerfile]
            |-- docker-compose.yml  ←[docker-compose.yml]

The procedure is as follows.

Terminal


① ~ %cd The path of the app where you want to install Docker

② App name% vi Dockerfile
 #After opening the file, press "i" to enter insert mode and write below
FROM ruby:2.6.5 #Ruby version of the app
  
RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       libpq-dev \
                       nodejs

RUN mkdir /app name
WORKDIR /app name

ADD ./Gemfile /app name/Gemfile
ADD ./Gemfile.lock /app name/Gemfile.lock

RUN gem install bundler #I get an error if I don't install bundler
RUN bundle install
ADD . /app name

 #When you have completed the description, press the "esc key" to ":Save as "wq"

② Create docker-compose.yml file

Terminal


① App name% vi docker-compose.yml

② docker-compose.Described below in yml

 #Enter "i" and write in insert mode

version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 'password' #It works fine as a password as it is
    ports:
      - "4306:3306" #Settings required for connecting to Docker container and Sequelpro

  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app name
    ports:
      - "3000:3000"
    depends_on:
      - db
 #When you have finished writing, press the "esc key" and click ":Save as "wq"

4) Edit the config/database.yml file

(Text editor)



default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password #If there is no password setting, you can leave it as it is
  socket: /tmp/mysql.sock
  host: db

development:
  <<: *default
  database:app name_development

Only two have been added: password: password and host: db.

5) Create a container with docker-compose build

Terminal


app name~ % docker-compose build

Let's execute the above command to create a container. * It may take some time

If the output is as follows, it is successful.

Terminal


Removing intermediate container dac250609513
 ---> f0ba8d685e44
Step 9/9 : ADD . /app name
 ---> f50cb7681119

Successfully built f50cb7681119
Successfully tagged app name_web:latest

By the way, if you are a beginner, you will see debconf: delaying package configuration, since apt-utils is not installed in red while executing the command, but as far as I can see, is there anything you care about? It looks like output.

6) Create DB and execute migration on the container

After creating the container, create a DB on the container.

Terminal


app name% docker-compose run web bundle exec rake db:create

With ** Rails 6 **, when you run this command

========================================
  Your Yarn packages are out of date!
  Please run `yarn install --check-files` to update.
========================================


To disable this check, please change `check_yarn_integrity`
to `false` in your webpacker config file (config/webpacker.yml).

It may be output like this. If it is output like this, edit the description of the config/webpacker.yml file according to the error as follows.

webpacker.yml



# Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: true

 # check_yarn_integrity:Let's change true to false → check_yarn_integrity: false

I think this will solve the error, so after changing the description, let's execute the ** DB creation command ** again.

Terminal


Created database 'app name_development'
Created database 'app name_test'

#As mentioned above, creating is created...Success when done

Next, let's execute migration.

Terminal


app name% docker-compose run web bundle exec rake db:migrate


==20201222010929 Create table name: migrating ====================================
-- create_table(:table name)
   -> 0.0095s
==20201222010929 Create table name: migrated (0.0096s) ===========================

#Rails db as above:If the migration is executed like when you executed migrate, it is successful.

7) Start the container

Finally, start the container and check if the app is displayed in the browser.

Let's execute the container start command.

Terminal


app name% docker-compose up 

#If it is running, the following output will be output

db_1   | 2020-12-27T06:45:52.494912Z 0 [Note] Event Scheduler: Loaded 0 events
db_1   | 2020-12-27T06:45:52.497330Z 0 [Note] InnoDB: Buffer pool(s) load completed at 201227  6:45:52
db_1   | 2020-12-27T06:45:52.497669Z 0 [Note] mysqld: ready for connections.
db_1   | Version: '5.7.32'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
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 3.12.6 (ruby 2.6.5-p114), codename: Llamas in Pajamas
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

Execute the command, let's access the local environment when the server starts http://localhost:3000/ If you can confirm the display and behavior safely, it means that you have installed Docker in the development environment.

It took me about 6 hours to do this alone, so please take advantage of this article and proceed quickly! !!

8) Connect Rails application in Docker environment (development environment) and Sequel Pro

Finally, let's connect the DB!

Open the settings page below and select ** Standard **.

se.png

Describe the following on the setting page

eee.png

"Name" → Set any one "Host" → "127.0.0.1" "User name / password" → Match the one described in the yml file "Port" → "4306"

After setting the above and "connecting", the DB of the corresponding application should be created in Sequel Pro.

Also, the boat number matches the description of ports: in docker-compose.yml.

This completes the introduction of Docker and docker-compose into the development environment. By the way, it took me about 6 hours so far, so please refer to it and proceed quickly.

This document

"Cloud infrastructure to learn by touching container construction from docker basics"Introduce Docker with MySQL to the existing rails6 app. Ruby on Rails "I want to introduce Docker to an app I made halfway" (MySQL / Sequel Pro)

Recommended Posts

Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
Introducing Rspec with Ruby on Rails x Docker
Introducing New Relic to Rails apps on Heroku
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Docker the development environment of Ruby on Rails project
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) How to create models and tables
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
How to use Ruby on Rails
Deploy Rails on Docker to heroku
Ruby on Rails --From environment construction to simple application development on WSL2
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Things to remember and concepts in the Ruby on Rails tutorial
Steps to build a Ruby on Rails development environment with Vagrant
Procedure for migrating Rails application development environment to Docker even if you are inexperienced (Rails5 + MySQL8.0 + docker-compose)
Install docker and docker-compose on Alpine Linux
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] Change the update date and creation date to your favorite notation
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Introducing Rspec, a Ruby on Rails test framework
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
[Rails MySQL] How to reset DB on heroku
Ruby on Rails development environment construction on M1 Mac
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Introduction] Try to create a Ruby on Rails application
[Ruby on Rails] How to write enum in Japanese
Build a Ruby on Rails development environment on AWS Cloud9
[Ruby on Rails] How to change the column name
I started MySQL 5.7 with docker-compose and tried to connect
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby On Rails] How to search and save the data of the parent table from the child table
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)