Skip to content

Commit 981ab73

Browse files
authored
Added 'Run a container example' to open-ondemand/apps/job-composer.md (#282)
Added 'Run a container example' to open-ondemand/apps/job-composer.md
1 parent 5ded32b commit 981ab73

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

docs/safe-haven-services/open-ondemand/apps/job-composer.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,177 @@ If you selected a back-end where your home directory is not common to both the O
207207
```
208208
Hello World to someuser from some-vm.nsh.loc
209209
```
210+
211+
---
212+
213+
## Run a container example
214+
215+
This example demonstrates how to create and submit a Slurm job that runs a bash script that runs the 'hello TRE' container which is used within the [Getting started](../getting-started.md) and the [Run Batch Container](./batch-container-app.md) app. The container is run using the TRE Container Execution Tools' commands `ces-pull`, to pull the container, and `ces-run` to run the container.
216+
217+
Create a job to run the container using Podman:
218+
219+
1. Select **New Job** menu, **From Template** option.
220+
1. Enter Job Name: Run Container
221+
1. Select a Cluster, back-end.
222+
1. Click **Create New Job**.
223+
1. Under 'main_job.sh', click **Open Editor**.
224+
1. A new browser tab will appear with an editor.
225+
1. Update the script as follows:
226+
227+
```bash
228+
#!/bin/bash
229+
#SBATCH --job-name=hello-tre
230+
#SBATCH --output=output.txt
231+
#SBATCH --ntasks=1
232+
#SBATCH --time=10:00
233+
#SBATCH --mem-per-cpu=100
234+
235+
CR_URL=git.ecdf.ed.ac.uk/tre-container-execution-service/containers/epcc-ces-hello-tre:1.1
236+
CR_USER=anonymous
237+
CR_TOKEN=...see below...
238+
ces-pull podman $CR_USER $CR_TOKEN $CR_URL
239+
240+
export CES_SCRATCH=$HOME/scratch/hello-tre
241+
export CES_SAFE_OUTPUTS=$HOME/safe_outputs/hello-tre
242+
mkdir -p $CES_SCRATCH
243+
mkdir -p $CES_SAFE_OUTPUTS
244+
245+
cat << EOF > env_file.txt
246+
HELLO_TRE=Greetings
247+
EOF
248+
249+
cat << EOF > arg_file.txt
250+
-d 10
251+
-n $USER
252+
EOF
253+
254+
ces-run podman -n hello-tre --env-file env_file.txt --arg-file arg_file.txt $CR_URL
255+
```
256+
257+
* For `CR_TOKEN`, copy in the 'hello TRE' container's 'Container registry access token' from the [Run Batch Container](./batch-container-app.md) app's form.
258+
* By default, `ces-run` creates directories with random names - `scratch-NNNN` and `outputs-NNNN` - and mounts these into a container at `/scratch` and `/safe_outputs`. However, `ces-run` supports `CES_SCRATCH` and `CES_SAFE_OUTPUTS` environment variables, which allow for existing directories to be used. In the script above, we create subdirectories of `$HOME` and define `CES_SCRATCH` and `CES_SAFE_OUTPUTS` to tell `ces-run` to mount these directories.
259+
* The script creates a file, `env_file.txt`, with an environment variable to be passed to the 'hello TRE' container. The container uses the environment variable `HELLO_TRE` to customise the greeting it prints.
260+
* The script also creates a file, `arg_file.txt`, with container-specific arguments to be passed directly to the container when it is run. The 'hello TRE' container will pause for `-d` seconds, then issue a greeting to the name cited in `-n` (here, the current user).
261+
262+
1. Click **Save**.
263+
264+
Submit job:
265+
266+
1. Switch to the Job Composer browser tab.
267+
1. Click **Submit job** (green play icon).
268+
269+
![Submit Job button, a green play icon](../../../images/open-ondemand/job-composer-play-button.png){: class="border-img center"} ***Submit Job** button*
270+
271+
1. The job 'Status' should go from 'Queued' to 'Completed'.
272+
273+
If you selected a back-end where your home directory is common to both the Open OnDemand VM and the back-end, then:
274+
275+
1. Click `output.txt` under **Folder Contents**.
276+
1. A new browser tab will appear with the contents of the file:
277+
278+
```text
279+
...
280+
Hello TRE!
281+
...
282+
Greetings USER!
283+
284+
Sleeping for 10 seconds...
285+
1
286+
2
287+
3
288+
4
289+
5
290+
6
291+
7
292+
8
293+
9
294+
10
295+
...and awake!
296+
297+
For more container examples and ideas, visit:
298+
https://github.com/EPCCed/tre-container-samples
299+
Goodbye USER!
300+
```
301+
302+
1. Close the tab.
303+
304+
If you selected a back-end where your home directory is not common to both the Open OnDemand VM and the back-end, then:
305+
306+
1. Click **Open Terminal** to log into the back-end on which the job was run. Once logged in, your current directory will be changed to match the job context directory.
307+
1. View the job context directory's contents:
308+
309+
```bash
310+
ls -1
311+
```
312+
313+
```text
314+
arg_file.txt
315+
env_file.txt
316+
main_job.sh
317+
output.txt
318+
```
319+
320+
1. View `output.txt`:
321+
322+
```bash
323+
cat output.txt
324+
```
325+
326+
```text
327+
...
328+
Hello TRE!
329+
...
330+
Greetings USER!
331+
332+
Sleeping for 10 seconds...
333+
1
334+
2
335+
3
336+
4
337+
5
338+
6
339+
7
340+
8
341+
9
342+
10
343+
...and awake!
344+
345+
For more container examples and ideas, visit:
346+
https://github.com/EPCCed/tre-container-samples
347+
Goodbye USER!
348+
```
349+
350+
Create a new job from the current job to run the container using Apptainer:
351+
352+
1. Switch to the Job Composer browser tab.
353+
1. Select **New Job** menu, **From Selected Job** option.
354+
1. Under 'main_job.sh', click **Open Editor**.
355+
1. Replace use of `podman` with `apptainer` as follows:
356+
357+
1. Replace the `ces-pull podman` line with:
358+
359+
```bash
360+
cd $HOME
361+
ces-pull apptainer $CR_USER $CR_TOKEN $CR_URL
362+
SIF_FILE=$HOME/epcc-ces-hello-tre:1.1.sif
363+
cd $SLURM_SUBMIT_DIR
364+
```
365+
366+
* `ces-pull apptainer` creates a SIF file in the current directory whose name is derived from the last part of the container URL. We change into `$HOME` so that the SIF file is created in `$HOME` before moving back to the directory with the job files, `$SLURM_SUBMIT_DIR`. This means we'll only ever have one copy of this SIF file in our `$HOME` directory, rather than one copy for each run of the job, which would quickly consume space!
367+
368+
1. Replace the `ces-run podman` line with:
369+
370+
```bash
371+
ces-run apptainer -n hello-tre --env-file env_file.txt --arg-file arg_file.txt $SIF_FILE
372+
```
373+
374+
Submit job:
375+
376+
1. Switch to the Job Composer browser tab.
377+
1. Click **Submit job** (green play icon).
378+
379+
![Submit Job button, a green play icon](../../../images/open-ondemand/job-composer-play-button.png){: class="border-img center"} ***Submit Job** button*
380+
381+
1. The job 'Status' should go from 'Queued' to 'Completed'.
382+
383+
View `output.txt` as described previously.

0 commit comments

Comments
 (0)