Help us understand the problem. What is going on with this article?

docker-composeでRailsのGemを更新する時、docker buildするのを回避したい

背景

docker-composeを使って開発しているが、Gemを更新する度にdocker buildをするのは辛いしだるい。
ゴールはGemの更新の度にこれが出来れば良い。

$ docker-compose run --rm rails bundle install

ちなみに、現状のDockerfileはこんな感じ。

FROM ruby:2.3.4
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN apt-get install -y qt5-default qt5-qmake libqt5webkit5-dev gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x
RUN mkdir /rails
WORKDIR /rails
ADD Gemfile /rails/Gemfile
ADD Gemfile.lock /rails/Gemfile.lock
RUN bundle install
ADD . /rails

解決策

bundlerによって配備されるディレクトリをdocker-composeが提供するボリュームに入れてあげれば良い。
まずは、現状のdocker-compose.ymlから。

version: '2'
services:
  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci
    volumes:
      - sql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - 3306
  rails:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/rails
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: development
      RAILS_DATABASE_NAME: hoge
      RAILS_DATABASE_USER: root
      RAILS_DATABASE_PASSWORD: password
      RAILS_DATABASE_HOST: db
    tty: true
    stdin_open: true
    links:
      - db
volumes:
  sql-data:
    driver: local

bundler用のボリュームを追加。

$ diff old.yml new.yml
diff --git a/old.yml b/new.yml
index 546cb42..22d618b 100644
--- a/old.yml
+++ b/new.yml
@@ -14,6 +14,7 @@ services:
     command: bundle exec rails s -p 3000 -b '0.0.0.0'
     volumes:
       - .:/rails
+      - bundle:/bundle
     ports:
       - "3000:3000"
     environment:
@@ -29,3 +30,5 @@ services:
 volumes:
   sql-data:
     driver: local
+  bundle:
+    driver: local

これで、Gemの更新の度にdocker buildをせずにdocker-compose run --rm rails bundle installが実行出来てストレスが軽減出来るネー。

resily
OKR導入・運用改善コンサルティングと、自社開発のクラウドOKRツール『Resily』を展開するスタートアップ
https://resily.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした