Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker

The environment created by Sample in docker was Ruby2.5 + Rails5 + PostgreSQL, which was old and PostgreSQL, so It is rewritten for each new version Ruby2.7 & Rails6.0 & MySQL8.0.

File preparation

  1. Dockerfile

1. Preparation of Dockerfile

FROM ruby:2.7
# or latest

#nodejs and mysql-client (default-mysql-client)Installation of
RUN apt-get update -qq && apt-get install -y curl apt-transport-https wget nodejs default-mysql-client

#Install Yarn
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 && \
  apt-get update -qq && apt-get install -y yarn

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# 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"]

2. Preparation of Gemfile

source 'https://rubygems.org'
gem 'rails', '~>6.0.0'

3. Prepare Gemfile.lock

$ touch Gemfile.lock
Step 7/14 : COPY Gemfile.lock /myapp/Gemfile.lock
ERROR: Service 'web' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder424702045/Gemfile.lock: no such file or directory

4. Prepare entrypoint.sh

#!/bin/bash
set -e

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

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

5. Prepare docker-compose.yml

version: '3'
services:
  db:
    image: mysql:8.0 # or lates:
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
    volumes:
      - db-data:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - ${DB_PORT}:3306
  web:
    build: .
    env_file: .env
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
      - bundle-data:/usr/local/bundle
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  db-data:
  bundle-data:

6. Prepare .env

DB_ROOT_PASSWORD=mysql_root_password
DB_USER=myapp
DB_PASSWORD=myapp_password
DB_PORT=3306

This completes the file preparation. Here is what I have described so far. https://github.com/madai0517/rails6-mysql8_docker


Build execution

rails new

$ docker-compose run web rails new . --force --no-deps --database=mysql --webpacker --skip-test
$ sudo chown -R $USER:$USER . #Required for Linux. Not required if you're on a Mac

DB connection settings

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: db

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Start, grant permissions to DB users, create DB

$ docker-compose build
$ docker-compose up -d
$ docker-compose exec db mysql -uroot -p -e"GRANT ALL PRIVILEGES ON *.* TO 'myapp'@'%';FLUSH PRIVILEGES;"
$ docker-compose run web rails db:create

This completes the environment construction. You should be able to see the usual screen by accessing http: // localhost: 3000 /. スクリーンショット 2020-06-08 3.07.23.png

Recommended Posts

Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Build an environment with Docker on AWS
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Build an environment of "API development + API verification using Swagger UI" with Docker
Build docker environment with WSL
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build docker + laravel environment with laradock
Build a PureScript development environment with Docker
I tried to build the environment of PlantUML Server with Docker
Build a Wordpress development environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an Ultra96v2 development environment on Docker 1
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
Self-hosting with Docker of AuteMuteUs in Windows environment
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Build an E2E test environment with Selenium (Java)
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
[Error resolution] Occurs when trying to build an environment for spring with docker
How to build docker environment with Gradle for intelliJ
Easily build a Vue.js environment with Docker + Vue CLI
Build ruby debug environment with VS Code of Windows 10
Create an E2E test environment with Docker x Cypress
[Note] Build a Python3 environment with Docker in EC2
Build Java development environment with WSL2 Docker VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build an Android image for Orange Pi 4 with Docker
Build WordPress environment with Docker (Local) and AWS (Production)
I tried to build an environment using Docker (beginner)
Pytorch execution environment with Docker
[Docker] Rails 5.2 environment construction with docker
React environment construction with Docker
Build DynamoDB local with Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Easy environment construction of MySQL and Redis with Docker and Alfred
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
I tried to build the environment of WSL2 + Docker + VSCode
[Oracle Cloud] Build a 4-Node RAC environment of Oracle Database 19c with Docker on OCI Compute
Rails + MySQL environment construction with Docker
Create a Vue3 environment with Docker!
Node.js environment construction with Docker Compose
Build an environment where pip3 can be used with CentOS7 + Python3
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Environment construction with Docker for beginners
I tried to build an http2 development environment with Eclipse + Tomcat
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
Build an ASP.net Core Web API environment on Docker (VSCode) Part 1
Create SolrCloud verification environment with Docker
How to build an environment with Docker, which is the minimum required to start a Rails application