5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rails x SQLite x Docker

Posted at

前提

Ruby on Rails SqLite(Railsのデフォルト)の環境を作成
Ruby on Rails Tutorial等の学習目的にサクッと作成できる環境を目指したのが作成動機
ホスト側にRuby関連の環境が存在しない場合の構築手順

準備すべきファイル

docker-compose.yml
Dockerfile
entrypoint.sh

docker-compose.yml

version: "3.8"
services:
  app:
    build:
      context: .
      dockerfile: ./Dockerfile
    tty: true
    volumes:
      - ./:/myapp
    ports:
      - "3000:3000"
    environment:
      - TZ=Asia/Tokyo
#!/bin/bash
set -e 

rm -f /myapp/tmp/pids/server.pid

# RUN MAIN PROCESS
exec "$@"
FROM ruby:3.2.2

WORKDIR /myapp

COPY Gemfile* /myapp/

RUN apt -qq update && apt install -y build-essential

RUN gem update --system && \
    bundle install

COPY . /myapp

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

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

手順

1 初めはDockerfileの内容を下記のみ残しそれ以外をコメントアウトする

FROM ruby:3.2.2

WORKDIR /myapp

2 docker compose build
3 docker compose run app bash
4 gem install rails
5 rails new . (Dockerfileの上書きの許可を求められるが今回は許可しないを選択)
6 exitでdockerコンテナから抜けてコンテナがdownしていることを確認
7 Dockerfileのコメントアウトを元に戻す
8 docker compose build
9 docker compose up(開発時のログを確認するため -dオプションは不要)

10 他のterminal windowでdocker compose exec app bash (開発時のコマンドはここで叩く)

終わりに

サクッと作れるDocker環境を持っておくと便利です。
shellでスクリプト化してrailssetupとかで自動作成されるようにすると便利そうですね。

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?