build
La commande build permet de télécharger des containers existants, de les convertir d'un format à l'autre, ou encore de construire un container à partir d'un fichier recette (recipe).
Nous allons voir premièrement, comment récupérer un container déjà existant et le modifier puis nous verrons comment en construire un à partir d'un fichier recette. Mais d'abord, un petit tour de la commande build avec help :
$ singularity help build
Build a new Singularity container
Usage:
singularity build [local options...]
Description:
IMAGE PATH:
When Singularity builds the container, output can be one of a few formats:
default: The compressed Singularity read only image format (default)
sandbox: This is a read-write container within a directory structure
note: It is a common workflow to use the "sandbox" mode for development of the
container, and then build it as a default Singularity image for production
use. The default format is immutable.
BUILD SPEC:
The build spec target is a definition (def) file, local image, or URI that can
be used to create a Singularity container. Several different local target
formats exist:
def file : This is a recipe for building a container (examples below)
directory: A directory structure containing a (ch)root file system
image: A local image on your machine (will convert to sif if
it is legacy format)
Targets can also be remote and defined by a URI of the following formats:
library:// an image library (default https://cloud.sylabs.io/library)
docker:// a Docker registry (default Docker Hub)
shub:// a Singularity registry (default Singularity Hub)
Options:
--builder string remote Build Service URL (default
"https://build.sylabs.io")
-d, --detached submit build job and print nuild ID (no
real-time logs and requires --remote)
-F, --force delete and overwrite an image if it currently exists
-h, --help help for build
--json interpret build definition as JSON
--library string container Library URL (default
"https://library.sylabs.io")
-T, --notest build without running tests in %test section
-r, --remote build image remotely (does not require root)
-s, --sandbox build image as sandbox format (chroot directory
structure)
--section strings only run specific section(s) of deffile (setup,
post, files, environment, test, labels, none)
(default [all])
-u, --update run definition over existing container (skips header)
[...]
COMMANDS:
Build a sif file from a Singularity recipe file:
$ singularity build /tmp/debian0.sif /path/to/debian.def
Build a sif image from the Library:
$ singularity build /tmp/debian1.sif library://debian:latest
Build a base sandbox from DockerHub, make changes to it, then build sif
$ singularity build --sandbox /tmp/debian docker://debian:latest
$ singularity exec --writable /tmp/debian apt-get install python
$ singularity build /tmp/debian2.sif /tmp/debian
Construire une image depuis une image existante
Contrairement à pull, qui récupère des containers sans possibilité directe de le modifié. La commande build permet de l'importer en mode bac à sable (sandbox).
La différence entre ces deux mode est que le mode writable simple va nous permettre d'écrire directement dans un container singularity, le mode sandbox permet d'avoir accès au container directement depuis le système de fichier, ce qui peut être pratique.
Récupération d'un container simplement (comme pull)
$ sudo singularity build debian_library.sif library://debian:latest
WARNING: Authentication token file not found : Only pulls of public images will succeed
INFO: Starting build...
40.36 MiB / 40.36 MiB [===================================================================================================================================================================] 100.00% 1.99 MiB/s 20s
INFO: Creating SIF file...
INFO: Build complete: debian_library.sif
$ singularity shell debian_library.sif
Singularity debian_library.sif:/data/singularity/build> apt-get update
Reading package lists... Done
E: List directory /var/lib/apt/lists/partial is missing. - Acquire (30: Read-only file system)
On constate que le système est en readonly (comme pour un pull)
Récupération d'un container en mode bac à sable (writable)
$ sudo singularity build --sandbox debian_library library://debian:latest
WARNING: Authentication token file not found : Only pulls of public images will succeed
INFO: Starting build...
40.36 MiB / 40.36 MiB [===================================================================================================================================================================] 100.00% 1.08 MiB/s 37s
INFO: Creating sandbox directory...
INFO: Build complete: debian_library
pour faire des actions, nous pouvons utiliser les commandes exec ou shell.
Exec
Il existe des applications différentes à la commande exec :
Examples:
$ singularity exec /tmp/debian.sif cat /etc/debian_version
$ singularity exec /tmp/debian.sif python ./hello_world.py
$ cat hello_world.py | singularity exec /tmp/debian.sif python
$ sudo singularity exec --writable /tmp/debian.sif apt-get update
$ singularity exec instance://my_instance ps -ef
$ singularity exec library://centos cat /etc/os-release
pour en savoir plus:
$ singularity help exec
cependant nous allons montrer par l'exemple les étapes suivantes :
Mise à jour apt :
$ singularity exec --writable debian_library apt-get update -y
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
on constate qu'il faut avoir les droits root pour modifier l'image (dés qu'il s'agit d'écrire de dans), utilisons sudo:
$ sudo singularity exec --writable debian_library apt-get update
Get:1 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Ign:2 http://cdn-fastly.deb.debian.org/debian stretch InRelease
Get:3 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB]
[...]
Reading package lists... Done
cela fonctionne mieux !
Maintenant installons un package (ex: python3)
$ sudo singularity exec --writable debian_library apt-get install python3
$ singularity help shell
[...]
Description:
singularity shell supports the following formats:
*.sif Singularity Image Format (SIF). Native to Singularity 3.0+
*.sqsh SquashFS format. Native to Singularity 2.4+
*.img ext3 format. Native to Singularity versions < 2.4.
directory/ sandbox format. Directory containing a valid root file
system and optionally Singularity meta-data.
instance://* A local running instance of a container. (See the instance
command group.)
library://* A container hosted on a Library (default
https://cloud.sylabs.io/library)
docker://* A container hosted on Docker Hub
shub://* A container hosted on Singularity Hub
[...]
Examples:
$ singularity shell /tmp/Debian.sif
Singularity/Debian.sif> pwd
/home/gmk/test
Singularity/Debian.sif> exit
$ singularity shell -C /tmp/Debian.sif
Singularity/Debian.sif> pwd
/home/gmk
Singularity/Debian.sif> ls -l
total 0
Singularity/Debian.sif> exit
$ sudo singularity shell -w /tmp/Debian.sif
$ sudo singularity shell --writable /tmp/Debian.sif
$ singularity shell instance://my_instance
$ singularity shell instance://my_instance
Singularity: Invoking an interactive shell within container...
Singularity container:~> ps -ef
UID PID PPID C STIME TTY TIME CMD
ubuntu 1 0 0 20:00 ? 00:00:00 /usr/local/bin/singularity/bin/sinit
ubuntu 2 0 0 20:01 pts/8 00:00:00 /bin/bash --norc
ubuntu 3 2 0 20:02 pts/8 00:00:00 ps -ef
Pas mal de possibilités s'offre à
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
[...]
Testons la commande python3 suite à l'installation du package
$ singularity exec debian_library python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Il n'y a pas '--writable' car nous n'avons pas besoin d'écrire dans le container Singularity, mais juste utilisé une application
Shell
Maintenant utilisons la commande shell, celle-ci permet d'accèder à un shell dans le container singularity.
Pour accèder en shell au container sandbox en écriture:
$ sudo singularity shell --writable debian_library
Singularity debian_library:/data/singularity/build>
C'est tout. Ensuite on peut faire comme si on était dans un shell classique.
Singularity debian_library:~> ifconfig
bash: ifconfig: command not found
Singularity debian_library:~> apt-get install net-tools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
net-tools
[...]
Singularity debian_library:~> ifconfig
Pour plus d'info, la commande help :
$ singularity help shell
[...]
Description:
singularity shell supports the following formats:
*.sif Singularity Image Format (SIF). Native to Singularity 3.0+
*.sqsh SquashFS format. Native to Singularity 2.4+
*.img ext3 format. Native to Singularity versions < 2.4.
directory/ sandbox format. Directory containing a valid root file
system and optionally Singularity meta-data.
instance://* A local running instance of a container. (See the instance
command group.)
library://* A container hosted on a Library (default
https://cloud.sylabs.io/library)
docker://* A container hosted on Docker Hub
shub://* A container hosted on Singularity Hub
[...]
Examples:
$ singularity shell /tmp/Debian.sif
Singularity/Debian.sif> pwd
/home/gmk/test
Singularity/Debian.sif> exit
$ singularity shell -C /tmp/Debian.sif
Singularity/Debian.sif> pwd
/home/gmk
Singularity/Debian.sif> ls -l
total 0
Singularity/Debian.sif> exit
$ sudo singularity shell -w /tmp/Debian.sif
$ sudo singularity shell --writable /tmp/Debian.sif
$ singularity shell instance://my_instance
$ singularity shell instance://my_instance
Singularity: Invoking an interactive shell within container...
Singularity container:~> ps -ef
UID PID PPID C STIME TTY TIME CMD
ubuntu 1 0 0 20:00 ? 00:00:00 /usr/local/bin/singularity/bin/sinit
ubuntu 2 0 0 20:01 pts/8 00:00:00 /bin/bash --norc
ubuntu 3 2 0 20:02 pts/8 00:00:00 ps -ef
Pas mal de possibilités s'offre à vous.