"""参考文档
https://apptainer.org/user-docs/master/build_a_container.html
"""
# 通过文件构建容器,相当于docker的Dockerfile
## 创建一个Singularity文件
>>> vim Singularity
Bootstrap: docker
"""
其中Bootstrap可以是:
shub(images hosted on Singularity Hub).
docker(images hosted on Docker Hub).
localimage(images saved on your machine).
yum(yum based systemd such as Centos and Scientific Linux)等
"""
From: ubuntu:18.04
Stage: build
%setup
"""
在构建过程中,该%setup部分中的命令首先在安装基本操作系统后在容器外的主机系统上执行,可以在该部分中使用$SINGULARITY_ROOTFS环境变量引用容器文件系统
"""
touch /file1
touch ${SINGULARITY_ROOTFS}/file2
%files
"""
允许您将文件复制到容器中,比使用该%setup部分更安全
"""
/file1
/file1 /opt
%environment
"""
允许您定义将在运行时设置的环境变量
"""
export LISTEN_PORT=12345
export LC_ALL=C
%post
"""
你可以使用git和wget等工具从互联网上下载文件,安装新的软件和库,编写配置文件,创建新的目录等。类似于dockerfile中的Run
"""
apt-get update && apt-get install -y netcat
NOW=`date`
echo "export NOW=\"${NOW}\"" >> $SINGULARITY_ENVIRONMENT
%runscript
"""
容器运行时执行命令
"""
echo "Container was created $NOW"
echo "Arguments received: $*"
exec echo "$@"
%startscript
"""
容器启动时运行的命令
"""
nc -lp $LISTEN_PORT
%test
"""
在构建过程的最后运行,以使用您选择的方法验证容器
"""
grep -q NAME=\"Ubuntu\" /etc/os-release
if [ $? -eq 0 ]; then
echo "Container base is Ubuntu as expected."
else
echo "Container base is not Ubuntu."
exit 1
fi
%labels
"""
用于将元数据添加到/.singularity.d/labels.json容器内的文件中。
"""
Author d@sylabs.io
Version v0.0.1
%help
"""
该部分中的任何文本%help都会被转录到容器中的元数据文件中。然后可以使用run-help命令显示此文本
"""
This is a demo container used to illustrate a def file that uses all
supported sections.
# 构建镜像
>>> singularity build ubuntu-test.image Singularity
# 运行容器
>>> singularity run ubuntu-test.image