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

Ubuntu Docker容器安装Ruby时提示source not found问题排查

问题:Docker搭建Jekyll时source ~/.profile报错"source not found"

我正尝试通过Docker搭建Jekyll项目,需要Ruby 3.1及以上版本,但执行source ~/.profile时出现错误:

source not found

以下是我的Dockerfile:

FROM ubuntu:22.04

LABEL maintainer="STS Commercial"

ARG NODE_VERSION=18

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

RUN apt-get update \
    && mkdir -p /etc/apt/keyrings \
    && apt-get install -y git curl autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev gnupg ca-certificates zip unzip libcap2-bin libpng-dev dnsutils librsvg2-bin fswatch ffmpeg nano  \
    && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /etc/apt/keyrings/ppa_ondrej_php.gpg > /dev/null \
    && echo "deb [signed-by=/etc/apt/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php8.3-cli php8.3-dev \
       php8.3-gd \
       php8.3-curl \
    && curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
    && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
    && apt-get update \
    && apt-get install -y nodejs \
    && apt-get install -y ruby-full \
    && apt-get install -y build-essential \
    && apt-get install -y zlib1g-dev \
    && apt-get update \
    && curl -sSL https://get.rvm.io | bash -s stable \
    && rvm install 3.1.2 \
    && npm install -g npm \
    && apt-get update \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN source ~/.profile \
    && rbenv install 3.1.0 \
    && rbenv global 3.1.0 \
    && gem install jekyll bundler 

COPY start-container /usr/local/bin/start-container
RUN chmod +x /usr/local/bin/start-container

EXPOSE 4000

ENTRYPOINT ["start-container"]

问题分析与解决

核心问题

  1. Ruby版本管理工具冲突:你同时使用了RVM和rbenv两种Ruby版本管理工具,二者功能重复且会互相干扰,导致环境加载异常。
  2. source命令的加载路径错误:RVM安装后,初始化脚本默认在/etc/profile.d/rvm.sh,而非~/.profile,直接调用后者无法正确加载RVM环境。
  3. 冗余安装步骤:重复安装了build-essential、zlib1g-dev等依赖,既增大镜像体积,也可能导致环境混乱。

修正后的Dockerfile

FROM ubuntu:22.04

LABEL maintainer="STS Commercial"

ARG NODE_VERSION=18
ARG RUBY_VERSION=3.1.2

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 确保默认shell为bash
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

RUN apt-get update \
    && mkdir -p /etc/apt/keyrings \
    && apt-get install -y git curl autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev gnupg ca-certificates zip unzip libcap2-bin libpng-dev dnsutils librsvg2-bin fswatch ffmpeg nano  \
    # 安装PHP及Composer(若项目不需要可移除)
    && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /etc/apt/keyrings/ppa_ondrej_php.gpg > /dev/null \
    && echo "deb [signed-by=/etc/apt/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php8.3-cli php8.3-dev php8.3-gd php8.3-curl \
    && curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \
    # 安装Node.js
    && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
    && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
    && apt-get update \
    && apt-get install -y nodejs \
    # 安装RVM并指定Ruby版本
    && curl -sSL https://get.rvm.io | bash -s stable \
    # 加载RVM环境并安装Ruby、设置为默认版本
    && /bin/bash -c "source /etc/profile.d/rvm.sh && rvm install $RUBY_VERSION && rvm use $RUBY_VERSION --default" \
    # 更新npm
    && npm install -g npm \
    # 清理系统缓存,减小镜像体积
    && apt-get update \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 加载RVM环境后安装Jekyll和Bundler
RUN /bin/bash -c "source /etc/profile.d/rvm.sh && gem install jekyll bundler"

COPY start-container /usr/local/bin/start-container
RUN chmod +x /usr/local/bin/start-container

EXPOSE 4000

ENTRYPOINT ["start-container"]

关键修正点

  • 移除了rbenv相关命令,统一使用RVM管理Ruby版本,避免工具冲突。
  • 通过/bin/bash -c "source /etc/profile.d/rvm.sh && ..."直接加载RVM官方初始化脚本,解决source命令找不到的问题。
  • 新增RUBY_VERSION参数,方便后续修改目标Ruby版本。
  • 合并重复的依赖安装步骤,清理系统缓存以减小镜像体积。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:45:22