[Ruby] Procedure to introduce Docker into existing Rails application development environment [Rails, MySQL, Docker]

1 minute read

Hello. This time, I summarized the procedure to introduce Docker into the existing Rails application development environment. I am still short of studying, so I would appreciate 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 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 and docker-compose.yml to the root directory

add_file.png

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

Dockerfile

~~~: 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 `FROM ruby:2.5.3`, match the Ruby version of the application.
- Install the required packages with `RUN apt-get update && apt-get install -y ~`.
- Create a folder in the container with `WORKDIR /kakeibo`.
- After copying Gemfile and Gemfile.lock in the container with `COPY Gemfile Gemfile.lock /kakeibo/`, execute `bundle install`.

#### docker-compose.yml

~~~: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 was summarized in this article in an easy-to-understand manner.

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 with 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 access http://localhost:3000, the app should be displayed successfully.

reference