将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
相关推荐
FHYAAAX几秒前
【机器学习】任务十:从函数分析到机器学习应用与BP神经网络
开发语言·python
PyAIGCMaster9 分钟前
python环境中,敏感数据的存储与读取问题解决方案
服务器·前端·python
baozhengw11 分钟前
UniAPP快速入门教程(一)
前端·uni-app
nameofworld21 分钟前
前端面试笔试(二)
前端·javascript·面试·学习方法·数组去重
何曾参静谧38 分钟前
「Py」模块篇 之 PyAutoGUI库自动化图形用户界面库
运维·python·自动化
帅比九日41 分钟前
【HarmonyOS NEXT】实战——登录页面
前端·学习·华为·harmonyos
pumpkin8451444 分钟前
客户端发送http请求进行流量控制
python·网络协议·http
smj2302_796826521 小时前
用枚举算法解决LeetCode第3348题最小可整除数位乘积II
python·算法·leetcode
摇光931 小时前
promise
前端·面试·promise
hummhumm1 小时前
第 12 章 - Go语言 方法
java·开发语言·javascript·后端·python·sql·golang