Rails + MySQL environment construction with Docker

Overview

Remake https://docs.docker.com/compose/rails/ for MySQL.

procedure:

  1. Preparation of configuration file
  2. Create Dockerfile
  3. Create Gemfile
  4. Create entrypoint.sh
  5. Create docker-compose.yaml
  6. Build
  7. Initialize rails project
  8. Image update
  9. Connect with DB
  10. Display confirmation

Preparation of configuration file

Dockerfile creation

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs build-essential libpq-dev
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"]

Create Gemfile

Create a Gemfile in the project root

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

Create Gemfile.lock

touch Gemfile.lock

Create 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 "$@"

Create docker-compose.yaml

Create volume as db_data to make DB data persistent

version: "3"
services:
  db:
    image: mysql:8.0
    volumes:
      - db_data:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password
    ports: 
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: root
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
  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
volumes:
  db_data:

Build

Create a Rail project.

Rails project initialization

Run rails new on the web to initialize rails. Gemfile is updated automatically.

docker-compose run --no-deps web rails new . --force --database=mysql

image update

The Gemfile has been updated, so build the image again.

docker-compose build

Connect with DB

Write DB settings to config/database.yml

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

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

Display confirmation

Container launch

docker-compose up -d

DB creation

docker-compose run web rake db:create

Connect to http: // localhost: 3000 and check the operation.

Recommended Posts

Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
[Docker] Rails 5.2 environment construction with docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails Docker environment construction
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Laravel + MySQL + phpMyadmin environment construction with Docker
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
MySQL 5.7 (Docker) environment construction memo
[Rails / MySQL] Mac environment construction
React environment construction with Docker
React + Django + Nginx + MySQL environment construction with Docker
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Build environment with vue.js + rails + docker
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Build Rails environment with Docker Compose
Rails on Docker environment construction procedure
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Docker environment construction
Easy environment construction of MySQL and Redis with Docker and Alfred
SQL statement learning ~ Environment construction ~ Docker + MySQL
GPU environment construction with Docker [October 2020 version]
Building Rails 6 and PostgreSQL environment with Docker
Laravel development environment construction with Docker (Mac)
Deploy to heroku with Docker (Rails 6, MySQL)
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
Rails & React & Webpacker & MySQL Environment Construction Manual
How to build Rails 6 environment with Docker
Rails deploy with Docker
[Docker] Connection with MySQL
[Rails] Development with MySQL
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Spring Boot environment construction with Docker (January 2021 version)
Environment construction command memo with Docker on AWS
[Rails] How to build an environment with Docker
Wordpress local environment construction & development procedure with Docker
I built a rails environment with docker and mysql, but I got stuck
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Pytorch execution environment with Docker
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails6 (MySQL, Ubuntu environment, Cloud9)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
Run Rails whenever with docker
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
[Docker] Use whenever with Docker + Rails
Stable development environment construction manual for "Rails6" with "Docker-compose"