Introduce Docker to the development environment and test environment of existing Rails and MySQL applications

Introduction

As the title suggests, Docker will be installed in the existing Rails and MySQL application development environment. It also allows you to run rspec tests within Docker. There are already a lot of similar articles, but I've encountered quite a few errors, so I'll summarize how I worked. I would appreciate it if you could let me know if there are any mistakes.

environment

If Rails version is 6 and MySQL version is 8, this method will not work.

procedure

  1. Create Dockerfile
  2. Create docker-compose.yml
  3. Modify the existing database.yml
  4. Start up

1. Create Dockerfile

Create a new Dockerfile in the existing project directory.

file organization


 test_app#Existing project
├── Abbreviation
    ├── Dockerfile #Create
    ├── docker-compose.yml #Create
    ├── Gemfile
    ├── Gemfile.lock
    ├── .env #use
    ├── config
         └──database.yml #Change

I will describe the contents. Please match test_app below to each app name.

Dockerfile


FROM ruby:2.6.5

RUN apt-get update \
    && apt-get install -y --no-install-recommends nodejs mariadb-client build-essential \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /test_app

COPY Gemfile /test_app/Gemfile
COPY Gemfile.lock /test_app/Gemfile.lock

RUN gem install bundler
RUN bundle install
COPY . /test_app

--First, get the image of ruby with FROM. The version suits your environment. --Next, you are installing the required packages. There is an article that mariadb-client is mysql-client, but note that it resulted in an error. mysql-client seems to be integrated with mariadb-client. --Next, specify the working directory. Even if there is no RUN mdir, it will be created with WORKDIR. --The following is copying gemfile etc. from local to the container. There is an article that COPY is ʻADD, but it seems that COPY` is recommended.

2. Create docker-compose.yml

docker-compose.yml


version: '3'

services:
  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
    ports: 
      - '3306:3306'
    environment:
      MYSQL_DATABASE: today_code_development
      MYSQL_ROOT_PASSWORD: ${DATABASE_DEV_PASSWORD}
      MYSQL_USER: ${DATABASE_DEV_USER}
      MYSQL_PASSWORD: ${DATABASE_DEV_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql

  test-db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
    environment:
      MYSQL_DATABASE: today_code_test
      MYSQL_ROOT_PASSWORD: ${DATABASE_DEV_PASSWORD}
      MYSQL_USER: ${DATABASE_DEV_USER}
      MYSQL_PASSWORD: ${DATABASE_DEV_PASSWORD}
    ports:
      - '3307:3306'

  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    tty: true 
    stdin_open: true
    depends_on:
      - db
      - test-db
    ports:
      - "3000:3000" 
    volumes:
      - .:/today_code
      - bundle:/usr/local/bundle

volumes:
  mysql-data:
  bundle:

Write only the points.

--The command line sets the character code of database to utf8. Without this, an error occurred at db: create. --In ʻenviroment, the user name and password are set. Of course, it works even if you write it directly, but here we are expanding the environment variables. The docker-compose file seems to read the .env file by default, so define it in the .env file as shown below. The defined environment variables can be expanded with $ {}`. Add the .env file to the .gitignore file so that it doesn't commit. --The data is persisted by creating named volumes with volumes. There seems to be a way to mount the directory, but the data volume seems to have more advantages.

.env


DATABASE_DEV_USER = 'hoge'
DATABASE_DEV_PASSWORD = 'password'

3. Modify database.yml

database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  charset: utf8
  pool: 5
  port: 3306

development:
  <<: *default
  database: test_app_development
  username: <%= ENV['DATABASE_DEV_USER'] %>
  password: <%= ENV['DATABASE_DEV_PASSWORD'] %>
  host : db

test:
  <<: *default
  database: test_app_test
  username: <%= ENV['DATABASE_DEV_USER'] %>
  password: <%= ENV['DATABASE_DEV_PASSWORD'] %>
  host : test-db

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

--username and password are the same as docker-compose. --It is set to connect to the db container created by host. ――Please adjust the production to each environment --The dotenv-rails gem is used to manage environment variables.

4. Start up

If you can start docker at the end, it is successful.

  1. Create an image with docker-compose build
  2. Launch the container with docker-compose up -d. -d is an option to start in the background.
  3. Create database with docker-compose run web rails db: create

If it is displayed at http: // localhost: 3000 /, it is successful. All you have to do is db: migrate or db: seed and you're done.

Reference article

I tried to build an existing Rails project development environment using Docker (Rails + Mysql) How to run an existing Rails application with a Docker container + DB container visualization with sequence pro Build a test DB in Rails x Docker environment Persist Docker data! Introduction to environment construction starting from understanding Data Volume

Recommended Posts

Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Docker the development environment of Ruby on Rails project
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
Migrate existing Rails 6 apps to Docker environment
[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]
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
Improve the performance of your Docker development environment
A reminder of Docker and development environment construction
Introduce dotenv to Docker + Rails to manage environment variables
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
Rails6 I tried to introduce Docker to an existing application
SSL in the local environment of Docker / Rails / puma
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
Easy environment construction of MySQL and Redis with Docker and Alfred
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to build Rails, Postgres, ElasticSearch development environment with Docker
I tried to build the environment of WSL2 + Docker + VSCode
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
[Rails] How to get the URL of the transition source and redirect
I tried to build the environment of PlantUML Server with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
[Rails] How to introduce kaminari with Slim and change the design
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] Read the RSS of the site and return the contents to the front
Install Rails in the development environment and create a new application
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Procedure for migrating Rails application development environment to Docker even if you are inexperienced (Rails5 + MySQL8.0 + docker-compose)
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Deploy to heroku with Docker (Rails 6, MySQL)
I tried to summarize the key points of gRPC design and development
How to execute with commands of normal development language in Docker development environment
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
[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)
Launching the production environment with docker + rails (ver5.2) and errors that occurred
How to solve the local environment construction of Ruby on Rails (MAC)!
[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 build a Ruby on Rails development environment with Docker (Rails 5.x)
The process of introducing Vuetify to Rails
[Rails] Articles for beginners to organize and understand the flow of form_with
I tried migrating the portfolio created on Vagrant to the Docker development environment
After all I wanted to preview the contents of mysql with Docker ...
How to build Rails 6 environment with Docker
Complete roadmap for building environment up to Docker + rails6 + MySQL + bootstrap, jquery
[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)
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
About the case where "Docker" freeter tried to put Docker in the existing Rails application
Build a development environment for Docker + Rails6 + Postgresql