MacBook M1环境下Rails容器可构建但无法启动问题
问题描述
容器可正常构建,但启动时抛出以下错误:
Activating bundler (~> 2.3) failed:
Could not find 'bundler' (2.3.18) required by$BUNDLER_VERSION.
To update to the latest version installed on your system, runbundle update --bundler.
To install the missing version, rungem install bundler:2.3.18
Checked in 'GEM_PATH=/root/.gem/ruby/2.5.0:/usr/local/lib/ruby/gems/2.5.0:/usr/local/bundle' , executegem envfor more informationTo install the version of bundler this project requires, run
gem install bundler -v '~> 2.3'
当前Dockerfile相关配置:
FROM --platform=linux/amd64 ruby:2.5.9-slim-stretch (...) ENV BUNDLER_VERSION 2.3.18 RUN gem update --system 3.2.3 RUN gem install bundler -v "$BUNDLER_VERSION"
解决思路
检查Bundler安装路径与GEM_PATH匹配
进入容器执行gem env,确认bundler 2.3.18是否安装在报错提示的GEM_PATH目录中。若路径不匹配,可在安装时指定目录:RUN gem install bundler -v "$BUNDLER_VERSION" --install-dir /usr/local/bundle确保启动阶段Bundler版本生效
若构建阶段设置的环境变量在启动时被覆盖,可在启动命令中显式指定Bundler版本:bundle _2.3.18_ exec rails server清理Gem缓存避免版本冲突
在Dockerfile中添加缓存清理步骤,消除缓存导致的版本不一致:ENV BUNDLER_VERSION 2.3.18 RUN gem update --system 3.2.3 && \ gem install bundler -v "$BUNDLER_VERSION" && \ rm -rf /usr/local/bundle/cache/*.gem && \ rm -rf /root/.gem尝试跳过RubyGems系统更新
若gem update --system导致环境异常,可直接安装指定版本的Bundler:ENV BUNDLER_VERSION 2.3.18 RUN gem install bundler -v "$BUNDLER_VERSION"
内容的提问来源于stack exchange,提问作者Daniel Belarmino

