---
# System prepended metadata

title: singularity 指令
tags: [singularity]

---

# singularity 指令
###### tags: `singularity`

* 下載已存在鏡像
和Docker類似，要運行 Singularity 也需要先獲取鏡像，Singularity 可以從Singularity Hub 或者 Docker Hub 來獲取已存在的鏡像。
```
Singularity Hub
singularity -d build lolcow.simg shub://GodloveD/lolcow

Docker Hub
singularity -d build lolcow.simg docker://godlovedc/lolcow
singularity -d build centos.simg docker://centos
singularity -d build ubuntu.simg docker://ubuntu
``` 
* 交互模式運行
```
singularity shell ubuntu.simg
Singularity: Invoking an interactive shell within container...

Singularity ubuntu.simg:~> pwd
/home/admin
Singularity ubuntu.simg:~> id
uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel)
```
* 執行一個命令並退出
```
singularity exec ubuntu.simg bash -c  "pwd && id"
/home/admin
uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel)
```
* 運行一個容器
```
singularity run ubuntu.simg
admin@bdmaster:~$ pwd
/home/admin
admin@bdmaster:~$ id
uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel)
```
### 後臺運行容器實例
* 啓動實例
```
singularity instance.start ubuntu.simg test1
singularity instance.start ubuntu.simg test2
```
*  查看實例
```
singularity instance.list
DAEMON NAME      PID      CONTAINER IMAGE
test1            14172    /home/admin/ubuntu.simg
test2            14239    /home/admin/ubuntu.simg
```
* 操作實例
可以通過 shell, exec, run 命令來連到容器中運行命令
```
singularity shell instance://test1
Singularity ubuntu.simg:~> ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
admin      1     0  0 03:14 ?        00:00:00 singularity-instance: admin [test1]
admin      3     0  3 03:14 pts/0    00:00:00 /bin/bash --norc
admin      4     3  0 03:14 pts/0    00:00:00 ps -ef
```
* 使用 exec 執行命令
```
singularity exec instance://test1 ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
admin      1     0  0 03:14 ?        00:00:00 singularity-instance: admin [test1]
admin      6     0  0 03:15 pts/0    00:00:00 ps -ef
```
* 停止實例
```
singularity instance.stop test1
```
* 綁定目錄
在 Singularity 中也可以在 shell, run, instance.start 等命令中通過 “-B” 選項來實現 Docker 中 “-v” 選項提供掛載卷的功能，比
```
singularity shell -B /apps:/apps ubuntu.simg
```
[來源網址](https://www.twblogs.net/a/5d425013bd9eee5174235098)
[上一屆超電共筆](https://hackmd.io/@tings0802/sc_note/https%3A%2F%2Fhackmd.io%2F%40tings0802%2Fsingularity)
[userguide](https://sylabs.io/guides/3.5/user-guide/index.html)
[使用教學](https://blog.gtwang.org/linux/singularity-3-installation-and-usage-tutorial/)