将conda虚拟环境打包并集成到singularity镜像中

1. 使用yml文件打包

bash 复制代码
conda activate your_env
conda env export > environment.yml

编写cond.def文件

bash 复制代码
Bootstrap: docker

From: continuumio/miniconda3

%files
    environment.yml

%post
    /opt/conda/bin/conda env create -f environment.yml

%runscript
    exec /opt/conda/envs/$(head -n 1 environment.yml | cut -f 2 -d ' ')/bin/"$@" 

生成镜像:

bash 复制代码
singularity build conda.sif conda.def

2. 利用tar包

2.1 安装conda-pack

bash 复制代码
pip install conda-pack

版本需要0.7以上。

2.2 导出tar包

bash 复制代码
conda-pack -n <MY_ENV> -o packed_environment.tar.gz

编写conda.def文件:

bash 复制代码
Bootstrap: docker

From: continuumio/miniconda3

%files
    packed_environment.tar.gz /packed_environment.tar.gz

%post
    tar xvzf /packed_environment.tar.gz -C /opt/conda
    conda-unpack
    rm /packed_environment.tar.gz

生成镜像:

bash 复制代码
singularity build --fakeroot <OUTPUT_CONTAINER.sif> conda.def

3. 在已有基础上构建

def:

bash 复制代码
Bootstrap: localimage
From: local_image.sif

%environment
    # set up environment for when using the container
    . /opt/conda/etc/profile.d/conda.sh
    conda activate

%post
    apt-get update -y
    apt-get install -y \
            build-essential \
		    wget \
            cmake \
            g++ \
            r-base-dev \
			make
	    

    R -e "install.packages('cowsay', dependencies=TRUE, repos='http://cran.rstudio.com/')"
	
    # install miniconda
    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    bash Miniconda3-latest-Linux-x86_64.sh -b -f -p /opt/conda
    rm Miniconda3-latest-Linux-x86_64.sh

    # install conda components - add the packages you need here
    . /opt/conda/etc/profile.d/conda.sh
    conda activate
    conda install -y -c conda-forge numpy cowpy
    conda update --all

4. 沙盒模式

4.1 构建沙河目录

bash 复制代码
singularity build --sandbox lolcow/ library://sylabs-jms/testing/lolcow

4.2 进入沙盒

bash 复制代码
singularity shell --writable lolcow/

4.3 将沙盒打包成sif

bash 复制代码
singularity build lolcow.sif lolcow/

5. 设置环境变量

pytorchcmake未设置cuda环境变量

bash 复制代码
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "path\\boost_1_80_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "path\\boost_1_80_0\\libs")

可以通过如下设置:

bash 复制代码
%environment
    export CUDA_INCLUDE_DIRS=/opt/conda/cuda/include
    export CUDA_CUDART_LIBRARY=/opt/conda/cuda/lib

    export LIBRARY_PATH=/opt/conda/cuda/lib:$LIBRARY_PATH
    export CPATH=/opt/conda/cuda/include:$CPATH
    export PATH=/opt/conda/cuda:$PATH
%post
   mkdir -p /opt/conda/cuda
   conda install cuda -c nvidia -p /opt/conda/cuda
   
   mkdir -p /opt/conda/cudnn
   conda install -c anaconda cudnn -p /opt/conda/cudnn

   export PATH=/opt/conda/cuda:$PATH
相关推荐
人工智能训练2 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
yaoming1682 小时前
python性能优化方案研究
python·性能优化
会跑的葫芦怪2 小时前
若依Vue 项目多子路径配置
前端·javascript·vue.js
码云数智-大飞3 小时前
使用 Python 高效提取 PDF 中的表格数据并导出为 TXT 或 Excel
python
biuyyyxxx4 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化
极客数模4 小时前
【2026美赛赛题初步翻译F题】2026_ICM_Problem_F
大数据·c语言·python·数学建模·matlab
pas1365 小时前
40-mini-vue 实现三种联合类型
前端·javascript·vue.js
摇滚侠5 小时前
2 小时快速入门 ES6 基础视频教程
前端·ecmascript·es6
小鸡吃米…6 小时前
机器学习中的代价函数
人工智能·python·机器学习
珑墨6 小时前
【Turbo】使用介绍
前端