【メモ】Rails APIモード × MySQL5.7 × Docker で環境構築
はじめに
久しぶりにDockerを作成したら所々躓いたので要点をメモ
手順
- ディレクトリ用意&移動
-
Gemfile
, Gemfile.lock
, Dockerfile
, docker-compose.yml
を作成
- 2.のファイルにコード記述
$ docker-compose build
$ docker-compose run app rails new --api . --force --no-deps --database=mysql
-
config/database.yml
を修正
$ docker-compose build --no-cache
$ docker-compose up
$ docker-compose run app rails db:create
Gemfile
, Gemfile.lock
, Dockerfile
, docker-compose.yml
を作成$ docker-compose build
$ docker-compose run app rails new --api . --force --no-deps --database=mysql
config/database.yml
を修正$ docker-compose build --no-cache
$ docker-compose up
$ docker-compose run app rails db:create
手順3, 6で用意するファイルについて
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
source "https://rubygems.org"
gem 'rails', '6.0.0'
Dockerfile
FROM ruby:2.6.5
ENV LANG C.UTF-8
# install required libraries
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN gem install bundler -v 2.0.2
# install bundler
RUN gem install bundler
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install
WORKDIR /app
COPY . /app
docker-compose.yml
version: '3'
services:
mysql:
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
environment:
MYSQL_DATABASE: app_development
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
app:
tty: true
stdin_open: true
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:
- mysql
volumes:
mysql-data:
driver: local
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
host: mysql
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
# 一例です。productionについてはデプロイする際に注入する環境変数を適宜用意して下さい。
production:
<<: *default
username: <%= ENV['MYSQL_USER'] %>
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
database: <%= ENV['MYSQL_DATABASE'] %>
host: <%= ENV['MYSQL_HOST'] %>
socket: <%= ENV['MYSQL_SOCKET'] %>
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
source "https://rubygems.org"
gem 'rails', '6.0.0'
Dockerfile
FROM ruby:2.6.5
ENV LANG C.UTF-8
# install required libraries
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN gem install bundler -v 2.0.2
# install bundler
RUN gem install bundler
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install
WORKDIR /app
COPY . /app
docker-compose.yml
version: '3'
services:
mysql:
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
environment:
MYSQL_DATABASE: app_development
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
app:
tty: true
stdin_open: true
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:
- mysql
volumes:
mysql-data:
driver: local
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
host: mysql
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
# 一例です。productionについてはデプロイする際に注入する環境変数を適宜用意して下さい。
production:
<<: *default
username: <%= ENV['MYSQL_USER'] %>
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
database: <%= ENV['MYSQL_DATABASE'] %>
host: <%= ENV['MYSQL_HOST'] %>
socket: <%= ENV['MYSQL_SOCKET'] %>
Author And Source
この問題について(【メモ】Rails APIモード × MySQL5.7 × Docker で環境構築), 我々は、より多くの情報をここで見つけました https://qiita.com/fuku_tech/items/ebed95ef6f132dce3096著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .