LoginSignup
7
7

More than 3 years have passed since last update.

Rails5.2(APIサーバーとして) + MySQL5.7 + Docker3メモ

Last updated at Posted at 2018-08-02

下記記事をベースにもろもろのバージョンをあげる

https://qiita.com/togana/items/30b22fc39fe6f7a188ec
https://qiita.com/jshimazu/items/ba13ce87dfdb11e2d1d9

Railsアプリ作成する

$ mkdir app_name
$ cd app_name

Rubyを指定したバージョンで取得する

$ docker pull ruby:2.5.1

Gemfile作成する

$ docker run --rm -v "$PWD":/usr/src/app_name -w /usr/src/app_name ruby:2.5.1 bundle init
$ vim Gemfile 
source "https://rubygems.org"
gem 'rails', '5.2.0'

Gemfile.lockを作成する

touch Gemfile.lock

Dockerfileを作成する

$ vim Dockerfile
FROM ruby:2.5.1

ENV APP_ROOT /usr/src/app_name

WORKDIR $APP_ROOT

RUN apt-get update && \
    apt-get install -y nodejs \
                       mysql-client \
                       postgresql-client \
                       sqlite3 \
                       --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

COPY Gemfile $APP_ROOT
COPY Gemfile.lock $APP_ROOT

RUN \
  echo 'gem: --no-document' >> ~/.gemrc && \
  cp ~/.gemrc /etc/gemrc && \
  chmod uog+r /etc/gemrc && \
  bundle config --global build.nokogiri --use-system-libraries && \
  bundle config --global jobs 4 && \
  bundle install && \
  rm -rf ~/.gem

ビルドしてイメージを作成する

$ docker build -t developer_name/app_name .

イメージができているか確認する

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
developer_name/app_name   latest              861aa800bf5c        About an hour ago   1.09GB
ruby                           2.5.1               1d8640b852eb        11 days ago         869MB

apiモードでrails newする

$ docker run --rm -it -v "$PWD":/usr/src/app_name developer_name/app_name rails new --api . -BT
       exist  
   identical  README.rdoc
   identical  Rakefile
   identical  config.ru
   identical  .gitignore
    conflict  Gemfile
Overwrite /usr/src/app_name/Gemfile? (enter "h" for help) [Ynaqdh] Y

Dockerfileを修正する

$ vim Dockerfile
FROM ruby:2.5.1

ENV APP_ROOT /usr/src/app_name

WORKDIR $APP_ROOT

RUN apt-get update && \
    apt-get install -y nodejs \
                       mysql-client \
                       postgresql-client \
                       sqlite3 \
                       --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

COPY Gemfile $APP_ROOT
COPY Gemfile.lock $APP_ROOT

RUN \
  echo 'gem: --no-document' >> ~/.gemrc && \
  cp ~/.gemrc /etc/gemrc && \
  chmod uog+r /etc/gemrc && \
  bundle config --global build.nokogiri --use-system-libraries && \
  bundle config --global jobs 4 && \
  bundle install && \
  rm -rf ~/.gem

# 下記を追記
COPY . $APP_ROOT

EXPOSE  3000
CMD ["rails", "server", "-b", "0.0.0.0"]

ビルドしてイメージを再作成する

$ docker build -t developer_name/app_name .

起動するか確認する

$ docker run -d -p 3000:3000 developer_name/app_name

Dockerfileをdocker-compose(バージョン3)で管理する

$ vim docker-compose.yml
version: '3'
services:
  app:
    build: .
    command: bin/rails s -b "0.0.0.0"
    ports:
      - "3000:3000"
    volumes:
      - ".:/app_name"
    depends_on:
      - db
    environment:
      - DATABASE_URL=mysql2://root:password@db/db_name

  db:
    image: mysql:5.7.10
    environment:
     MYSQL_DATABASE: db_name
     MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
volumes:
  mysql-data:

再ビルドする

$ docker-compose build
$ docker-compose up -d

必要であればdockerを再起動する

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
05a6270674cc        developer_name/app_name   "rails server -b 0.0."   6 seconds ago       Up 5 seconds        0.0.0.0:3000->3000/tcp   goofy_lumiere
$ docker stop 05a6270674cc
05a6270674cc
$ docker-compose up -d

DBをsqliteからmysql2に変更するためGemfileを修正する

$ vim Gemfile
# gem 'sqlite3'
gem 'mysql2'

config/database.ymlもmysql2に対応させる

default: &default
  adapter: mysql2
  encoding: utf8
  port: 3306
  pool: 5
  timeout: 5000
  url: <%= ENV['DATABASE_URL'] %>
development:
  <<: *default
  database: db_development

test:
  <<: *default
  database: db_test

production:
  <<: *default
  database: db_production

再ビルドする

$ docker-compose build
$ docker-compose up -d

ダミーのDBを作成する

$ docker-compose run --rm app rake db:create

最後にlocalhostで動作をきちんと立ち上がっているか確認する

以上

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7