问题出在我的Dockerfile中:`./bin/rails assets:precompile` 没有成功完成。

huangapple go评论119阅读模式
英文:

Problem with my dockerfile: ./bin/rails assets:precompile" did not complete successfully

问题

构建我的 Dockerfile 时,出现以下错误:

无法解决:进程"/bin/sh -c SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile"未成功完成:退出代码:1

似乎我需要安装 yarn 来预编译我的 Rails 项目中的资产,但我不知道如何做。

这是我的 Dockerfile:

  1. # 语法 = docker/dockerfile:1
  2. # 确保 RUBY_VERSION 与 .ruby-version 和 Gemfile 中的 Ruby 版本匹配
  3. ARG RUBY_VERSION=3.2.2
  4. FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
  5. # Rails 应用程序位于此处
  6. WORKDIR /rails
  7. # 设置生产环境
  8. ENV RAILS_ENV="production" \
  9. BUNDLE_DEPLOYMENT="1" \
  10. BUNDLE_PATH="/usr/local/bundle" \
  11. BUNDLE_WITHOUT="development"
  12. # 丢弃构建阶段以减小最终镜像的大小
  13. FROM base as build
  14. # 安装构建 gems 所需的软件包
  15. RUN apt-get update -qq && \
  16. apt-get install --no-install-recommends -y build-essential git yarn nodejs libpq-dev libvips pkg-config
  17. # 安装应用程序 gems
  18. COPY Gemfile Gemfile.lock ./
  19. RUN bundle install && \
  20. rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
  21. bundle exec bootsnap precompile --gemfile
  22. # 复制应用程序代码
  23. COPY . .
  24. # 预编译用于更快启动时间的 bootsnap 代码
  25. RUN bundle exec bootsnap precompile app/ lib/
  26. # 在不需要秘密的情况下为生产预编译资产
  27. RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
  28. # 最终的应用程序镜像阶段
  29. FROM base
  30. # 安装部署所需的软件包
  31. RUN apt-get update -qq && \
  32. apt-get install --no-install-recommends -y curl libvips postgresql-client && \
  33. rm -rf /var/lib/apt/lists /var/cache/apt/archives
  34. # 复制构建的工件:gems、应用程序
  35. COPY --from=build /usr/local/bundle /usr/local/bundle
  36. COPY --from=build /rails /rails
  37. # 以非根用户的身份运行并拥有仅运行时文件以提高安全性
  38. RUN useradd rails --create-home --shell /bin/bash && \
  39. chown -R rails:rails db log storage tmp
  40. USER rails:rails
  41. # 入口点准备数据库。
  42. ENTRYPOINT ["/rails/bin/docker-entrypoint"]
  43. # 默认情况下启动服务器,可以在运行时进行覆盖
  44. EXPOSE 3000
  45. CMD ["./bin/rails", "server"]
英文:

When building my dockerfile I have the following error:

failed to solve: process "/bin/sh -c SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile" did not complete successfully: exit code: 1

It seems like I need to install yarn to precompile the assets on my rails project but I don't know how to do it.

Here is my dockerfile

  1. # syntax = docker/dockerfile:1
  2. # Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
  3. ARG RUBY_VERSION=3.2.2
  4. FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
  5. # Rails app lives here
  6. WORKDIR /rails
  7. # Set production environment
  8. ENV RAILS_ENV="production" \
  9. BUNDLE_DEPLOYMENT="1" \
  10. BUNDLE_PATH="/usr/local/bundle" \
  11. BUNDLE_WITHOUT="development"
  12. # Throw-away build stage to reduce size of final image
  13. FROM base as build
  14. # Install packages needed to build gems
  15. RUN apt-get update -qq && \
  16. apt-get install --no-install-recommends -y build-essential git yarn nodejs libpq-dev libvips pkg-config
  17. # Install application gems
  18. COPY Gemfile Gemfile.lock ./
  19. RUN bundle install && \
  20. rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
  21. bundle exec bootsnap precompile --gemfile
  22. # Copy application code
  23. COPY . .
  24. # Precompile bootsnap code for faster boot times
  25. RUN bundle exec bootsnap precompile app/ lib/
  26. # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
  27. RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
  28. # Final stage for app image
  29. FROM base
  30. # Install packages needed for deployment
  31. RUN apt-get update -qq && \
  32. apt-get install --no-install-recommends -y curl libvips postgresql-client && \
  33. rm -rf /var/lib/apt/lists /var/cache/apt/archives
  34. # Copy built artifacts: gems, application
  35. COPY --from=build /usr/local/bundle /usr/local/bundle
  36. COPY --from=build /rails /rails
  37. # Run and own only the runtime files as a non-root user for security
  38. RUN useradd rails --create-home --shell /bin/bash && \
  39. chown -R rails:rails db log storage tmp
  40. USER rails:rails
  41. # Entrypoint prepares the database.
  42. ENTRYPOINT ["/rails/bin/docker-entrypoint"]
  43. # Start the server by default, this can be overwritten at runtime
  44. EXPOSE 3000
  45. CMD ["./bin/rails", "server"]

答案1

得分: 0

我使用 bin/rails generate dockerfile 重新生成了 Dockerfile,最后它成功运行了。

英文:

I re-generate the dockerfile by using bin/rails generate dockerfile and it worked in the end.

huangapple
  • 本文由 发表于 2023年6月8日 02:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定