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

如何在不修改.def文件的情况下向Singularity构建传递主机环境变量?

无需修改Singularity定义文件传递代理与证书变量的方法?

环境信息

  • 系统:Ubuntu 22.04.5 LTS
  • Singularity版本:4.2.0
  • 本地已配置的内部代理(写入.bashrc):
export wss_proxy="VALUE"
export http_proxy="VALUE"
export https_proxy="VALUE"
export ALL_PROXY="VALUE"
export WSS_PROXY="VALUE"
export HTTP_PROXY="VALUE"
export HTTPS_PROXY="VALUE"
export REQUESTS_CA_BUNDLE=/etc/ssl/cacert.pem

其中cacert.pem是机构内部证书合集,缺少它会导致conda、pip无法正常工作。

当前构建场景

使用以下Singularity定义文件(def/python.3.12.2.debian.def)构建镜像:

Bootstrap: docker
From: python:3.12.2

%post
    # 安装Python依赖
    pip3 install --no-cache-dir \
        biopython==1.84

%test
    python --version

执行构建命令时:

sudo singularity build image/python.3.12.2.debian.sif def/python.3.12.2.debian.def

出现SSL证书验证错误:

SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)')))

当前临时解决方案(不推荐)

在定义文件的%post段硬编码代理与证书变量,并绑定本地证书目录:

%post
    export wss_proxy="http://www-cache:3128/"
    export http_proxy="http://www-cache:3128/"
    export https_proxy="http://www-cache:3128/"
    export ALL_PROXY="http://www-cache:3128/"
    export WSS_PROXY="http://www-cache:3128/"
    export HTTP_PROXY="http://www-cache:3128/"
    export HTTPS_PROXY="http://www-cache:3128/"
    export REQUESTS_CA_BUNDLE=/etc/ssl/cacert.pem
    # 安装Python依赖
    pip3 install --no-cache-dir \
        biopython==1.84

构建命令添加绑定参数:

sudo singularity build --bind /etc/ssl:/etc/ssl image/python.3.12.2.debian.sif def/python.3.12.2.debian.def

但这种方式硬编码了代理地址,无法在其他机器复用,属于不良实践。

尝试过的无效方法

尝试用--env参数传递变量,但Singularity的build命令不支持该参数,执行以下命令:

sudo singularity build \
    --bind /etc/ssl:/etc/ssl \
    --env wss_proxy=VALUE \
    --env http_proxy=VALUE \
    --env https_proxy=VALUE \
    --env ALL_PROXY=VALUE \
    --env WSS_PROXY=VALUE \
    --env HTTP_PROXY=VALUE \
    --env HTTPS_PROXY=VALUE \
    --env REQUESTS_CA_BUNDLE=/etc/ssl/cacert.pem \
    image/python.3.12.2.debian.sif \
    def/python.3.12.2.debian.def

报错:

Error for command "build": unknown flag: --env

需求

求无需修改Singularity定义文件,即可向构建过程传递代理与证书相关环境变量的方法。

注:请勿建议联系IT部门,对方明确不支持Linux且无法提供帮助。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 19:50:02