Rails6 [API mode] + MySQL5.7 environment construction with Docker

Introduction

For myself Rails6 API mode + MySQL5.7 environment construction with Docker (docker compose)

Introduction of Docker itself is omitted

First, create the files necessary for building the environment

Create & move top level directories for apps

$ cd

$ mkdir sample_app

$ cd sample_app

Create Dockerfile, docker-compose.yml, Gemfile, Gemfile.lock

sample_app$ touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock}

sample_app$ ls
Dockerfile docker-compose.yml Gemfile Gemfile.lock

Write the contents of the file

sample_app/Dockerfile file

Dockerfile


FROM ruby:2.6.5

#Install required packages (Install yarn since Rails 6 has Webpacker)
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 build-essential libpq-dev nodejs yarn

#Creating a working directory
RUN mkdir /myapp
WORKDIR /myapp

#Add the host side (local) (left side) Gemfile to the container side (right side) Gemfile
ADD ./Gemfile /myapp/Gemfile
ADD ./Gemfile.lock /myapp/Gemfile.lock

#Gemfile bundle install
RUN bundle install
ADD . /myapp

sample_app/docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: root
    ports:
    - "3306:3306"

  web:
    build: .
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'"
    tty: true
    stdin_open: true
    depends_on:
      - db
    ports:
      - "3001:3001"
    volumes:
      - .:/myapp

sample_app/Gemfile

Gemfile


source 'https://rubygems.org'

gem 'rails', '~> 6.0.3'

Gemfile.lock remains empty

Create a Rails app with the docker-compose run command

$ docker-compose run web rails new . --force --database=mysql --skip-bundle --api --webpacker

Modify database.yml file

database.yml


default: &dafault
	adapter: mysql2
	encoding: utf8
	pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
	username: root
	password: password  # docker-compose.yml MYSQL_ROOT_PASSWORD
	host: db    # docker-compose.yml service name

Start a Docker container

$ docker-compose build

$ docker-compose up -d

Create a DB

$ docker-compose up -d

$ docker ps -a

$ docker exec -it <Container ID> /bin/bash

$ rails db:create
$ rails db:migrate

$ exit

$ docker-compose run web rails db:create
$ docker-compose run web rails db:migrate


That's all for construction. Now opens on localhost: 3001.



Other

When stopping the server

--Do not stop with Ctrl + C. The container remains and an error occurs at the next startup --If you do, delete tmp/pids/server.pid and restart with docker-compose up again

$ docker-compose down

Reflect changes in Dockerfile and docker-compose.yml, restart rails server

$ docker-compose up --build

If you want to execute a command such as bundle install

#  docker-compose run {Service name} {Arbitrary command}
$ docker-compose run web bundle install

Connect to MySQL container locally

--Start if the container is not started

$ docker-compose up -d

--Check mysql id

$ docker ps

--Login to MySQL container

$ docker exec -it <MySQL container ID> /bin/bash

$ mysql -u root -p -h 0.0.0.0 -P 3306 --protocol=tcp

mysql>

//escape
mysql> quit


that's all.



Articles that I used as a reference

Rails5 + MySQL on Docker environment construction by too polite Docker-compose (Docker for Mac) [Rails] Building an environment with Rails 6.0 x Docker x MySQL

Recommended Posts

Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
[Docker] Rails 5.2 environment construction with docker
Build Rails (API) x MySQL x Nuxt.js environment with Docker
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]
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
React + Django + Nginx + MySQL environment construction with Docker
[Rails / MySQL] Mac environment construction
React environment construction with Docker
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Rails on Docker environment construction procedure
Easy environment construction of MySQL and Redis with Docker and Alfred
Docker environment construction
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
Rails API server environment construction using docker-compose
Create a MySQL environment with Docker from 0-> 1
Rails & React & Webpacker & MySQL Environment Construction Manual
How to build Rails 6 environment with Docker
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]
I built a rails environment with docker and mysql, but I got stuck
Spring Boot environment construction with Docker (January 2021 version)
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
Environment construction command memo with Docker on AWS
Rails deploy with Docker
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
[Docker] Connection with MySQL
[Rails] Development with MySQL
[Rails] How to build an environment with Docker
Wordpress local environment construction & development procedure with Docker
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
Stable development environment construction manual for "Rails6" with "Docker-compose"
Database environment construction with Docker in Spring boot (IntellJ)
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
API creation with Rails + GraphQL
Run Rails whenever with docker
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction