背景:因研究需要,复现某论文,使用的是某校的服务器集群。他们服务器使用的容器是Singularity,而之前自己在院里用的时docker。docker学了没多久,就开始Singularity。恰恰是会一点docker,所以在转用singularity的过程中,踩的坑比较多(造孽啊)。记录一下,前事不忘,后世之师。
目录
一、 容器简介:
1、容器
容器是一种Linux上广为采用的应用封装技术,它将可执行程序与依赖库打包成一个镜像文件,启动时与宿主节点共享操作系统内核。 由于镜像文件同时携带可执行文件和依赖库,避免了两者不匹配造成的兼容性问题,还能在一个宿主Linux操作系统上支持多种不同的Linux发行版,譬如在CentOS发行版上运行Ubuntu的 apt-get
命令。
2、singularity的重要概念
(1)容器(container): 容器是一个包含用户软件和依赖的镜像系统,可独立运行某一条或者多条命令。Singularity没有镜像的概念,用户创建和运行的都是一个一个容器。
(2)SIF(Singularity Image File):压缩后的只读(read-only)的Singularity镜像文件,是生产使用的主要形式。
(3)Sandbox :可写(writable)的容器存在形式,是文件系统中的一个目录,常用于开发或者创建自己的容器,是开发使用的主要形式。
3、singularity 和docker 的区别:
(1)基于 Singularity 的高性能计算容器技术,相比Docker等在云计算环境中使用的容器技术,Singularity 同时支持root用户和非root用户启动,且容器启动前后,用户上下文保持不变,这使得用户权限在容器内部和外部都是相同的。
(2)Singularity与Docker的一个很大区别是生产环境使用的Singulariy容器是只读的SIF格式文件.
(3)Singularity 强调容器服务的便捷性、可移植性和可扩展性,而弱化了容器进程的高度隔离性,因此量级更轻,内核namespace更少,性能损失更小。
docker相关实践参考:
Docker 实践指南——下载、配置及应用等常见命令_此何人哉tan的博客-优快云博客_docker下载文件命令
Docker实践指南(二)之 配置anaconda+远程jupyter notebook容器_此何人哉tan的博客-优快云博客_anaconda 容器
二、获取镜像
1、搜索镜像文件
singularity search alp # 搜索镜像
2、获取镜像文件
# From Sylabs cloud library
singularity pull alpine.sif library://alpine:latest
# From Docker
singularity pull tensorflow.sif docker://tensorflow/tensorflow:latest
# From Shub
singularity pull singularity-images.sif shub://vsoch/singularity-images
# From supporting OCI registry (e.g. Azure Container Registry)
singularity pull image.sif oras://<username>.azurecr.io/namespace/image:tag
3、 定制镜像文件
singularity build --sandbox /tmp/debian docker://debian:latest
singularity exec --writable /tmp/debian apt-get install python
singularity build /tmp/debian2.sif /tmp/debian
三、Singularity容器命令
主要命令就是三个,容易混淆的地方对于 只读的.sif镜像文件,sandbox形式容器和一般容器实例的应用有所不同。
singularity shell
singularity exec
singularity run
1、镜像文件操作
一般我们得到的是.sif文件,这是只读的singularity镜像文件。我们可以直接对这个镜像文件.sif进行操作。如运行镜像文件中的命令(singularity exec/run),进入镜像文件(singularity shell)。进入镜像文件,不能对文件进行读写。比如安装软件、更新软件的操作都是不行的。
(1)singularity exec 运行cat命令
(2)singularity shell 进入镜像文件
(3)singularity run 与singularity exec 不同之处在于,(官方给的,我也不是太清楚,试了一下。singularity run xxx.sif 能进入镜像文件,而singularity exec xxx.sif 不能。其它的好像一样输出)
# Here we see that the runscript prints "Hello world: "
singularity exec /tmp/debian.sif cat /singularity
#!/bin/sh
echo "Hello world: "
# It runs with our inputs when we run the image
singularity run /tmp/debian.sif one two three
Hello world: one two three
# Note that this does the same thing
./tmp/debian.sif one two three
2、sandbox 操作
这是singularity 容器开发的主要操作,也是踩坑比较多的地方。一般情况下,我们从一个.sif镜像文件或者各种库的某个镜像,build一个sandbox形式容器(可指定目录)如:
# 从docker 镜像库中build
singularity build --sandbox /tmp/debian_v1 docker://debian:latest
# tmp/下有一个debian_v1目录, 里面是具体镜像文件
# 从xxx.sif镜像文件中build
singularity build --sandbox /tmp/debian_v2 /tmp/debian.sif
# tmp/下有一个debian_v2目录, 里面是具体镜像文件。
之后就可以用命令:singularity shell -w(-w == --writable,可写),进入sandbox形式容器,中定制自己的镜像文件。如安装某个软件、修改配置文件,搭建自己的pipeline。
singularity shell -w xxx_sandbox/ # 进入容器,并操作
但是涉及到root权限可能需要用到fakeroot。
singularity shell -w -f xxx_sandbox/ # 进入容器,并操作
而fakeroot,需要用户命令空间,用户命名空间如果一开始没有的话,需要宿主机sudo权限才能创建。如CentOS需要运行:
sudo sh -c 'echo user.max_user_namespaces=15000 \
>/etc/sysctl.d/90-max_net_namespaces.conf'
sudo sysctl -p /etc/sysctl.d /etc/sysctl.d/90-max_net_namespaces.conf
具体可以参考:
Singularity 啟用 Fakeroot - Yi Yang's Blog User Namespaces & Fakeroot — Singularity Admin Guide 3.6 documentation
3、创建、删除容器实例:
我们可以在.sif镜像文件基础上,创建一个容器(singularity instance start),运行容器(singularity run),进入容器(singularity shell),停止容器(singularity instance stop)。
singularity instance start /tmp/my-sql.sif mysql
singularity shell instance://mysql
Singularity my-sql.sif> pwd
singularity instance stop /tmp/my-sql.sif mysql
Stopping /tmp/my-sql.sif mysql
参考:
1 Singularity入门之通过沙盒创建镜像_kongxx的博客-优快云博客
2 singularity基本用法_HackerTom的博客-优快云博客_singularity使用
4 Singularity 日常使用指南 · XTAO Achelous
5 singularity安装与使用,安装需要root - 简书
6 容器引擎 - Singularity 使用说明 - 知乎
7 singularity build — Singularity container 3.6 documentation
9、SingularityCE User Guide — SingularityCE User Guide 3.10 documentation