在docker-compose中进行Rails的Gem更新的时候,回避每次执行docker build

背景


现在在使用docker-compose进行开发,但是每次有gemfile更新的时候,都要执行bundle exec,对于在中国的网速来说十分痛苦。目标是只要执行如下的命令就可以实现更新gems

$ 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/
RUN bundle install
ADD . /rails

解决方案:


首先,这是修改前的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之后的docker-compose.yml 的diff如下:

$ 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

从此,告别每次gemfile更新的时候都要执行 docker-compose up --build 的时代了。提高工作效率_!!

你可能感兴趣的:(在docker-compose中进行Rails的Gem更新的时候,回避每次执行docker build)