Skip to content

Commit 710e561

Browse files
committed
$SINGULARITY_ENVIRONMENT, --nv, and random cleanup
1 parent 4d29381 commit 710e561

File tree

11 files changed

+441
-61
lines changed

11 files changed

+441
-61
lines changed

_posts/recipes/2017-05-09-hostlibs-gpus-and-mpi.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ category: recipes
44
permalink: tutorial-gpu-drivers-open-mpi-mtls
55
---
66

7+
**Note: _Much of the GPU portion of this tutorial is deprecated by the `--nv`
8+
option that automatically binds host system driver libraries into your
9+
container at runtime. See [the `exec` command](/docs-exec#a-gpu-example) for
10+
an example_**
11+
712
Singularity does a fantastic job of isolating you from the host so you don't
813
have to muck about with `LD_LIBRARY_PATH`, you just get exactly the library
914
versions you want. However, in some situations you need to use library

pages/docs/contributing/contributing-docs.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ title: Contributing to Documentation
33
sidebar: main_sidebar
44
permalink: contributing-docs
55
folder: releases
6+
toc: false
67
---
78

89
We (like almost all open source software providers) have a documentation dillemma... We tend to focus on the code features and functionality before working on documentation. And there is very good reason for this, we want to share the love so nobody feels left out!
910

1011
The following documentation page assumes one is running on OS X, but if you are not, you should be able to easily transpose the necessary commands to your operating system of choice.
1112

13+
{% include toc.html %}
1214

1315
## Setting Up Your Development Environment
1416

@@ -113,8 +115,10 @@ asciinema rec -w 1 demo-asciicast.js
113115
# To play
114116
asciinema play demo-asciicast.js
115117
```
118+
Make sure to resize your terminal to 25 rows x 115 columns or less before you
119+
begin your recording. Otherwise it will not display properly on the webpage.
116120

117-
Once you've generated an asciicast, you should drop the file (e.g., some `demo-asciicast.js`) into the `assets/asciicast` folder, and then include the following in the page or post:
121+
Once you've generated an asciicast, you should drop the file (e.g., `demo-asciicast.js`) into the `assets/asciicast` folder, and then include the following in the page or post:
118122

119123
```bash
120124
{{ "{% include asciicast.html source='demo-asciicast.js' title='How to make demos' author='email@domain.com'" }}%}

pages/docs/overview/faq.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ See the Singularity on HPC page for more details.
213213

214214
### Does Singularity support containers that require GPUs?
215215

216-
Yes, Singularity has been tested to run some test and diagnostic code from within a container without modification. There are however potential issues that can come into play when using GPUs, for instance there are version API compatibilities between kernel and user land which will have to be considered.
216+
Yes. Many users run GPU dependant code within Singularity containers. The
217+
experimental `--nv` option allows you to leverage host GPUs without installing
218+
system level drivers into your container. See [the `exec` command](/docs-exec#a-gpu-example) for
219+
an example.
217220

218221
## Container portability
219222

pages/docs/user-docs/docs-bootstrap-image.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ title: Getting Started with Bootstrap
33
sidebar: user_docs
44
permalink: bootstrap-image
55
folder: docs
6+
toc: false
67
---
78

8-
## Bootstrapping a Container
99
Bootstrapping is the process where we install an operating system and then configure it appropriately for a specified need. To do this we use a bootstrap definition file (a text file called `Singularity`) which is a recipe of how to specifically build the container. Here we will overview the sections, best practices, and a quick example.
1010

1111
{% include toc.html %}
@@ -142,7 +142,25 @@ Version 2.0
142142
```
143143

144144
#### %environment
145-
Akin to labels, you can add pairs of `VARIABLE` and `VALUE` under environment to be sourced when the container is used as variables in the environment. The entire section is written to a file that gets sourced, so you should generally use the same conventions that you might use in a `bashrc` or `profile`. See <a href="/docs-environment-metadata">Environment and Metadata</a> for more information about these two sections.
145+
You can add environment variables to be sourced when the container is used in the `%environment` section. The entire section is written to a file that gets sourced, so you should generally use the same conventions that you might use in a `bashrc` or `profile`.
146+
147+
```
148+
%environment
149+
export VADER=badguy
150+
export LUKE=goodguy
151+
export SOLO=someguy
152+
```
153+
154+
You can also add environment variables to your container in the `%post` section (see below) using the following syntax:
155+
156+
```
157+
%post
158+
echo 'export JAWA_SEZ=wutini' >>$SINGULARITY_ENVIRONMENT
159+
```
160+
161+
Variables added to your container using the `$SINGULARITY_ENVIRONMENT` syntax take precedence over those added in the `%environment` section.
162+
163+
See <a href="/docs-environment-metadata">Environment and Metadata</a> for more information about the `%labels` and `%environment` sections.
146164

147165

148166
#### %post

pages/docs/user-docs/docs-bootstrap.md

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,90 @@ toc: false
66
folder: docs
77
---
88

9+
The process of *bootstrapping* a Singularity container is equivalent to describing a recipe for the container creation. There are several recipe formats that Singularity supports, but only the primary format of version 2.3 will be documented here. If you want a general overview with examples, see <a href="/bootstrap-image">Bootstrapping an Image</a>. The detailed options for each of the header and sections are provided here.
10+
911
{% include toc.html %}
1012

11-
The process of *bootstrapping* a Singularity container is equivalent to describing a recipe for the container creation. There are several recipe formats that Singularity supports, but only the primary format of version 2.3 will be documented here. If you want a general overview with examples, see <a href="/bootstrap-image">Bootstrapping an Image</a>. The detailed options for each of the header and sections are provided here.
13+
## Usage
14+
15+
```
16+
USAGE: singularity [...] bootstrap <container path> <definition file>
17+
18+
The bootstrap command is useful for creating a new bootstrap or modifying an
19+
existing one using a definition file that describes how to build the
20+
container. The quickest, easiest Bootstrap is dumping DOcker layers into
21+
a container:
22+
23+
BootStrap: docker
24+
From: library/ubuntu:14.04.1
25+
26+
For more documentation on definition file recipes, look below.
27+
28+
note: This command must be executed as root.
29+
30+
31+
32+
CREATE OPTIONS:
33+
-T|--notest Bootstrap without running tests in %test section
34+
-f|--force Force a rebootstrap of an OS (note: this does not delete
35+
what is currently in the image, just causes the core to
36+
be reinstalled)
37+
-s|--section Only run a given section within the bootstrap (setup,
38+
post, files, environment, test, labels, none)
39+
--nobase Skip the generation of the base OS
40+
41+
42+
DEFFILE BASEOS EXAMPLES:
43+
44+
Docker:
45+
BootStrap: docker
46+
From: tensorflow/tensorflow:latest
47+
IncludeCmd: yes #Include the CMD/ENTRYPOINT as the Singularity Runscript
48+
49+
YUM/RHEL:
50+
BootStrap: yum
51+
OSVersion: 7
52+
MirrorURL: http://mirror.centos.org/centos-%{OSVERSION}/%{OSVERSION}/os/$basearch/
53+
Include: yum #This is important to include inside the base container
54+
55+
Debian/Ubuntu:
56+
BootStrap: debootstrap
57+
OSVersion: trusty
58+
MirrorURL: http://us.archive.ubuntu.com/ubuntu/
59+
60+
61+
DEFFILE SECTION EXAMPLES:
62+
63+
%setup
64+
echo "This is a scriptlet that will be executed on the host, as root, after"
65+
echo "the container has been bootstrapped. To install things into the container"
66+
echo "reference the file system location with \$SINGULARITY_BUILDROOT"
67+
68+
%post
69+
echo "This scriptlet section will be executed from within the container after"
70+
echo "the bootstrap/base has been created and setup"
71+
72+
%test
73+
echo "Define any test commands that should be executed after container has been"
74+
echo "built. This scriptlet will be executed from within the running container"
75+
echo "as the root user. Pay attention to the exit/return value of this scriptlet"
76+
echo "as any non-zero exit code will be assumed as failure"
77+
exit 0
78+
79+
%labels
80+
HELLO MOTO
81+
KEY VALUE
82+
83+
%files
84+
/path/on/host/file.txt /path/on/container/file.txt
85+
relative_file.txt /path/on/container/relative_file.txt
86+
87+
88+
EXAMPLES:
89+
90+
$ singularity create /tmp/Debian.img
91+
$ sudo singularity bootstrap /tmp/Debian.img debian.def
92+
```
1293

1394
## The header fields:
1495

@@ -48,7 +129,7 @@ The Docker bootstrap module will create a core operating system image based on a
48129
Once the `Bootstrap` module has completed, the sections are identified and utilized if present. The following sections are supported in the bootstrap definition, and integrated during the bootstrap process:
49130

50131

51-
#### %setup
132+
### %setup
52133
This section blob is a Bourne shell scriptlet which will be executed on the host outside the container during bootstrap. The path to the container is accessible from within the running scriptlet environment via the variable `$SINGULARITY_ROOTFS`. For example, consider the following scriptlet:
53134

54135
```
@@ -65,8 +146,7 @@ As we investigate this example scriptlet, you will first see this is the outside
65146

66147
*note: Any uncaught command errors that occur within the scriptlet will cause the entire build process to halt!*
67148

68-
69-
#### %post
149+
### %post
70150
Similar to the `%setup` section, this section will be executed once during bootstrapping, but this scriptlet will be run from inside the container. This is where you should put additional installation commands, downloads, and configuration into your containers. Here is an example to consider:
71151

72152
```
@@ -94,8 +174,26 @@ The above example runs inside the container, so in this case we will first insta
94174

95175
*another note: This is not a good example of a reproducible definition because it is pulling Open MPI from a moving target. A better example, would be to pull a static released version, but this serves as a good example of building a `%post` scriptlet.*
96176

177+
### %environment
178+
Beginning with Singularity v2.3 you can set up your environment using the `%environment` section of the definition file. For example, if you wanted to add a directory to your search path, you could do so like this.
179+
180+
```
181+
%environment
182+
export PATH=/opt/good/stuff:$PATH
183+
```
184+
185+
In older versions of Singularity you could set up your container's environment by adding text to a file called `/environment`. This method is now deprecated. If you need to add environment variables to your container during the `%post` section (perhaps because you will not know the values of some variables until some installation steps have completed). You can do so with the following syntax:
186+
187+
```
188+
%post
189+
echo 'export PATH=/opt/good/stuff:$PATH' >>$SINGULARITY_ENVIRONMENT
190+
```
191+
192+
Text in the `%environment` section will be appended to a file called `/.singularity.d/env/90-environment.sh`. Text redirected to the `$SINGULARITY_ENVIRONMENT` variable will added to a file called `/.singularity.d/env/91-environment.sh`. At runtime, scripts in `/.singularity/env` are sourced in order.
193+
194+
This means that variables in `$SINGULARITY_ENVIRONMENT` take precedence over those added via `%environment`.
97195

98-
#### %runscript
196+
### %runscript
99197
The `%runscript` is another scriptlet, but it does not get executed during bootstrapping. Instead it gets persisted within the container to a file called `/singularity` which is the execution driver when the container image is ***run*** (either via the `singularity run` command or via executing the container directly).
100198

101199
When the `%runscript` is executed, all options are passed along to the executing script at runtime, this means that you can (and should) manage argument processing from within your runscript. Here is an example of how to do that:
@@ -108,7 +206,7 @@ When the `%runscript` is executed, all options are passed along to the executing
108206

109207
In this particular runscript, the arguments are printed as a single string (`$*`) and then they are passed to `/usr/bin/python` via a quoted array (`$@`) which ensures that all of the arguments are properly parsed by the executed command. The `exec` command causes the given command to replace the current entry in the process table with the one that is to be called. This makes it so the runscript shell process ceases to exist, and the only process running inside this container is the called Python command.
110208

111-
#### %test
209+
### %test
112210
You may choose to add a `%test` section to your definition file. This section will be run at the very end of the boostrapping process and will give you a chance to validate the container during the bootstrap process. If you are building on Singularity Hub, [it is a good practice](https://github.com/singularityhub/singularityhub.github.io/wiki/Generate-Images#add-tests) to have this test section so you can be sure that your container works as expected. A non-zero status code indicates that one or more of your tests did not pass. You can also execute this scriptlet through the container itself, such that you can always test the validity of the container itself as you transport it to different hosts. Extending on the above Open MPI `%post`, consider this example:
113211

114212
```

pages/docs/user-docs/docs-environment-metadata.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
---
2-
title: Changing Existing Containers
2+
title: Environment and Metadata
33
sidebar: user_docs
44
permalink: docs-environment-metadata
55
folder: docs
6+
toc: false
67
---
78

8-
## Container Metadata
9+
Singularity containers support environment variables and labels that can be added by user during the bootstrap process.
910

10-
Singularity containers have two level of metadata - environment variables, and labels from the user and bootstrap process.
11+
{% include toc.html %}
1112

12-
### Environment
13+
## Environment
1314

1415
If you are importing a Docker container, the environment will be imported as well. If you want to define custom environment variables in your bootstrap recipe file `Singularity` you can do that like this
1516

@@ -18,10 +19,19 @@ Bootstrap:docker
1819
From: ubuntu:latest
1920

2021
%environment
21-
VARIABLE_NAME=VARIABLE_VALUE
22-
export VARIABLE_NAME
22+
VARIABLE_NAME=VARIABLE_VALUE
23+
export VARIABLE_NAME
2324
```
2425

26+
If you need to add an environment variable to your container during the `%post` section, you can do so using the `$SINGULARITY_ENVIRONMENT` variable with the following syntax:
27+
28+
```
29+
%post
30+
echo 'export VARIABLE_NAME=VARIABLE_VALUE' >>$SINGULARITY_ENVIRONMENT
31+
```
32+
33+
Text in the `%environment` section will be appended to the file `/.singularity.d/env/90-environment.sh` while text redirected to `$SINGULARITY_ENVIRONMENT` will end up in the file `/.singularity.d/env/91-environment.sh`. Because files in `/.singularity.d/env` are sourced in alpha-numerical order, this means that variables added using `$SINGULARITY_ENVIRONMENT` take precedence over those added via the `%environment` section.
34+
2535
Forget something, or need a variable defined at runtime? You can set any variable you want inside the container by prefixing it with "SINGULARITYENV_". It will be transposed automatically and the prefix will be stripped. For example, let's say we want to set the variable `HELLO` to have value `WORLD`. We can do that as follows:
2636

2737
```bash
@@ -58,7 +68,7 @@ singularity exec centos7.img cat /.singularity.d/env/10-docker.sh
5868
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
5969
```
6070

61-
### Labels
71+
## Labels
6272
Your container stores metadata about it's build, along with Docker labels. You can see the data as follows:
6373

6474
```bash

0 commit comments

Comments
 (0)