[RUBY] How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]

Introduction

This article is the procedure when I introduced Docker to the local environment of the existing portfolio app. Since the official document of Docker is the procedure explanation in PostgreSQL, there is no procedure explanation of the official document when it comes to MySQL. Also, the information on Rails6 is still less than that on Rails5, and as a result, the information on the combination of Rails6 + MySQL was very little. As a result, I got various errors and tried and errored myself, so I hope this article will help those who introduce Docker with the combination of Rails6 + MySQL. Since the author is a beginner who has no practical experience in job hunting, there may be some mistakes. In that case, I would appreciate it if you could let me know in the comments.

Prerequisites

version

Step 1 Create Dockerfile

Create a Dockerfile in the root directory of the existing application and write as follows. ** * This time, the directory name of the application is sample_app. Replace this with the directory name of your app. ** **

Dockerfile


FROM ruby:2.6.5
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
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs yarn
WORKDIR /sample_app
COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock
RUN gem install bundler
RUN bundle install
COPY . /sample_app
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"]

Step 2 Create docker-compose.yml

As in step 1, create docker-compose.yml in the root directory of the existing application and write it as follows.

docker-compose.yml


version: '3' #docker-compose version
services:
  db:
    image: mysql:8.0.21 #Combine with existing apps. To the terminal[$ mysql --version]Confirm with
    environment:
      MYSQL_ROOT_PASSWORD: vxgbizakqc #Your password
      MYSQL_DATABASE: root
    ports:
      - "4306:3306"
    volumes:
      - ./mysql-confd:/etc/mysql/conf.d
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/sample_app
    ports:
      - 3000:3000
    depends_on:
      - db
    tty: true
    stdin_open: true
volumes:
  mysql-data:

Step 3 Create entrypoint.sh

As with steps 1 and 2, create entrypoint.sh in the root directory of the existing application and write it as follows.

entrypoint.sh


#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /sample_app/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "[email protected]"

Step 4 Edit config / database.yml

Edit database.yml in the config directory as follows.

config/database.yml



default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: vxgbizakqc #Your password
  socket: /tmp/mysql.sock
  host: db

development:
  <<: *default
  database: sample_app_development

test:
  <<: *default
  database: sample_app_test

Step 5 Create a Docker image

Execute the following command in the terminal to create a Docker image.

Terminal


MacBook-Pro:sample_app$ docker-compose build

Step 6 Start the Docker container

Execute the following command in the terminal to start the Docker container.

Terminal


MacBook-Pro:sample_app$ docker-compose up -d

Step 7 Create a database in the container

Execute the following command in the terminal to create the database in the container.

Terminal


MacBook-Pro:sample_app$ docker-compose run web rails db:create

When you execute the above command, a database will be created based on the database.yml edited earlier. If the database is created successfully, the following will be displayed.

Terminal


reating sample_app_web_run ... done
Created database 'sample_app_development'
Created database 'sample_app_test'

Step 8 Execute migration of the created database

Execute the following command in the terminal to execute the database migration.

Terminal


MacBook-Pro:sample_app$ docker-compose run web rails db:migrate

If the database migration is successful, the following will be displayed.

Terminal


Creating sample_app_web_run ... done
== 20209165960112 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0253s

  #Omission

-- add_index(:notifications, :comment_id)
   -> 0.0251s
== 20209165960112 CreateNotifications: migrated (0.1280s) =====================

Step 9 Install Gem

Well, this is the last of the long introduction of Docker. Execute the following command in the terminal to install Gem.

Terminal


MacBook-Pro:sample_app$ docker-compose exec web bundle install

that's all! !! You can see the application by connecting to localhost: 3000.

References

This time, I was able to introduce Docker as a result of referring to the following articles and video teaching materials. Thank you to the people who wrote the article and Mr. Kojima, the creator of the video teaching materials.

Recommended Posts

How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
SSL in the local environment of Docker / Rails / puma
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to build an environment with Docker
How to solve the local environment construction of Ruby on Rails (MAC)!
How to migrate a web application created in a local docker environment to AWS
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
How to build an environment with Docker, which is the minimum required to start a Rails application
After learning Progate, I tried to make an SNS application using Rails in the local environment
Rails6 I tried to introduce Docker to an existing application
[Rails] How to display an image in the view
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to set environment variables in the properties file of Spring boot application
About the case where "Docker" freeter tried to put Docker in the existing Rails application
Install by specifying the version of Django in the Docker environment
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
[Rails] How to delete production environment MySQL data after putting it in the development environment
How to use git with the power of jgit in an environment without git commands
How to install Swiper in Rails
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
I tried to develop the cache function of Application Container Cloud Service in the local environment
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to install Pry after building Rails development environment with Docker
How to install the language used in Ubuntu and how to build the environment
Install Rails in the development environment and create a new application
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
[Rails] How to reset the database in production environment (Capistrano version)
How to get the length of an audio file in java
Migrate existing Rails 6 apps to Docker environment
How to use MySQL in Rails tutorial
How to build Rails 6 environment with Docker
[Programming beginner] What to do when rails s becomes an error in the local development environment
How to make an application with ruby on rails (assuming that the environment has been built)
How to execute with commands of normal development language in Docker development environment
Run Redmine in the local environment of Windows10 Pro-Use Docker Desktop for Windows
How to check the logs in the Docker container
How to install Titan2D (v4.2.0) in virtual environment
Rails6.0 ~ How to create an eco-friendly development environment
How to use JQuery in js.erb of Rails6
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
[Rails & Docker & MySQL environment construction] Could not find gem ‘mysql2 (> = 0.4.4, <0.6.0)’ in any of the gem sources listed in your Gemfile.
How to install Docker
[Rails5.2] Support for emoji of Mysql 5.7 in Docker (change character code to utf8mb4)
[Rails] How to operate the helper method used in the main application with Administrate
How to make a unique combination of data in the rails intermediate table
How to install MySQL
How to set the display time to Japan time in Rails
Docker the development environment of Ruby on Rails project
How to install the root certificate of Centos7 (Cybertrust)
[Rails] How to change the column name of the table
Command memo to install xeyes in ubuntu docker environment
[Rails] How to get the contents of strong parameters
Install MySQL 5.6 on CentOS6 [How to specify the version]
[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)
Install multiple submit buttons in Rails View to get the value of the pressed button
How to check the WEB application created in the PC development environment on your smartphone