singularityの使い方
はじめに
singularity v3.6.0時点での記事です
メリット
- SingularityとはDockerと同じコンテナプラットフォームで,主にHPC向けに作られたオープンソースソフトウェア
- Dockerより簡単にGPUを使うことができるらしい (最近はnvidia-dockerがdockerに統合されたため,このメリットは微妙)
- Docker互換(Dockerfile, Docker imageを持ってくることができる)
- rootlessでも動作
- 自動で以下の領域をマウント(sudo付けないと一部マウントされないかも)
host | singulalrity |
---|---|
$HOME | $HOME |
/sys | /sys |
/proc | /proc |
/tmp | /tmp |
/var/tmp | /var/tmp |
/etc/resolv.conf | /etc/resolv.conf |
/etc/passwd | /etc/passwd |
$PWD | $PWD |
Dockerとの違い
SingularityのアーキテクチャはDockerと少し異なります. Singularityのコンテナは
- immutableな*.sif(Singularity Image File)
- muutableなsandbox (フォルダ)
の2種類あります. Dockerのコンテナは変更可能なので,singularityで言うsandboxに該当します.
$HOME
などが自動マウントされるので,基本的はsifを使うみたいですが,sifを使ったコンテナは後々pipでライブラリを追加したり出来ません.
Dockerコンテナライクな使い方をしたい場合や,環境を作って行きたい場合はsandboxを使うことをオススメします.
sandboxである程度環境が整った後に.sifに変換することも可能です. - sandboxで扱った後にimuutableにする方法
singularity build some-image.sif sandbox-dir
コンテナ作成方法
- Docker Hub,Singularity Hub, Container Libraryからビルド
- マシン上の既存コンテナからビルド(Docker Imageも可)
- Singularity定義ファイル*.defからビルド
の3種類の方法があります.
build
コマンドを使用してコンテナを作成します.
Usage: singularity build [local options...] <IMAGE PATH> <BUILD SPEC>
コンテナ作成例
pytorch/conda-cudaをDocker Hubから.sif作成
- conda-cuda-torch.sifという名前の.sifファイルを作成する例
singularity build conda-cuda-torch.sif docker://nablascom/cuda-pytorch:latest
sandboxでコンテナを作成するオプション
--sandbox
オプションをつけてIMAGE PATH を任意のフォルダ名(ここではconda-cuda-torch)にすればOK
singularity build --sandbox conda-cuda-torch docker://nablascom/cuda-pytorch:latest
マシン上のdockerにあるhello-world imageから
singularity build hello-world-singularity.sif docker-daemon://hello-world:latest
conda-torch.defから
- Dockerfileに相当するSingularity Definition File(.def)からビルドする例
- sudoが必要
sudo singularity build conda-torch.sif conda-torch.def
コンテナ実行方法
実行コマンドは以下の3つあります.
この記事から引用すると
cmd | 説明 | Usage |
---|---|---|
exec |
ホストシステム上で直接実行されているかのように、 コンテナイメージ内の任意のコマンドを実行できます。 |
singularity exec [exec options...] <container> <command> |
shell |
コンテナ内に対話型シェルを自動的に生成します。 | singularity shell [shell options...] <container> |
run |
コンテナがファイル名で直接実行または実行されたときに 実行されるカスタムアクションを定義できます。 |
singularity run [run options...] <container> |
また,先程作成したコンテナを元にいくつか実行例を示します.
- cuda torch検証用スクリプト
import torch dev = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(dev)
conda-cuda-torch.sifの環境上でpythonスクリプトを実行
- sandboxの場合は.sifの部分に対応するフォルダ名を指定すれば良い
singularity exec conda-cuda-torch.sif python check_torch_cuda.py $ cpu
GPU環境でpythonスクリプト実行
--nv
オプションをつける
singularity exec --nv conda-cuda-torch.sif python check_torch_cuda.py $ cuda:0
shellに入る
singularity shell --nv conda-cuda-torch.sif $ Singularity>
sandbox環境に書き込み可能な状態でshell実行(Dockerのコンテナはこの状態)
- デフォルトは読み取り専用なので、
--writable
を付ける
singularity shell --nv --writable conda-cuda-torch $ Singularity>
run補足
- sif作成時,
%runscript
を指定することで,singularity run file_name.sif
もしくは./file_name.sif
のように実行形式ファイル風に実行できる
port forwardingについて
jupyterを使いたい場合があると思いますが,Singularityはデフォルトで多くの領域をマウントするので,勝手にhostのポートを使ってくれます.(webカメラ等も同様)
DBやWebサーバ等をバックグラウンドで実行したい時
Dockerライクなインターフェイスとして,singularity instance
コマンドがあります.
詳しくはこちらのDocumentへ - https://sylabs.io/guides/3.6/user-guide/cli/singularity_instance.html
singularity instanceでバックグラウンド実行する方法
- 一応jupyter notebookをバックグラウンドで実行して停止する例を一つ出しておきます.
- もちろんDockerのようなポートフォワーディングの記述は不要です.
# cuda-torchは INSTANCE NAME singularity instance start --nv conda-cuda-torch.sif cuda-torch # docker ps的なコマンド singularity instance list $ INSTANCE NAME PID IP IMAGE $ cuda-torch 14030 /home/oda/conda-cuda-torch.sif # shell起動 singularity shell instance://cuda-torch Singularity> jupyter notebook ... # exitで抜ける Singularity> exit # インスタンスを停止 singularity instance stop cuda-torch # インスタンスがないことを確認 singularity instance list $ INSTANCE NAME PID IP IMAGE
まとめ
- Docker互換
- コンテナはimmutableな.sifとsandboxがある
- build時に
--sandbox
を指定しかつ,実行時に--writable
を指定すると書き込み可能 - 実行は
exec
,shell
,run
がある - 実行時,勝手にマウント
--nv
コマンドだけでGPU対応- バックグラウンドで動かしたいときDockerライクなコマンド
instance
がある
参考
公式Document top page
公式Document Quick Start
公式Document Build
- 公式Document Instance
https://sylabs.io/guides/3.6/user-guide/cli/singularity_instance.html
- SingularityでDockerコンテナを使いたい
- [第2回 Singularity 勉強会] Singularity Hands-on
- SingularityコンテナをBuildする方法まとめ(Singularity v3.0.3)