# Singularity ###### tags: `container` `singularity` ``` Introduction of the singularity - Reproducible software stacks - Mobility of compute - Compatibility with complicated architectures - Security model Singularity has two primary roles: - Container Image Generator - Container Runtime Singularity supported formats types. - compressed read-only ​squashfs​ file system suitable for production (default) - writable ​ext3​ file system suitable for interactive development (--writable option) - writable (ch)root directory called a ​sandbox​ for interactive development (--sandbox option) Singularity-Hub https://singularityhub.github.io/containers/registry/singularity-hub-registry/ Installation on ubuntu 16.10 yakkety release apt-get update; apt-get install -y git autoconf libtool python make git clone ​https://github.com/singularityware/singularity.git cd singularity/ git checkout 2.4.1 ./autogen.sh ./configure --prefix=/usr/local make make install Basic usage of singularity root@singularity:~# singularity --help USAGE: singularity [global options...] <command> [command options...] ... GLOBAL OPTIONS: -d|--debug Print debugging information -h|--help Display usage summary -s|--silent Only print errors -q|--quiet Suppress all normal output --version Show application version -v|--verbose Increase verbosity +1 -x|--sh-debug Print shell wrapper debugging information GENERAL COMMANDS: help Show additional help for a command or container selftest Run some self tests for singularity install CONTAINER USAGE COMMANDS: exec Execute a command within container run Launch a runscript within container shell Run a Bourne shell within container test Launch a testscript within container CONTAINER MANAGEMENT COMMANDS: apps List available apps within a container bootstrap *Deprecated* use build instead build Build a new Singularity container check Perform container lint checks inspect Display container's metadata mount Mount a Singularity container image pull Pull a Singularity/Docker container to $PWD COMMAND GROUPS: image Container image command group instance Persistent instance command group CONTAINER USAGE OPTIONS: see singularity help <command> For any additional help or support visit the Singularity website: http://singularity.lbl.gov/ [ pull a singularity image (squashfs) for singularity hub ] root@singularity:~# singularity pull --name hello-world.simg shub://vsoch/hello-world Progress |===================================| 100.0% Done. Container is at: /root/hello-world.simg root@singularity:~# file ./hello-world.simg ./hello-world.simg: a /usr/bin/env run-singularity script executable (binary data) [ run the singularity image ] root@singularity:~# ./hello-world.simg RaawwWWWWWRRRR!! root@singularity:~# singularity run hello-world.simg RaawwWWWWWRRRR!! [ get shell in the singularity image ] root@singularity:~# singularity shell hello-world.simg Singularity: Invoking an interactive shell within container... Singularity hello-world.simg:~> df Filesystem 1K-blocks Used Available Use% Mounted on OverlayFS 1023824 0 1023824 0% / /dev/sda1 18447056 2635152 14851804 16% /tmp udev 1002248 0 1002248 0% /dev tmpfs 1023824 0 1023824 0% /dev/shm tmpfs 204768 3328 201440 2% /etc/resolv.conf Singularity hello-world.simg:~> cat /etc/os-release NAME="Ubuntu" VERSION="14.04.5 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.5 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" [ bind an path in the singularity image and access it ] root@singularity:~# cat /data/vader.sez I am your father root@singularity:~# singularity exec --bind /data:/mnt hello-world.simg cat /mnt/vader.sez I am your father [ build singularity container in sandbox directory ] root@singularity:~# singularity build --sandbox ubuntu/ docker://ubuntu Docker image path: index.docker.io/library/ubuntu:latest Cache folder set to /root/.singularity/docker Importing: base Singularity environment Importing: /root/.singularity/docker/sha256:660c48dd555dcbfdfe19c80a30f557ac57a15f595250e67bfad1e5663c1725bb.tar.gz Importing: /root/.singularity/docker/sha256:4c7380416e7816a5ab1f840482c9c3ca8de58c6f3ee7f95e55ad299abbfe599f.tar.gz Importing: /root/.singularity/docker/sha256:421e436b5f80d876128b74139531693be9b4e59e4f1081c9a3c379c95094e375.tar.gz Importing: /root/.singularity/docker/sha256:e4ce6c3651b3a090bb43688f512f687ea6e3e533132bcbc4a83fb97e7046cea3.tar.gz Importing: /root/.singularity/docker/sha256:be588e74bd348ce48bb7161350f4b9d783c331f37a853a80b0b4abc0a33c569e.tar.gz Importing: /root/.singularity/metadata/sha256:c6a9ef4b9995d615851d7786fbc2fe72f72321bee1a87d66919b881a0336525a.tar.gz Singularity container built: ubuntu/ Cleaning up… root@singularity:~# ls -l ./ubuntu total 76 drwxr-xr-x 2 root root 4096 Nov 14 21:49 bin drwxr-xr-x 2 root root 4096 Apr 13 2016 boot drwxr-xr-x 2 root root 4096 Nov 14 21:48 dev lrwxrwxrwx 1 root root 36 Dec 4 11:34 environment -> .singularity.d/env/90-environment.sh drwxr-xr-x 42 root root 4096 Nov 18 05:59 etc drwxr-xr-x 2 root root 4096 Apr 13 2016 home drwxr-xr-x 8 root root 4096 Sep 13 2015 lib drwxr-xr-x 2 root root 4096 Nov 14 21:49 lib64 drwxr-xr-x 2 root root 4096 Nov 14 21:48 media drwxr-xr-x 2 root root 4096 Nov 14 21:48 mnt drwxr-xr-x 2 root root 4096 Nov 14 21:48 opt drwxr-xr-x 2 root root 4096 Apr 13 2016 proc drwx------ 2 root root 4096 Nov 14 21:49 root drwxr-xr-x 5 root root 4096 Nov 14 21:49 run drwxr-xr-x 2 root root 4096 Nov 18 05:59 sbin lrwxrwxrwx 1 root root 24 Dec 4 11:34 singularity -> .singularity.d/runscript drwxr-xr-x 2 root root 4096 Nov 14 21:48 srv drwxr-xr-x 2 root root 4096 Feb 5 2016 sys drwxrwxrwt 2 root root 4096 Nov 14 21:49 tmp drwxr-xr-x 10 root root 4096 Nov 14 21:48 usr drwxr-xr-x 11 root root 4096 Nov 14 21:49 var [ build singularity container in writable image ] root@singularity:~# singularity build --writable ubuntu.img docker://ubuntu Building into existing container: ubuntu.img Docker image path: index.docker.io/library/ubuntu:latest Cache folder set to /root/.singularity/docker Importing: base Singularity environment Importing: /root/.singularity/docker/sha256:660c48dd555dcbfdfe19c80a30f557ac57a15f595250e67bfad1e5663c1725bb.tar.gz Importing: /root/.singularity/docker/sha256:4c7380416e7816a5ab1f840482c9c3ca8de58c6f3ee7f95e55ad299abbfe599f.tar.gz Importing: /root/.singularity/docker/sha256:421e436b5f80d876128b74139531693be9b4e59e4f1081c9a3c379c95094e375.tar.gz Importing: /root/.singularity/docker/sha256:e4ce6c3651b3a090bb43688f512f687ea6e3e533132bcbc4a83fb97e7046cea3.tar.gz Importing: /root/.singularity/docker/sha256:be588e74bd348ce48bb7161350f4b9d783c331f37a853a80b0b4abc0a33c569e.tar.gz Importing: /root/.singularity/metadata/sha256:c6a9ef4b9995d615851d7786fbc2fe72f72321bee1a87d66919b881a0336525a.tar.gz Building Singularity image... Singularity container built: ubuntu.img Cleaning up... root@singularity:~# singularity shell --writable ./ubuntu.img Singularity: Invoking an interactive shell within container... Singularity ubuntu.img:~> root@singularity:~# singularity shell ./ubuntu.img Singularity: Invoking an interactive shell within container... Singularity ubuntu.img:~> touch /file2 Singularity ubuntu.img:~> ls -l /file* -rw-r--r-- 1 root root 0 Dec 5 06:05 /file1 -rw-r--r-- 1 root root 0 Dec 5 06:06 /file2 Singularity ubuntu.img:~> exit exit root@singularity:~# singularity exec ./ubuntu.img ls -l /file1 -rw-r--r-- 1 root root 0 Dec 5 06:05 /file1 root@singularity:~# singularity exec ./ubuntu.img ls -l /file2 ls: cannot access '/file2': No such file or directory [ convert container format ] root@singularity:~# singularity build --writable writable-hello-world.img hello-world.simg Building from local image: hello-world.simg Creating empty Singularity writable container 208MB Creating empty 260MiB image file: writable-hello-world.img Formatting image with ext3 file system Image is done: writable-hello-world.img Building Singularity image... Singularity container built: writable-hello-world.img Cleaning up... root@singularity:~# singularity build ubuntu.simg ubuntu Building image from sandbox: ubuntu Building Singularity image... Singularity container built: ubuntu.simg Cleaning up... [ build container from singularity rceipe file ] root@singularity:~# cat ./recipe Bootstrap: shub From: singularityhub/hello-world %runscript exec echo "The runscript is the containers default runtime command!" %files /home/ycheng/party_dinosaur.gif /opt/the-party-dino.gif # %environment VARIABLE=MEATBALLVALUE export VARIABLE %labels AUTHOR inwinstack@inwinstack.com %post apt-get update && apt-get -y install python3 git wget mkdir /data echo "The post section is where you can install, and configure your container." root@singularity:~# singularity build recipe-hello-world.simg recipe Using container recipe deffile: recipe Sanitizing environment Adding base Singularity environment to container Progress |===================================| 100.0% Exporting contents of shub://singularityhub/hello-world to /tmp/.singularity-build.4WKf8n User defined %runscript found! Taking priority. Adding files to container Copying '/home/ycheng/party_dinosaur.gif' to '/opt/the-party-dino.gif' Adding environment to container Running post scriptlet + apt-get update Ign http://archive.ubuntu.com trusty InRelease Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB] Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB] Get:3 http://security.ubuntu.com trusty-security/universe Sources [80.6 kB] Get:4 http://archive.ubuntu.com trusty-backports InRelease [65.9 kB] Get:5 http://security.ubuntu.com trusty-security/main amd64 Packages [859 kB] Hit http://archive.ubuntu.com trusty Release.gpg ….. [ run nginx service in singularity ] root@singularity:~# cat ./startscript %startscript service nginx start root@singularity:~# cat ./recipe-nginx Bootstrap: shub From: singularityhub/hello-world %runscript exec echo "The runscript is the containers default runtime command!" %files /root/startscript /root/startscript %environment VARIABLE=MEATBALLVALUE export VARIABLE %labels AUTHOR inwinstack@inwinstack.com %post apt-get update && apt-get -y install nginx mkdir /data echo "The post section is where you can install, and configure your container." %startscript service nginx start root@singularity:~# singularity build recipe-nginx.simg recipe-nginx Using container recipe deffile: recipe-nginx Sanitizing environment Adding base Singularity environment to container Progress |===================================| 100.0% Exporting contents of shub://singularityhub/hello-world to /tmp/.singularity-build.hMXwK7 User defined %runscript found! Taking priority. Adding files to container Copying '/root/startscript' to '/root/startscript' Adding environment to container Running post scriptlet + apt-get update Ign http://archive.ubuntu.com trusty InRelease Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB] Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB] Get:3 http://security.ubuntu.com trusty-security/universe Sources [80.6 kB] ….. root@singularity:~# ls -l ./recipe-nginx.simg -rwxr-xr-x 1 root root 87392287 Dec 5 15:17 ./recipe-nginx.simg root@singularity:~# singularity instance.start recipe-nginx.simg nginx root@singularity:~# singularity instance.list DAEMON NAME PID CONTAINER IMAGE nginx 7985 /root/recipe-nginx.simg root@singularity:~# ps -elf | grep 7985 5 S root 7985 1 0 80 0 - 3489 sigtim 15:20 ? 00:00:00 singularity-instance: root [nginx] 5 S root 8001 7985 0 80 0 - 21476 sigsus 15:20 ? 00:00:00 nginx: master process /usr/sbin/nginx 0 R root 8047 2136 0 80 0 - 3550 - 15:28 pts/0 00:00:00 grep --color=auto 7985 root@singularity:~# netstat -antlp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8001/nginx: master tcp6 0 0 :::80 :::* LISTEN 8001/nginx: master ! you might use “instance.stop <name of instance>” to stop the singularity instance ```