微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

目前我正在使用docker设置我的应用.我有一个最小的rails应用程序,有1个控制器.你可以运行以下命令来获取我的设置:
rails new app --database=sqlite --skip-bundle
cd app
rails generate controller --skip-routes Home index
echo "Rails.application.routes.draw { root 'home#index' }" > config/routes.rb
echo "gem 'foreman'" >> Gemfile
echo "web: rails server -b 0.0.0.0" > procfile
echo "port: 3000" > .foreman

我有以下设置:

Dockerfile:

FROM ruby:2.3

# Install dependencies
RUN apt-get update && apt-get install -y \
      nodejs \
      sqlite3 \
      --no-install-recommends \
      && rm -rf /var/lib/apt/lists/*

# Configure bundle
RUN bundle config --global frozen 1
RUN bundle config --global jobs 7

# Expose ports and set entrypoint and command
EXPOSE 3000
CMD ["foreman","start"]

# Install Gemfile in different folder to allow caching
workdir /tmp
copY ["Gemfile","Gemfile.lock","/tmp/"]
RUN bundle install --deployment

# Set environment
ENV RAILS_ENV production
ENV RACK_ENV production

# Add files
ENV APP_DIR /app
RUN mkdir -p $APP_DIR
copY . $APP_DIR
workdir $APP_DIR

# Compile assets
RUN rails assets:precompile
VOLUME "$APP_DIR/public"

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

FROM Nginx

ADD Nginx.conf /etc/Nginx/Nginx.conf

然后是docker-compose.yml:

version: '2'

services:
  web:
    build: config/docker/web
    volumes_from:
      - app
    links:
      - app:app
    ports:
      - 80:80
      - 443:443
  app:
    build: .
    environment:
      SECRET_KEY_BASE: 'af3...ef0'
    ports:
      - 3000:3000

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

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

解决方法

Compose preserves volumes on recreate.

你有几个选择:

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

原文地址:https://www.jb51.cc/ruby/269730.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐