在Docker中使用nginx服务Rails的预编译资产

目前我正在使用docker设置我的应用程序。 我有一个最小的应用程序,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"正在创build一个与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 

这工作,但只有我第一次build立它。 如果我改变任何资产,并再次build立图像,他们不会更新。 可能是因为卷在映像构build上没有更新,我认为是因为Docker如何处理caching。

我希望每次运行docker-compose built && docker-compose up都要更新资源。 任何想法如何做到这一点?

撰写保留重新创build卷 。

你有几个select:

  1. 不要为资产使用卷,而是在构build期间构build资产并将其ADDCOPY到Web容器中
  2. docker-compose rm app运行之前删除旧的容器和卷。