Way to the science

如何容器化个人常用的计算软件

singularity 是广泛应用于前沿HPC服务商的容器化应用,打包成sif格式的容器可以在同架构安装了singularity的任意linux系统下运行。如,通过singularity容器可以在centos8下运行centos6时代的软件,极大的保证了计算的可靠性和可重复性。下例为集群g16的singularity def打包脚本,供有需求的大家参考:

本例打包的singularity sif文件位于/mnt/softs/singularity_sifs/g16_openmpi.sif, 使用以下命令即可调用:

singularity exec /mnt/softs/singularity_sifs/g16_openmpi.sif g16 x.gjf

or

singularity exec --env GAUSS_LFLAGS="-vv" /mnt/softs/singularity_sifs/g16_openmpi.sif g16 x.gjf

注意,由于gaussian程序对用户权限做了特殊限制 (多数软件无此限制,给予755和可执行权限即可),在其他集群使用时,务必修改g16的gid跟个人所在的任一一个gid相同,或使用root权限执行,否则g16会报错

例:本镜像中g16用户组id是1010,可通过以下命令创建1010用户组,并把当前用户加入g16组
root@flare-5800x:~# sudo groupadd -g 1010 g16

root@flare-5800x:~# usermod -a -G g16 flare

#Bootstrap: localimage
#From: /mnt/softs/singularity_sifs/jammy_base
#From: /mnt/softs/singularity_sifs/g16_openmpi.sif
Bootstrap: debootstrap
OSVersion: jammy
MirrorURL:  https://mirrors.ustc.edu.cn/ubuntu/
Include: apt

%files
	# cp g16 dir to container, one can also cp tar file 
	/mnt/softs/tests/g16 /opt/gaussian16
	# tree /opt/gaussian16
	#/opt/gaussian16/
	#├── basis
	#├── bsd
	#├── doc
	#└── sratch
	#...

%post
	# Install necessary packages
	sudo apt-get update -y
	sudo apt upgrade -y
	apt-get install -y openmpi-bin
	# comment the ulimit commands in g16.profile
	sed -i 's/ulimit/#ulimit/g' /opt/gaussian16/bsd/g16.profile

	chmod 777 -R /opt/gaussian16/
	# add g16 group
	# 注意1010是groupid,container内外的group名字可以不同,只要gid对上就行,即保证与用户所在的任一gid相同即可
	groupadd g16 -g 1010 

	### no need to add users with same uid to the host ####
	#useradd  --no-create-home lym -u xxx
	#usermod -aG g16 lym
	
	# make sure that the g16 dir has the right permission
	sudo chown -R root:g16 /opt/gaussian16
	sudo chmod -R 750 /opt/gaussian16

	
%environment
    export g16root=/opt/gaussian16
	export PATH=$g16root:$PATH
	source $g16root/bsd/g16.profile
	export GAUSS_EXEDIR=$g16root
	export GAUSS_SCRDIR=/tmp
	# ulimit, do this in host

%runscript
    #/usr/bin/python3.8 "$@"

%labels
    #Maintainer="Your Name <your_email@example.com>"
    Description="Singularity container with Python 3.8"
    Version="0.1.0"

Leave a comment

Your email address will not be published. Required fields are marked *