使用docker构建的Rails容器未复制公用文件夹下的所有文件

时间:2018-12-04 00:32:51

标签: ruby-on-rails docker

您好,我试图分阶段构建Rails docker映像,但我意识到在公共场所不存在文件(例如具有资产的资产文件夹)。

我需要在Docker映像中包含404500个错误页面。

让我告诉我我的Dockerfile:

# Build image for development environment
FROM ruby:2.3.7-alpine AS Development
# Update Alpine and get essentials
ENV BUILD_PACKAGES bash curl-dev ruby-dev build-base git postgresql-dev postgresql-client nodejs tzdata imagemagick file libcrypto1.0 libssl1.0 ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family
RUN apk add --update \
  && apk add --no-cache $BUILD_PACKAGES \
  && rm -rf /var/cache/apk/(*)
# Make dir for app
RUN mkdir /myapp
WORKDIR /myapp
# Copy Gemfile
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
# Install gems for Development
RUN bundle install --jobs 4 --retry 3 \
  && rm -rf /usr/local/bundle/cache/(*).gem \
  && find /usr/local/bundle/gems/ -name "*.c" -delete \
  && find /usr/local/bundle/gems/ -name "*.o" -delete
# Copy bin from image
COPY --from=madnight/docker-alpine-wkhtmltopdf /bin/wkhtmltopdf /bin/
# Create ubuntu user 
RUN addgroup -g 1000 -S ubuntu \
  && adduser -u 1000 -S ubuntu -G ubuntu
# Copy project folder
COPY --chown=ubuntu:ubuntu . /myapp
# Expose port
EXPOSE 3000

# Build image ProductionBuilder
FROM Development AS ProductionBuilder
# Install gems for production
RUN bundle install --without development test --jobs 4 --retry 3 \
  && rm -rf /usr/local/bundle/cache/(*).gem \
  && find /usr/local/bundle/gems/ -name "*.c" -delete \
  && find /usr/local/bundle/gems/ -name "*.o" -delete
# Copy project folder
COPY --from=Development --chown=ubuntu:ubuntu /myapp /myapp
# Define workdir
WORKDIR /myapp
# Compile assets
RUN chmod u+x bin/rails \
  && bin/rails assets:precompile RAILS_ENV=production SECRET_KEY_BASE=token DEVISE_SECRET_KEY=token 
# Expose port
EXPOSE 3000

# Build image for Production environment
FROM ruby:2.3.7-alpine AS Production
# Update system and install essentials
ENV BUILD_PACKAGES git postgresql-client tzdata imagemagick file libcrypto1.0 libssl1.0 ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family
RUN apk add --update \
  && apk add --no-cache $BUILD_PACKAGES \
  && rm -rf /var/cache/apk/(*)
# Make dir for app
ENV PROJECT_PATH /var/www/myapp
RUN mkdir -p $PROJECT_PATH
# Define workdir
WORKDIR $PROJECT_PATH
# Copy gems from ProductionBuilder 
COPY --from=ProductionBuilder /usr/local/bundle/ /usr/local/bundle/
# Copy bin from Development
COPY --from=Development /bin/wkhtmltopdf /bin/
# Copy project from ProductionBuilder
COPY --from=ProductionBuilder /myapp $PROJECT_PATH
# Clean project
RUN rm -Rf /var/www/myapp/tmp/cache /var/www/myapp/app/assets /var/www/myapp/vendor/assets /var/www/myapp/lib/assets /var/www/myapp/test

COPY ./public $PROJECT_PATH/public

# Expose port
EXPOSE 3000

CMD ["bundle", "exec", "rails", "server", "-e", "production"]

我不知道为什么只有Assets文件夹位于public dir下,而没有将404.html 500.html复制到public。

我正在执行:docker build --rm --tag my_app:latest --target Production .

注意::我改用(*)*来使突出显示语法在  stackoverflow所见即所得

有什么想法吗? 谢谢

1 个答案:

答案 0 :(得分:0)

我的错误,我将/ public / *放在.dockerignore下,现在已解决。