[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.

Introduction

I tried to build Rails6 and Docker environment, so I will leave it here as a memorandum. Qiita This is my first post, and since I am a beginner, I think there are various points that cannot be reached, so I would appreciate it if you could point out any mistakes.

environment

reference Build Rails6 + MySQL environment with docker-compose [Rails environment construction] Environment construction with docker + rails + mysql (even beginners can complete in 30 minutes!) [ENTRYPOINT is "must execute", CMD is "(default) argument"](https://pocketstudio.net/2020/01 / 31 / cmd-and-entrypoint /)

It was carried out with reference to this.

goal

Build the environment and display the Rails home screen.

1. Install Docker for Windows

https://hub.docker.com/ Go to and install.

2. Write a Dockerfile

$ mkdir ~/Desktop/docker-tutrial/Rails_app
$ cd  ~/Desktop/docker-tutrial/Rails_app
$ code Dockerfile

Open a terminal, create a directory in any location, and write `` `Dockerfile``` as follows. (I use vsCode.)

$code file name

Then vsCode will start and you can write a new file. (VsCode must be installed on your PC in advance.)

FROM ruby:2.7
RUN apt-get update -qq && apt-get install -y nodejs yarnpkg
RUN ln -s /usr/bin/yarnpkg /usr/bin/yarn
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

3. Description of Gemfile

source 'https://rubygems.org'
gem 'rails', '~>6'
$ touch Gemfile.lock

Write the initial `Gemfile` and the empty `` `Gemfile.lock```.

Four. Description of entrypoint.sh

Entrypoint defined as ENTRYPOINT in Dockerfile.Describe sh.



```shell

#!/bin/bash
set -e

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

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

Five. Description of docker-compose.yml

version: '3'
services:
  db:
    image: mysql:8.0
    volumes:
      - ./tmp/db:/var/lib/mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=1
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

MySQL uses version 8.0

By setting ** MYSQL_ALLOW_EMPTY_PASSWORD **, you can connect as root even if the password is empty.

6. Rails new and doocker-compose build.

$ docker-compose run web bundle exec rails new . --force --database=mysql

Overwrite the existing file with ** --force ** and specify MySQL with ** --database **. Now that you have the usual Rails fileset, build it.

$ docker-compose build

7. Change the DB host name and do docker-compose up.

Since the database connection cannot be made as it is, Replace the ** host ** part of ** config / database.yml ** with ** db **.


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: db
development:
  <<: *default
  database: app_development
test:
  <<: *default
  database: app_test

** db ** is the container name.

Once this is done, build again and do ** docker-compose up **. If you access Rails with ** localhost: 3000 **, you will get ** ActiveRecord :: NoDatabaseError **.

This happens because the web container does not support the ** caching_sha2_password authentication method ** of mysql 8.0. Follow the steps below to change to the old authentication method ** mysql_native_password **.

8. 8. Change of MySQL authentication method

First of all, enter the DB container and start bash.

$ docker-compose exec db bash

Then connect with the mysql command.


# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL

...

You can see the user list and its authentication method by sending the following query.


mysql> select User,Host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

Change ** caching_sha2_password ** in the rightmost plugin to ** mysql_native_password **. Use ** ALTER USER ** to change the target ** root @% ** user settings this time.


ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '';
-- Query OK, 0 rows affected (0.02 sec)

After change

mysql> select User,Host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

9. Access the Rails home screen.

If you do ** db: prepare ** and create a DB, you will be able to access the Rails home screen. Rails.png

Recommended Posts

[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
I tried to create React.js × TypeScript × Material-UI on docker environment
I tried to create a padrino development environment with Docker
I tried to create a Spring MVC development environment on Mac
Rails + MySQL environment construction with Docker
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
I tried to create a java8 development environment with Chocolatey
[Rails] I tried to create a mini app with FullCalendar
I tried running Docker on Windows Server 2019
Create Rails 6 + MySQL environment with Docker compose
Create a MySQL environment with Docker from 0-> 1
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
I tried to build a laravel operating environment while remembering Docker
I built a rails environment with docker and mysql, but I got stuck
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
I tried to build a Firebase application development environment with Docker in 2020
01. I tried to build an environment with SpringBoot + IntelliJ + MySQL (MyBatis) (Windows10)
[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)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
I tried migrating the portfolio created on Vagrant to the Docker development environment
Rails6 [API mode] + MySQL5.7 environment construction with Docker
I tried to create a LINE clone app
[Personal memo] Ruby on Rails environment construction (Windows)
I tried running Ansible on a Docker container
Rails Docker environment construction
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Build a development environment to create Ruby on Jets + React apps with Docker
[Introduction] Try to create a Ruby on Rails application
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
I tried to create a Clova skill in Java
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
I tried using Docker Desktop for Windows on Windows 10 Home
Rails6 I tried to introduce Docker to an existing application
[Ruby on Rails] From MySQL construction to database change
I tried to build an environment using Docker (beginner)
I tried node-jt400 (Environment construction)
MySQL 5.7 (Docker) environment construction memo
A memo when building a Rails 5.2 development environment using Docker Desktop + WSL2 on Windows 10 Home
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
[Docker] Rails 5.2 environment construction with docker
[Rails / MySQL] Mac environment construction
Docker command to create Rails project with a single blow in environment without Ruby
I tried adding a separator line to TabLayout on Android
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I want to create a form to select the [Rails] category
I tried to build the environment little by little using docker
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
I tried to build the environment of WSL2 + Docker + VSCode
I tried running a Docker container on AWS IoT Greengrass 2.0