ruby-on-rails – 在Docker中使用nginx为Rails提供预编译资产

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Docker中使用nginx为Rails提供预编译资产前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

目前我正在使用docker设置我的应用.我有一个最小的rails应用程序,有1个控制器.你可以运行以下命令来获取我的设置:

  1. rails new app --database=sqlite --skip-bundle
  2. cd app
  3. rails generate controller --skip-routes Home index
  4. echo "Rails.application.routes.draw { root 'home#index' }" > config/routes.rb
  5. echo "gem 'foreman'" >> Gemfile
  6. echo "web: rails server -b 0.0.0.0" > Procfile
  7. echo "port: 3000" > .foreman

我有以下设置:

Dockerfile:

  1. FROM ruby:2.3
  2. # Install dependencies
  3. RUN apt-get update && apt-get install -y \
  4. nodejs \
  5. sqlite3 \
  6. --no-install-recommends \
  7. && rm -rf /var/lib/apt/lists/*
  8. # Configure bundle
  9. RUN bundle config --global frozen 1
  10. RUN bundle config --global jobs 7
  11. # Expose ports and set entrypoint and command
  12. EXPOSE 3000
  13. CMD ["foreman","start"]
  14. # Install Gemfile in different folder to allow caching
  15. WORKDIR /tmp
  16. COPY ["Gemfile","Gemfile.lock","/tmp/"]
  17. RUN bundle install --deployment
  18. # Set environment
  19. ENV RAILS_ENV production
  20. ENV RACK_ENV production
  21. # Add files
  22. ENV APP_DIR /app
  23. RUN mkdir -p $APP_DIR
  24. COPY . $APP_DIR
  25. WORKDIR $APP_DIR
  26. # Compile assets
  27. RUN rails assets:precompile
  28. VOLUME "$APP_DIR/public"

VOLUME“$APP_DIR / public”正在创建一个与Nginx容器共享的卷,它在Dockerfile中有这个:

  1. FROM Nginx
  2. ADD Nginx.conf /etc/Nginx/Nginx.conf

然后是docker-compose.yml:

  1. version: '2'
  2. services:
  3. web:
  4. build: config/docker/web
  5. volumes_from:
  6. - app
  7. links:
  8. - app:app
  9. ports:
  10. - 80:80
  11. - 443:443
  12. app:
  13. build: .
  14. environment:
  15. SECRET_KEY_BASE: 'af3...ef0'
  16. ports:
  17. - 3000:3000

这有效,但只是我第一次构建它.如果我更改任何资产,并再次构建图像​​,它们就不会更新.可能因为图像构建时没有更新卷,我想是因为Docker如何处理缓存.

我希望每次运行docker-compose built&& amp;时都会更新资产.码头工人组成.知道怎么做到这一点?

最佳答案
Compose preserves volumes on recreate.

你有几个选择:

>不要为资产使用卷,而是在构建期间构建资产并将其添加或复制到Web容器中
> docker-compose rm应用程序在运行之前删除旧容器和卷.

猜你在找的Docker相关文章