You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New to Singularity 2.4 is the ability to clone your image, meaning you create an instance of it that has its own namespace. Why would you want to do this? It means that your container can be instantiated and then serve a process that your computer has control of.
9
+
Singularity 2.4 introduces the ability to run "container instances", allowing you to run services (*e.g. Nginx, MySQL, etc...*) using Singularity. A container instance, simply put, is a persistant and isolated version of the container image that runs in the background.
10
10
11
11
## Why container instances?
12
12
Let's say I want to run a web server. With nginx, that is pretty simple, I install nginx and start the service:
@@ -23,85 +23,198 @@ With older versions of Singularity, if you were to do something like this, from
23
23
You would lose control of the process. It would still be running, but you couldn't kill it. This is a called a ghost process, and it means that for running (enduring) services, Singularity was a no starter.
24
24
25
25
26
-
## Cloning containers
27
-
With version 2.4, you can do this in a more realistic way. First, let's put the commands of how to start our service into a script. Let's call it a `startscript`. And we can imagine this fitting into a bootstrap recipe file like this:
26
+
## Container Instances in Singularity
27
+
With Singularity 2.4 and the addition of container instances, the ability to cleanly, reliably, and safely run services in a container is here. First, let's put the commands of how to start our service into a script. Let's call it a `startscript`. And we can imagine this fitting into a build definition file like this:
28
28
29
29
```
30
30
%startscript
31
31
32
32
service nginx start
33
33
```
34
34
35
-
and an instruction to stop it too:
35
+
Now let's say we build a container with that startscript into an image called `nginx.img`and we want to run an nginx service. All we need to do is start the instance and the startscript will get run inside the container automatically:
36
36
37
+
```
38
+
[command] [image] [name of instance]
39
+
$ singularity instance.start nginx.img web
40
+
```
41
+
42
+
When we run that command, Singularity creates an isolated environment for the container instances' processes/services to live inside. We can confirm that this command started an instance by running the following command:
43
+
44
+
```
45
+
$ singularity instance.list
46
+
INSTANCE NAME PID CONTAINER IMAGE
47
+
web 790 /home/mibauer/nginx.img
48
+
```
49
+
50
+
If we want to run multiple instances from the same image, it's as simple as running the command multiple times. The instance names are an identifier used to uniquely describe an instance, so they cannot be repeated.
51
+
52
+
```
53
+
$ singularity instance.start nginx.img web1
54
+
$ singularity instance.start nginx.img web2
55
+
$ singularity instance.start nginx.img web3
56
+
```
57
+
58
+
And again to confirm that the instances are running as we expected:
59
+
60
+
```
61
+
$ singularity instance.list
62
+
INSTANCE NAME PID CONTAINER IMAGE
63
+
web1 790 /home/mibauer/nginx.img
64
+
web2 791 /home/mibauer/nginx.img
65
+
web3 792 /home/mibauer/nginx.img
66
+
```
67
+
68
+
Once an instance is started, the environment inside of that instance will never change. If the service you want to run in your instance requires a bind mount, then you must pass the `-B` option when calling `instance.start`. For example, if you wish to capture the output of the `web1` container instance which is placed at `/output/` inside the container you could do:
In this section, we will demonstrate an example of packaging a service into a container and running it. The service we will be packaging is an API server that converts a web page into a PDF, and can be found [here](https://github.com/alvarcarto/url-to-pdf-api). The final example can be found [here on GitHub](https://github.com/bauerm97/instance-example), and [here on SingularityHub](link-to-shub). If you wish to just download the final image directly from Singularity Hub, simply run `singularity pull shub://bauerm97/instance-example`.
77
+
78
+
### Building the Image
79
+
80
+
To begin, we need to build the image. When looking at the GitHub page of the `url-to-pdf-api`, we can see that it is a Node 8 server that uses headless Chromium called [Puppeteer](https://github.com/GoogleChrome/puppeteer). Let's first choose a base from which to build our container, in this case I used the docker image `node:8` which comes pre-installed with Node 8:
81
+
82
+
```
83
+
Bootstrap: docker
84
+
From: node:8
85
+
Includecmd: no
86
+
```
87
+
88
+
Puppeteer also requires a few dependencies to be manually installed in addition to Node 8, so we can add those into the `post` section as well as the installation script for the `url-to-pdf-api`:
And now we need to define what happens when we start an instance of the container. In this situation, we want to run the commands that starts up the url-to-pdf-api server:
37
108
38
109
```
39
110
%startscript
111
+
cd /scif/apps/pdf_server/pdf_server
112
+
# Use nohup and /dev/null to completely detach server process from terminal
113
+
nohup npm start > /dev/null 2>&1 < /dev/null &
114
+
```
115
+
116
+
Also, the `url-to-pdf-api` server requires some environment variables be set, which we can do in the `environment` section:
40
117
41
-
service nginx stop
118
+
```
119
+
%environment
120
+
export NODE_ENV=development
121
+
export PORT=8000
122
+
export ALLOW_HTTP=true
123
+
export URL=localhost
42
124
```
43
125
44
-
You might even have some special (longer set) of commands in your startscript, if warranted:
126
+
Now we can build the definition file into an image! Simply run build and the image will be ready to go:
echo "server is already running. Use restart or stop."
54
-
fi
134
+
Now that we have an image, we are ready to start an instance and run the server:
55
135
56
-
if [ -z "$OMGTACOSCELERY" ]; then
57
-
/bin/bash /code/helpers/ctrl/celery.screen
58
-
echo "worker started, status code $?"
59
-
else
60
-
echo "worker is already running. Use restart or stop."
61
-
fi
136
+
```
137
+
$ singularity instance.start url-to-pdf-api.img pdf
62
138
```
63
139
64
-
In the above example, there are two services in my container, and based on environment varibles, there is some custom functionality that happens based on how the user sets them upon starting the container instance.
140
+
We can confirm it's working by sending the server an http request using curl:
65
141
66
-
Now let's say we have a container called `nginx.img` and we want to run a service in it. What do we do? Well, first we clone it to make an instance:
When I do that, I still have my file `nginx.img` sitting on my Desktop, but now you can think about having actually an instance of it running, which I can now control! Heck, I could do that multiple times, if it made sense for my service:
170
+
### Making it Pretty
171
+
172
+
Now that we have comfirmation that the server is working, let's make it a little cleaner. It's reallying annoying to have to remember the exact curl comand and URL syntax each time you want to request a PDF, so let's automate that. To do that, we're going to be using apps. If you haven't already, check out the [Singularity app documentation](link-to-app-docs-or-scif) to come up to speed.
173
+
174
+
First off, we're going to move the installation of the `url-to-pdf-api` into an app, so that there is a designated spot to place output files. To do that, we want to add a section to our definition file to build the server:
Once you create this instance, you can't do additional things like binds. So if your service requires a special mount or any other kind of connection, do that at the time of the clone:
184
+
Now we want to define the pdf_client app, which we will run to send the requests to the server:
Once you have generated instances, you can start them up! You do that with start, directed to the instance name:
195
+
As you can see, the `pdf_client` app checks to make sure that the user provides at least one argument. Now that we have an output directory in the container, we need to expose it to the host using a bind mount. Once we've rebuilt the container, make a new directory callout `out` for the generated PDF's to go. Now we simply start the instance like so:
89
196
90
197
```
91
-
singularity start nginx.img instance1
198
+
$ singularity instance.start -B out/:/scif/data/pdf_client/output/ url-to-pdf-api.img pdf
92
199
```
93
200
94
-
## Listing Services
95
-
You can then easily list services:
201
+
And to request a pdf simply do:
96
202
97
203
```
98
-
singularity list
204
+
$ singularity run --app pdf_client instance://pdf http://google.com google.pdf
99
205
```
100
206
207
+
And to confirm that it works:
208
+
209
+
```
210
+
$ ls out/
211
+
google.pdf
212
+
```
213
+
214
+
101
215
## Important Notes
102
216
103
-
- The instances are linked with your user. So if you clone and start with sudo, that is going to go under root, and you will be confused to call `singularity list` as your user and then not see your services.
104
-
- The only reason to specify the image is because it could be the case that you have two different images with services named equally.
217
+
- The instances are linked with your user. So if you start an instance with sudo, that is going to go under root, and you will need to call `sudo singularity instance.list` in order to see it.
105
218
106
219
107
220
This stuff is completely under development and likely to change! <ahref="https://github.com/singularityware/singularity/issues"target="_blank"> Join the conversation!</a>.
0 commit comments