Singularity

简介

多年来,软件的复杂性不断增加,有时甚至很难安装和运行软件。容器通过将软件及其所有依赖项(包括最小的操作系统)存储在一个大镜像中来解决这个问题,这样就没有什么可安装的了, 当运行软件时,一切都“正常工作”。这使得软件既可共享又可移植,同时确保再现性。

singularity vs docker

Docker镜像并不安全,因为它们提供了一种获得对其运行的系统的root访问权限的方法。因此,Docker在集群1不可用,仅在集群2提供了web访问和使用。 singularity分为三个不同的项目(singularity、singularityCE和singularity)。目前两个集群提供singularity使用。 singularity是Docker的一种替代方案,它既安全又专为高性能计算而设计。singularity与所有Docker镜像兼容,可以与GPU和MPI应用程序一起使用。

使用容器的理由

•singularity镜像将应用程序及其软件依赖项、数据、脚本、文档、许可证和最低操作系统捆绑在一起。这种形式的软件可确保结果的可重复性。事实上,DOI可以从出版物镜像获取到。随着软件依赖性的复杂性不断增长,这种方法变得更有吸引力。

•singularity镜像存储为单个文件,便于共享。您可以将您的镜像托管在 Singularity Cloud Library 上,供其他人下载。你也可以像其他文件一样,把它放在网络服务器上可用。

•singularity镜像可以在任何具有与制作镜像相同的体系结构(例如x86-64)和二进制文件格式的系统上运行。这提供了可移植性。

•在具有某个 glibc 版本的一个系统上构建的软件可能无法在具有较旧glibc的第二个系统上运行。还可能遇到 ABI 兼容性问题,例如标准C++库。singularity可以通过使用更新的基本操作系统和相应的库和工具构建镜像来解决这些问题。

•科学软件通常是为特定的Linux发行版(如Ubuntu)开发的。在其他Linux发行版上安装这样的软件可能很困难。在这种情况下,最简单的方法是使用与编写安装说明相同的基本操作系统来制作singularity镜像。

•singularity可用于运行大规模并行应用程序,这些应用程序利用快速InfiniBand互连和GPU。这些应用程序的性能损失最小,因为singularity被设计为“接近硬件”运行。

•自带软件(BYOS)。也就是说,你不必问系统管理员是否愿意为你安装一些东西。你可以在镜像中安装任何你想要的东西,然后运行它。这是因为没有办法升级特权。容器外部的用户与容器内部的用户相同,因此singularity容器没有额外的安全问题。

流行的容器注册表

在寻找容器化软件时,请尝试以下存储库:

Docker Hub

NVIDIA GPU Cloud

Singularity Cloud Library

Singularity Hub

Quay.io

BioContainers

IBM PowerAI (仅遍历)

AMD InfinityHub (AMD GPUs)

在Docker Hub和Singularity云库上可以很容易地获得您自己的容器镜像。

singularity

一、构建singularity镜像

获取图像:使用pull命令

Singularity 支持很多云平台,用户可以在云平台上搜索,并下载所需镜像

Sylabs Cloud:

singularity build --sandbox ./ubuntu20 docker://ubuntu:20.04

Docker Hub

singularity pull ubuntu.sif docker://ubuntu:latest

Singularity Hub

singularity pull singularity-images.sif shub://vsoch/singularity-images

支持 ORAS 的 OCI 镜像云平台

singularity pull image.sif oras://registry/namespace/image:tag

在/share/install_packages/singularity存放着常用的本地镜像

二、自行制作镜像

当镜像库平台没有找到适用的现成镜像,用户可以在本地将软件打包成镜像,上传到hpc超算上运行。

请注意,用户只能在自己的计算机上制作镜像,不能在超算上制作镜像。

1、安装singularity(本地计算机)

首先需要在本地主机上root权限安装singularity,以下为安装步骤。

#安装go
yum update
yum install -y gcc libuuid-devel squashfs-tools openssl-devel libseccomp-devel fuse-devel
wget https://dl.google.com/go/go1.21.6.linux-amd64.tar.gz
tar -C /usr/local -zxvf go1.21.6.linux-amd64.tar.gz
echo 'export PATH=/usr/local/go/bin:$PATH' >> /etc/profile
source /etc/profile

#安装singularity
mkdir ~/singularity
wget https://github.com/sylabs/singularity/releases/download/v4.1.1/singularity-ce-4.1.1.tar.gz
tar -xvf singularity-ce-4.1.1.tar.gz
cd singularity-ce-4.1.1/
./mconfig --prefix=/root/singularity
make -C ./builddir && make -C ./builddir install
echo 'export PATH=$PATH:/root/singularity/bin' >> /etc/profile
source /etc/profile

2、制作镜像(本地计算机)

# 创建sandbox;
singularity build --sandbox ./ubuntu20_lammps docker://ubuntu:20.04
# 进入创建好的sandbox,并进行修改;
singularity shell -w ./ubuntu20_lammps

# Ubuntu下安装LAMMPS并行版需要安装必要的依赖包
apt-get update && apt upgrade -y
apt-get install openmpi-bin openmpi-doc libopenmpi-dev -y
apt-get install gcc g++ gfortran make wget vim libtool -y

wget http://www.fftw.org/fftw-3.3.9.tar.gz
tar zxvf fftw-3.3.9.tar.gz
cd fftw-3.3.9
./configure --prefix=/opt/software/fftw_3.3.9 --enable-shared --enable-static --enable-fma
make && make install

# 设置临时fftw环境变量
export PATH=/opt/software/fftw_3.3.9/bin:$PATH
export LD_LIBRARY_PATH=/opt/software/fftw_3.3.9/lib:$LD_LIBRARY_PATH

# 安装lammps
wget https://lammps.sandia.gov/tars/lammps-10Feb21.tar.gz #无法下载的话用浏览器或迅雷等下载后上传
tar zxvf lammps-10Feb21.tar.gz
cd lammps-10Feb21/src
vim MAKE/OPTIONS/Makefile.g++_openmpi # 修改如下行
FFT_INC = -DFFT_FFTW -I/opt/software/fftw_3.3.9/include
FFT_PATH = -L/opt/software/fftw_3.3.9/lib
FFT_LIB = -lfftw3

make yes-std
make no-lib
make -j g++_openmpi

mkdir /opt/software/lammps
cp ./lmp_g++_openmpi /opt/software/lammps/

# 设置临时lammps环境变量
export PATH=/opt/software/lammps:$PATH

cp /root/lammps-10Feb21/bench/in.lj /opt/software/lammps
mpirun --allow-run-as-root -np 2 --mca btl ^openib lmp_g++_openmpi -in /opt/software/lammps/in.lj

# 退出容器,设置永久环境变量(宿主机)
vim ./ubuntu20_lammps/environment
# 加入下面两行
export PATH=/opt/software/fftw_3.3.9/bin:/opt/software/lammps:$PATH
export LD_LIBRARY_PATH=/opt/software/fftw_3.3.9/lib:$LD_LIBRARY_PATH

# 验证(宿主机)
singularity exec ./ubuntu20_lammps mpirun --allow-run-as-root -np 2 --mca btl ^openib lmp_g++_openmpi -in /opt/software/lammps/in.lj

# 把修改好的sandbox打包成sif格式;
# 删除不必要的安装包, 如 fftw-3.3.9.tar.gz lammps-10Feb21.tar.gz

singularity build ubuntu20_lammps.sif ./ubuntu20_lammps

三、提交使用(超算平台)

上传创建好的镜像到超算平台,并执行镜像中的软件 在登录节点提交作业 新建作业脚本 test.sh

   #!/bin/bash
   #SBATCH --partition=cpuXeon6458
   #SBATCH --nodes=1
   #SBATCH --ntasks-per-node=8
   #SBATCH --output=slurm_out.%j
   #SBATCH --error=slurm_error.%j

   # 加载超算平台预安装的 singularity
   module load singularity

   # 测试in.lj算例
   singularity exec ./ubuntu20_lammps.sif mpirun -np 8 --mca btl ^openib lmp_g++_openmpi -in /opt/software/lammps/in.lj

提交作业
sbatch test.sh

运行singularity镜像

注意

集群2需要先加载singularity :module load apps/singularity/3.9.9。

要在singularity镜像中运行默认命令,请使用,例如:

$ singularity run ./psortb_commandline_1.0.2.sif <arg-1> <arg-2> ... <arg-N>

请注意,有些容器没有默认命令。请参阅 singularity run 命令的文档。

要运行容器中定义的特定命令,请使用 singularity exec

$ singularity exec ./psortb_commandline_1.0.2.sif <command> <arg-1> <arg-2> ... <arg-N>
$ singularity exec ./psortb_commandline_1.0.2.sif python3 myscript.py 42

使用“shell”命令在容器中运行shell:

$ singularity shell ./psortb_commandline_1.0.2.sif
singularity> cat /etc/os-release
singularity> cd /
singularity> ls -l
singularity> exit

当您试图在容器中查找某些文件时, singularity shell 命令非常有用(请参阅下文)。

容器内查找文件

$ apptainer shell --containall dart_121520.sif
Apptainer> find / -iname "*python*" 2>/dev/null

文件和存储空间

正在运行的容器自动绑定装载以下路径:

  • 个人目录/share/home/<YourUserID>

  • 正在运行容器的空间

您也可以创建自己的绑定挂载路径。有关更多信息,请参阅singularity网站上的 Bind Paths and Mounts


最后更新: 2025 年 06 月 30 日