Skip to content

Commit 9924dfa

Browse files
committed
simplifiying the runscript section
1 parent 5d8de7c commit 9924dfa

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

02-building/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ OSVersion: stable
197197
MirrorURL: http://ftp.us.debian.org/debian/
198198
199199
%runscript
200-
fortune | cowsay | lolcat
200+
echo "This is what happens when you run the container..."
201201
202202
%post
203203
apt-get update

05-runscript/README.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
# The Runscript: Making containerized apps behave more like normal apps
1+
# The Runscript: Making containerized apps behave like normal apps
22

3-
We are now going to consider an extended example describing a containerized application that takes a file as input, analyzes the data in the file, and produces another file as output.
4-
This is obviously a very common situation.
3+
Consider an application that takes one file as input, analyzes the data in the file, and produces another file as output. This is obviously a very common situation.
54

6-
Let's imagine that we want to use the cowsay program in our `lolcow.simg` to "analyze data". We should give our container an input file, it should reformat it (in the form of a cow speaking), and it should dump the output into another file.
5+
Let's imagine that we want to use the cowsay program in our `lolcow.sif` to "analyze data". We should give our container an input file, it should reformat the text (in the form of a cow speaking), and it should dump the output into another file.
76

87
Here's an example. First I'll make some "data"
98

109
```
11-
$ echo "The grass is always greener over the septic tank" > input
10+
$ echo "The grass is always greener over the septic tank" > /data/input
1211
```
1312

1413
Now I'll "analyze" the "data"
1514

1615
```
17-
$ cat input | singularity exec lolcow.simg cowsay > output
16+
$ cat /data/input | singularity exec lolcow.sif cowsay >/data/output
1817
```
1918

20-
The "analyzed data" is saved in a file called `output`.
19+
The "analyzed data" is saved in a file called `/data/output`.
2120

2221
```
23-
$ cat output
22+
$ cat /data/output
2423
______________________________________
2524
/ The grass is always greener over the \
2625
\ septic tank /
@@ -41,77 +40,55 @@ $ ./lolcow.simg
4140
This is what happens when you run the container...
4241
```
4342

44-
Let's rewrite this runscript in the definition file and rebuild our container
45-
so that it does something more useful.
43+
Let's rewrite this runscript in the definition file and rebuild our container so that it does something more useful. And while we're at it, we'll change the bootstrap method to `library`
4644

4745
```
48-
BootStrap: debootstrap
49-
OSVersion: stable
50-
MirrorURL: http://ftp.us.debian.org/debian/
46+
BootStrap: library
47+
From: debian:9
5148
5249
%runscript
53-
infile=
54-
outfile=
55-
56-
usage() {
57-
>&2 echo "Usage:"
58-
>&2 echo "$SINGULARITY_NAME -i <infile> -o <outfile> [ -- <cowsay options> ]"
50+
if [ $# -ne 2 ]; then
51+
echo "Please provide an input and an output file."
5952
exit 1
60-
}
61-
62-
while getopts i:o: argument
63-
do
64-
case $argument in
65-
i)
66-
infile="$OPTARG"
67-
;;
68-
o)
69-
outfile="$OPTARG"
70-
;;
71-
?)
72-
usage
73-
;;
74-
esac
75-
done
76-
77-
shift "$((OPTIND - 1))"
78-
79-
if [ -z "$infile" ] || [ -z "$outfile" ]
80-
then
81-
usage
8253
fi
83-
84-
cat "$infile" | cowsay "$@" > "$outfile"
54+
cat $1 | cowsay > $2
8555
8656
%post
87-
echo "Hello from inside the container"
8857
apt-get update
8958
apt-get -y install fortune cowsay lolcat
90-
apt-get clean
9159
9260
%environment
9361
export PATH=$PATH:/usr/games
94-
export LC_ALL=C
9562
```
9663

9764
Now we must rebuild out container to install the new runscript.
9865

9966
```
100-
$ sudo singularity build --force lolcow.simg Singularity
67+
$ sudo singularity build --force lolcow.sif lolcow.def
10168
```
10269

10370
Note the `--force` option which ensures our previous container is completely overwritten.
10471

105-
After rebuilding our container, we can call the lolcow.simg as though it were an executable, give it input and output file names, and optionally give additional arguments to go directly to the `cowsay` program.
72+
After rebuilding our container, we can call the `lolcow.sif` as though it were an executable, give it input and output file names.
10673

10774
```
108-
$ ./lolcow.simg
109-
Usage:
110-
lolcow.simg -i <infile> -o <outfile> [ -- <cowsay options> ]
75+
$ ./lolcow.sif /data/input /data/output2
76+
/.singularity.d/runscript: 7: /.singularity.d/runscript: cannot create /data/output2: Directory nonexistent
77+
cat: /data/input: No such file or directory
78+
```
79+
80+
Whoops!
11181

112-
$ ./lolcow.simg -i input -o output2
82+
We are no longer piping redirecting standard output into and out of the container, so we need to bind mount the `/data` directory into the container. It will be convenient to simply set the bind path as an environment variable.
11383

114-
$ cat output2
84+
```
85+
$ export SINGULARITY_BINDPATH=/data
86+
87+
$ ./lolcow.sif /data/input /data/output2
88+
89+
$ ./lolcow.sif /data/vader.txt /data/output3
90+
91+
$ cat /data/output2 /data/output3
11592
______________________________________
11693
/ The grass is always greener over the \
11794
\ septic tank /
@@ -121,4 +98,27 @@ $ cat output2
12198
(__)\ )\/\
12299
||----w |
123100
|| ||
101+
__________________
102+
< I am your father >
103+
------------------
104+
\ ^__^
105+
\ (oo)\_______
106+
(__)\ )\/\
107+
||----w |
108+
|| ||
124109
```
110+
111+
To summarize, we have written a runscript for our container that will do some very basic error checking and expects the location of an input file and an output file allowing it to analyze the data. This is obviously a trivial example, but the sky is the limit. If you can code it, you can make your container do it!
112+
113+
---
114+
**BONUS**
115+
116+
You will often see this or something similar as a containers runscript.
117+
118+
```
119+
%runscript
120+
python "$@"
121+
```
122+
What does this do?
123+
124+
---

0 commit comments

Comments
 (0)