131
129

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-04-26

背景

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が実行出来てストレスが軽減出来るネー。

131
129
1

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
131
129

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?