LoginSignup
2
0

More than 1 year has passed since last update.

Dockerを使ってRails+Elasticsearchの環境構築

Posted at

はじめに

Dockerを使ってRails+Elasticsearchの環境を構築したいと思ったので作成してみました😊

各種バージョン

・OS Mac
・チップ Apple M1
・Ruby 2.7
・Ruby on Rails 6.1.7
・docker 20.10.21
・docker-compose 2.12.1

手順

  1. ファイル構成
  2. アプリを作成する
  3. イメージをビルドする
  4. データベースを接続する
  5. 動作確認

1. ファイル構成

全体の構成は以下のようになっています

% tree .
.
├── Dockerfile
├── Dockerfile-elasticsearch
├── Gemfile
├── Gemfile.lock
├── docker-compose.yml
└── entrypoint.sh

6つのファイルを作成してください🙏

touch Dockerfile
touch Dockerfile-elasticsearch
touch Gemfile
touch Gemfile.lock
touch docker-compose.yml
touch entrypoint.sh

それぞれの中身はこのようになっています✍️

Dockerfile

Dockerfile
FROM ruby:2.7
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 nodejs yarn \
    && mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

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

Dockerfile-elasticsearch

Dockerfile-elasticsearch
FROM docker.elastic.co/elasticsearch/elasticsearch:7.10.1
# 日本語用のプラグイン
RUN bin/elasticsearch-plugin install analysis-kuromoji
# 国際的に規約されてる文字用のプラグイン
RUN bin/elasticsearch-plugin install analysis-icu

docker-compose.yml

docker-compose.yml
version: '3'
services:
   # elasticsearchコンテナ
  elasticsearch:
    build:
      context: .
      dockerfile: Dockerfile-elasticsearch
    environment:
      - discovery.type=single-node
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200
    volumes:
      - esdata:/usr/share/elasticsearch/data
  # kibanaコンテナ
  kibana:
    image: docker.elastic.co/kibana/kibana:7.10.1
    ports:
      - 5601:5601
    depends_on:
      - elasticsearch
  # DBコンテナ
  db:
    # M1対応
    platform: linux/x86_64
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  # Railsコンテナ
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
      - elasticsearch
    stdin_open: true
    tty: true
volumes:
  esdata:
  mysql-data:

Gemfile

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

Gemfile.lock

こちらには何も記入しなくて大丈夫です

entrypoint.sh

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

2. アプリを作成する

下記のコードを入力して、アプリを作成します

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

3. イメージをビルドする

イメージをビルドしてください

% docker-compose build

4. データベースと接続する

データベースと接続するためにconfig/database.yml を編集します

config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("MYSQL_USERNAME", "root") %>
  password: <%= ENV.fetch("MYSQL_PASSWORD", "password") %>
  host: <%= ENV.fetch("MYSQL_HOST", "db") %>

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

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

続いて、データベースを作成します
ターミナルで以下のコードを実行してください

% docker-compose run web rake db:create

5. 動作確認

ターミナルにて、下記のコードを実行します

% docker-compose up

実装できているか確認してください🙏
それぞれ結果が返ってきたらOKです🙆

Rails

localhost:3000

Kibana

localhost:5601

Elasticsearch

localhost:9200
↓結果

{
  "name" : "xxxxxxxxx",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "xxxxxxxxx",
  "version" : {
    "number" : "7.10.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "xxxxxxxxx",
    "build_date" : "2020-12-05T04:58:07.655216Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

最後に

以上で環境構築は終了です。誤字脱字ありましたら、ご連絡ください🙏

参考文献

https://qiita.com/mitsuooo/items/dc2dd995cc94e042e33b

2
0
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
2
0