LoginSignup
4
3

More than 5 years have passed since last update.

rails5.1.6 + ruby2.5 + SQLServer on dockerの環境

Posted at

SQLServerを使ったrailsアプリの環境を作ろうと試したので、構築手順を紹介します。
下記を参考にしました。

https://docs.docker.com/compose/rails/
https://qiita.com/AQeNku/items/9c4fa5ba2624a4fd702e

手順

最初のbuildの時のGemfileを作成

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

Gemfile.lock作っておく

touch Gemfile.lock

参考サイトのまんまですが、rails serverの立ち上げ状態をチェックするファイルは毎回消したいので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 "$@"

次にSQLServerを動かすために必要なライブラリをインストールするDockerfileを作ります。
activerecord-sqlserver-adapterを使うためにはfreetdsというライブラリが必要なのでインストールします。
freetdsは1.0以上のバージョンで動かす必要がありますが、apt-get installでは入らないので直接ファイルを落としてインストールしています。

FROM ruby:2.5

RUN mkdir /myapp

ENV APP_ROOT=/myapp

WORKDIR $APP_ROOT

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

RUN wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.27.tar.gz && \
    tar -xzf freetds-1.00.27.tar.gz && \
    cd freetds-1.00.27 && \
     ./configure --prefix=/usr/local --with-tdsver=7.3 && \
    make && \
    make install

COPY Gemfile $APP_ROOT
COPY Gemfile.lock $APP_ROOT

RUN bundle install

COPY . $APP_ROOT

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"]

tdsのバージョンは下記の表を参考に要件に合わせて指定します。
2019-02-1012.53.46.png

sqlserverと共に立ち上げるためのdocker-compose.yml

version: '3'
services:
  mssql:
    image: "microsoft/mssql-server-linux"
    container_name: myapp_mssql
    ports:
    - 1433:1433
    environment:
      SA_PASSWORD: "YourPassword"
      ACCEPT_EULA: "Y"
  app:
    container_name: myapp_rails
    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:
      - mssql

いちどrails newを実行したいので、下記コマンドでアプリケーションを作成。

docker-compose run app rails new . --force --no-deps --database=sqlserver

rails new が終わったら, Gemfileに

# SQLServer
gem 'tiny_tds', '2.1.2'
gem 'activerecord-sqlserver-adapter'

を追記して


docker-compose build

database.ymlはこんな感じにしておきます。

default: &default
  adapter: sqlserver
  username: sa
  password: YourPassword
  host: myapp_mssql
  port: 1433

development:
  <<: *default
  database: testdb

コンテナーをたちあげると localhost:3000 にアクセスできました。

docker-compose up

スクリーンショット 2019-03-03 21.50.19.png

ActiveRecordを使ってみる

まずdb、モデル、テーブル作成までを下記コマンドでやります。

docker-compose exec app rake db:create
docker-compose exec app rails g model SampleUser user_id:integer name:string
docker-compose exec app rake db:migrate

コンソールに入ってActiveRecordのモデルを使ってみます。

docker-compose exec app rails c

下記のコマンドを打てばレコードを作成できました。

user = SampleUser.new(user_id: 1, name: "sample1")
user.save()
4
3
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
4
3