using_singularity.md 9.21 KB
Newer Older
Adam Caprez's avatar
Adam Caprez committed
1
+++
2
title = "Using Singularity and Docker Containers"
Adam Caprez's avatar
Adam Caprez committed
3
4
description = "How to use the Singularity containerization software on HCC resources."
+++
5

6
7
## What is Singularity

npavlovikj's avatar
npavlovikj committed
8
[Singularity](https://www.sylabs.io/singularity/)
9
10
11
12
13
14
15
16
is a containerization solution designed for high-performance computing
cluster environments.  It allows a user on an HPC resource to run an
application using a different operating system than the one provided by
the cluster.  For example, the application may require Ubuntu but the
cluster OS is CentOS.  Conceptually, it is similar to other container
software such as Docker, but is designed with several important
differences that make it more suited for HPC environments.  

Adam Caprez's avatar
Adam Caprez committed
17
18
19
20
- Encapsulation of the environment
- Containers are image based
- No user contextual changes or root escalation allowed
- No root owned daemon processes
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
## Finding Images

Singularity can run images from a variety of sources, including
both a flat image file or a Docker image from Docker Hub.

### Docker Hub

Publically available Docker images can be found at [Docker Hub](http://hub.docker.com). For
convenience, HCC also provides a set of images on [Docker Hub](https://hub.docker.com/u/unlhcc/)
known to work on HCC resources.  


### Available Images at HCC

The following table lists the currently available images and the command
to run the software.

{{< readfile file="static/markdown/singularity-images.md" markdown="true" >}}

{{% notice note %}}
If you would like to request an image to be added, please fill out the
HCC [Software Request Form](http://hcc.unl.edu/software-installation-request)
and indicate you would like to use Singularity.
{{% /notice %}}


## Use images on HCC resources

50
To use Singularity on HCC machines, first load the `singularity `module.
Adam Caprez's avatar
Adam Caprez committed
51
52
Singularity provides a few different ways to access the container.
Most common is to use the `exec` command to run a specific command
53
54
55
56
within the container; alternatively, the `shell` command is used to
launch a bash shell and work interactively.  Both commands take the
source of the image to run as the first argument.  The `exec` command
takes an additional argument for the command within the container to
57
58
59
run. 

Finally, pass any arguments for the program itself in the same manner as you would if running it directly.
60
 For example, the Spades Assembler software is run using the Docker
Adam Caprez's avatar
Adam Caprez committed
61
62
image `unlhcc/spades` and via the command `spades.py`.
To run the software using Singularity, the commands are:
63

Adam Caprez's avatar
Adam Caprez committed
64
65
{{% panel theme="info" header="Run Spades using Singularity" %}}
{{< highlight bash >}}
66
67
module load singularity
singularity exec docker://unlhcc/spades spades.py <spades arguments>
Adam Caprez's avatar
Adam Caprez committed
68
69
{{< /highlight >}}
{{% /panel %}}
70

71
72
73
### Use images within a SLURM job

Using Singularity in a SLURM job is similar to how you would use any other software within a job. Load the module, then execute your image:
74

Adam Caprez's avatar
Adam Caprez committed
75
76
{{% panel theme="info" header="Example Singularity SLURM script" %}}
{{< highlight bash >}}
77
78
79
80
81
82
83
84
85
#!/bin/sh
#SBATCH --time=03:15:00          # Run time in hh:mm:ss
#SBATCH --mem-per-cpu=4096       # Maximum memory required per CPU (in megabytes)
#SBATCH --job-name=singularity-test
#SBATCH --error=/work/[groupname]/[username]/job.%J.err
#SBATCH --output=/work/[groupname]/[username]/job.%J.out

module load singularity
singularity exec docker://unlhcc/spades spades.py <spades arguments>
Adam Caprez's avatar
Adam Caprez committed
86
87
{{< /highlight >}}
{{% /panel %}}
88

89
## Create a custom image
90

91
92
Custom images can be created locally on your personal machine and added to Docker Hub for use
on HCC clusters. More information on creating custom Docker images can be found in the [Docker documentation](https://docs.docker.com/develop/develop-images/baseimages/).
93

94
95
96
97
98
You can create custom Docker image and use it with Singularity on our clusters.
Singularity can run images directly from Docker Hub, so you don't need to upload anything to HCC.
For this purpose, you just need to have a Docker Hub account and upload
your image there. Then, if you want to run the command "*mycommand*"
from the image "*myimage*", type:
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{{< highlight bash >}}
module load singularity
singularity exec docker://myaccount/myimage mycommand
{{< /highlight >}}

where "*myaccount*" is your Docker Hub account.

In case you see the error `ERROR MANIFEST_INVALID: manifest invalid`
when running the command above, try:

{{< highlight bash >}}
module load singularity
unset REGISTRY
singularity exec docker://myaccount/myimage mycommand
{{< /highlight >}}
115

Adam Caprez's avatar
Adam Caprez committed
116
{{% notice info %}}
117
If you get the error `FATAL: kernel too old` when using your Singularity image on the HCC clusters, that means the *glibc* version in your image is too new for the kernel on the cluster. One way to solve this is to use lower version of your base image (for example, if you have used Ubuntu:18.04 please use Ubuntu:16.04 instead).
Adam Caprez's avatar
Adam Caprez committed
118
{{% /notice %}}
119

120
121
122
123
124
125
126
127
128
129
130

All the Dockerfiles of the images we host on HCC are
[publicly available here](https://github.com/unlhcc/singularity-dockerfiles).
You can use them as an example when creating your own image.

### Add packages to an existing image

Alternatively, instead of building an image from scratch, you can start with an HCC-provided
image as the base for your Dockerfile (i.e. `FROM unlhcc/spades`)
and add any additional packages you desire.

131
132
133
134
135
136
137
138
139
140
141
142
Unfortunately it's not possible to create one image that has every
available Python package installed for logistical reasons.  Images are
created with a small set of the most commonly-used scientific packages,
but you may need others.  If so, you can install them in a location in
your `$WORK` directory and set the `PYTHONPATH` variable to that
location in your submit script.  The extra packages will then be "seen"
by the Python interpreter within the image.  To ensure the packages will
work, the install must be done from within the container via
the `singularity shell` command.  For example, suppose you are using
the `tensorflow-gpu` image and need the packages `nibabel` and `tables`.
 First, run an interactive SLURM job to get a shell on a worker node.

Adam Caprez's avatar
Adam Caprez committed
143
144
{{% panel theme="info" header="Run an interactive SLURM job" %}}
{{< highlight bash >}}
145
srun --pty --mem=4gb --qos=short --gres=gpu --partition=gpu $SHELL
Adam Caprez's avatar
Adam Caprez committed
146
147
{{< /highlight >}}
{{% /panel %}}
148

149
150
151
152
153
154
155
{{% notice info %}}
The `--gres=gpu --partition=gpu` options are used here as the `tensorflow-gpu` image is
GPU enabled.  If you are using a non-GPU image, you may omit those options.  See the page
on [submitting GPU jobs]({{< relref "/guides/submitting_jobs/submitting_cuda_or_openacc_jobs/_index.md" >}})
for more information.
{{% /notice %}}

156
157
158
After the job starts, the prompt will change to indicate you're on a
worker node.  Next, start an interactive session in the container.

Adam Caprez's avatar
Adam Caprez committed
159
160
{{% panel theme="info" header="Start a shell in the container" %}}
{{< highlight bash >}}
161
162
module load singularity
singularity shell docker://unlhcc/tensorflow-gpu
Adam Caprez's avatar
Adam Caprez committed
163
164
{{< /highlight >}}
{{% /panel %}}
165
166
167
168
169
170
171
172

This may take a few minutes to start.  Again, the prompt will change and
begin with `Singularity` to indicate you're within the container.

Next, install the needed packages via `pip` to a location somewhere in
your `work` directory.  For example, `$WORK/tf-gpu-pkgs`.  (If you are
using Python 3, use `pip3` instead of `pip`).

Adam Caprez's avatar
Adam Caprez committed
173
174
{{% panel theme="info" header="Install needed Python packages with pip" %}}
{{< highlight bash >}}
175
176
export LC_ALL=C
pip install --system --target=$WORK/tf-gpu-pkgs --install-option="--install-scripts=$WORK/tf-gpu-pkgs/bin" nibabel tables
Adam Caprez's avatar
Adam Caprez committed
177
178
{{< /highlight >}}
{{% /panel %}}
179
180
181
182
183
184
185
186
187
188
189
190

You should see some progress indicators, and a
"`Successfully installed..."` message at the end.  Exit both the
container and the interactive SLURM job by typing `exit` twice.  The
above steps only need to be done once per each image you need additional
packages for.   Be sure to use a separate location for each image's
extra packages.

To make the packages visible within the container, you'll need to add a
line to the submit script used for your Singularity job.  Before the
lines to load the `singularity `module and run the script, add a line
setting the `PYTHONPATH` variable to the `$WORK/tf-gpu-pkgs` directory.
Adam Caprez's avatar
Adam Caprez committed
191
For example,
192

Adam Caprez's avatar
Adam Caprez committed
193
194
{{% panel theme="info" header="Example SLURM script" %}}
{{< highlight bash >}}
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/sh
#SBATCH --time=03:15:00          # Run time in hh:mm:ss
#SBATCH --mem-per-cpu=4096       # Maximum memory required per CPU (in megabytes)
#SBATCH --job-name=singularity-test
#SBATCH --partition=gpu
#SBATCH --gres=gpu
#SBATCH --error=/work/[groupname]/[username]/job.%J.err
#SBATCH --output=/work/[groupname]/[username]/job.%J.out
 
export PYTHONPATH=$WORK/tf-gpu-pkgs
module load singularity
singularity exec docker://unlhcc/tensorflow-gpu python /path/to/my_tf_code.py
Adam Caprez's avatar
Adam Caprez committed
207
208
{{< /highlight >}}
{{% /panel %}}
209
210
211
212
213
214
215
216
217
218
219

The additional packages should then be available for use by your Python
code running within the container.

### What if I need a specific software version of the Singularity image?

You can see all the available versions of the software built with
Singularity in the table above. If you don't specify a specific sofware
version, Singulariy will use the latest one. If you want to use a
specific version instead, you can append the version number from the
table to the image. For example, if you want to use the Singularity
Adam Caprez's avatar
Adam Caprez committed
220
image for Spades version 3.11.0, run:
221

Adam Caprez's avatar
Adam Caprez committed
222
{{< highlight bash >}}
223
singularity exec docker://unlhcc/spades:3.11.0 spades.py
Adam Caprez's avatar
Adam Caprez committed
224
{{< /highlight >}}