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

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

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:

# 语法 = docker/dockerfile:1

# 确保 RUBY_VERSION 与 .ruby-version 和 Gemfile 中的 Ruby 版本匹配
ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails 应用程序位于此处
WORKDIR /rails

# 设置生产环境
ENV RAILS_ENV="production" \
    BUNDLE_DEPLOYMENT="1" \
    BUNDLE_PATH="/usr/local/bundle" \
    BUNDLE_WITHOUT="development"

# 丢弃构建阶段以减小最终镜像的大小
FROM base as build

# 安装构建 gems 所需的软件包
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git yarn nodejs libpq-dev libvips pkg-config

# 安装应用程序 gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
    bundle exec bootsnap precompile --gemfile

# 复制应用程序代码
COPY . .

# 预编译用于更快启动时间的 bootsnap 代码
RUN bundle exec bootsnap precompile app/ lib/

# 在不需要秘密的情况下为生产预编译资产
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

# 最终的应用程序镜像阶段
FROM base

# 安装部署所需的软件包
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl libvips postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# 复制构建的工件:gems、应用程序
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# 以非根用户的身份运行并拥有仅运行时文件以提高安全性
RUN useradd rails --create-home --shell /bin/bash && \
    chown -R rails:rails db log storage tmp
USER rails:rails

# 入口点准备数据库。
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# 默认情况下启动服务器,可以在运行时进行覆盖
EXPOSE 3000
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

# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base as build
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git yarn nodejs libpq-dev libvips pkg-config
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
# Copy application code
COPY . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image
FROM base
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails
# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER rails:rails
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
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:

确定