[RUBY] Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]

Hello. This time, I summarized the procedure to introduce Docker to the development environment of the existing Rails application. I haven't studied enough yet, so I would appreciate it if you could point out any corrections or improvements.

Advance preparation

-Install Docker-for-mac --Existing Rails app (This time I will use the simple household account book app I created earlier.)

environment

Ruby:2.5.3 Rails:5.2.4.3 MySQL:5.6 Docker:19.03.8 docker-compose:1.25.4

procedure

1. Add Dockerfile, docker-compose.yml to the root directory

add_file.png

Create Dockerfile and docker-compose.yml directly under the root directory of your existing Rails app. Below are the contents of each file.

Dockerfile


FROM ruby:2.5.3
RUN apt-get update && apt-get install -y \
    build-essential \
    nodejs
WORKDIR /kakeibo
COPY Gemfile Gemfile.lock /kakeibo/
RUN bundle install

--For the FROM ruby: 2.5.3 part, match it with the Ruby version of the app. --RUN apt-get update && apt-get install -y ~ to install the required packages. --Create a folder in the container with WORKDIR / kakeibo. --Copy Gemfile and Gemfile.lock into the container with COPY Gemfile Gemfile.lock / kakeibo /, then run bundle install.

docker-compose.yml

version: '3'

volumes:
  mysql-data:

services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    ports:
      - '3000:3000'
    volumes:
      - '.:/kakeibo'
    tty: true
    stdin_open: true
    depends_on:
      - db
    links:
      - db

  db:
    image: mysql:5.6
    volumes:
      - 'mysql-data:/var/lib/mysql'
    environment:
      - 'MYSQL_ROOT_PASSWORD=password'

A detailed explanation of the contents of Dockerfile and docker-compose.yml is summarized in this article.

2. Edit config / database.yml

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

development:
  <<: *default
  database: kakeibo_development

Match the password and host in config / database.yml to the values set in docker-compose.yml.

3. Start container

$ docker-compose build
$ docker-compose up -d
$ docker-compose exec web rails db:create
$ docker-compose exec web rails db:migrate

Now when you visit http: // localhost: 3000, you should be able to see the app successfully.

reference

-Procedure for developing an existing rails project with Docker -Procedure to install Docker in existing Rails app -Create an existing application development environment with Docker [Ruby2.6 + Rails5.2 + Mysql5.7] -[Rails] Procedure for creating Rails + MySQL development environment with Docker -Docker beginners carefully summarize the procedure for building a virtual environment with Rails + PostgreSQL or MySQL -#Linux #Ubuntu #docker #Dockerfile What is this? apt-get install --no-install-recommends -"Dockerfile Best Practices"

Recommended Posts

Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Docker the development environment of Ruby on Rails project
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Procedure for migrating Rails application development environment to Docker even if you are inexperienced (Rails5 + MySQL8.0 + docker-compose)
Build a development environment for Docker + Rails6 + Postgresql
SSL in the local environment of Docker / Rails / puma
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Rails + MySQL environment construction with Docker
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
Use MailHog for checking emails in the development environment (using Docker)
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
Build a development environment for Django + MySQL + nginx with Docker Compose
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
Prepare the security check environment for Rails 6
Run Redmine in the local environment of Windows10 Pro-Use Docker Desktop for Windows
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[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.
The process of introducing Vuetify to Rails
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
[Rails5.2] Support for emoji of Mysql 5.7 in Docker (change character code to utf8mb4)
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
Procedure for loading JDBC of MySQL using JAVA-Eclipse
Rails6 [API mode] + MySQL5.7 environment construction with Docker
A reminder of Docker and development environment construction
[Rails] Building an environment for developing web applications
Build a development environment for Docker, java, vscode
Wordpress local environment construction & development procedure with Docker
WebSphere Liberty for Windows development environment maintenance procedure
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
[Rails] How to delete production environment MySQL data after putting it in the development environment
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Stable development environment construction manual for "Rails6" with "Docker-compose"
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
[First team development ③] Share the development environment created with Docker
Roughly the flow of web application development with Rails.
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Make the strongest Laravel development environment (Docker) Japan time
Debug the VSCode + Docker + PHP development environment with XDebug.
The story of introducing a very Rails-like serverless framework "Ruby on Jets" into the production environment
Rails Docker environment construction
[Rails] Development with MySQL
A memorandum of the environment variable "JAVA_HOME" path setting procedure
Building an environment for copying the book "Test Driven Development"
Install by specifying the version of Django in the Docker environment
[Docker] List of errors that occurred when building the environment
Setting the baseURL in the axios module of Docker environment Nuxt
Easy environment construction of MySQL and Redis with Docker and Alfred
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour