You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建Rails镜像时gem install bundler超时失败求助

Docker构建Rails镜像时Gem安装超时问题

报错详情

Step 8/11 : RUN gem install bundler:2.3.24
 ---> Running in 19f290ad8c53
ERROR:  Could not find a valid gem 'bundler' (= 2.3.24), here is why:
          Unable to download data from https://rubygems.org/ - Net::OpenTimeout: Net::OpenTimeout (https://rubygems.org/specs.4.8.gz)
The command '/bin/sh -c gem install bundler:2.3.24' returned a non-zero code: 2

所用Dockerfile

FROM ubuntu:22.04

# Set the working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs ruby-full
RUN gem update --system
ENV GEM_HOME="/usr/local/bundle"
ENV PATH $GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH
# Install bundler and gems
COPY Gemfile Gemfile.lock ./
ENV BUNDLER_VERSION=2.3.24
RUN gem install bundler:2.3.24
RUN bundle install

# Copy the rest of the application code
COPY . .

# Start your Rails application
CMD ["rails", "server", "-b", "0.0.0.0"]

已排查操作

  • 执行sudo service docker restart重启Docker服务
  • 执行sudo docker system prune清理Docker缓存与无用资源
  • 确认仅当前服务器出现该问题,其他服务器构建正常

解决建议

1. 排查服务器网络连通性

在服务器主机上直接测试与rubygems.org的网络连通性:

# 解析域名,检查DNS是否正常
nslookup rubygems.org
# 测试443端口连通性
telnet rubygems.org 443
# 尝试下载报错中提到的文件
curl -v https://rubygems.org/specs.4.8.gz

若以上命令出现超时,说明服务器存在防火墙拦截、网络带宽不足或代理配置问题,需联系运维调整网络规则。

2. 切换RubyGems国内镜像

修改Dockerfile,将默认源替换为国内镜像以提升下载速度:
在gem install bundler前添加源配置:

RUN gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

同时为bundle配置镜像:

RUN bundle config mirror.https://rubygems.org https://gems.ruby-china.com

修改后的Dockerfile相关片段:

ENV BUNDLER_VERSION=2.3.24
RUN gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
RUN gem install bundler:2.3.24
RUN bundle config mirror.https://rubygems.org https://gems.ruby-china.com
RUN bundle install

3. 延长Gem安装超时时间

为gem install命令添加超时参数,避免因网络波动导致超时:

RUN gem install bundler:2.3.24 --timeout 120

4. 使用Host网络构建

尝试使用主机网络模式进行构建,绕过Docker默认bridge网络的限制:

docker build --network host -t your-rails-app-image .

内容的提问来源于stack exchange,提问作者SudoByter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 20:06:23