Software Exercise 1.4: Build, Test, and Deploy an Apptainer Container¶
Objective: to practice building and using a custom apptainer container
Why learn this?: You may need to go through this process if you want to use a container for your jobs and can't find one that has what you need.
Create a script called
#!/usr/bin/env python3 import cowsay cowsay.cow('Hello OSG User School')
Give it executable permissions:
$ chmod +x hello-cow.py
Try running the script:
It will likely fail, because the cowsay library isn't installed. This is a scenario where we will want to build our own container that includes a base Python installation and the
Preparing a Definition File¶
We can describe our desired Apptainer image in a special format called a definition file. This has special keywords that will direct Apptainer when it builds the container image.
- Create a file called
py-cowsay.defwith these contents:
Bootstrap: docker From: opensciencegrid/osgvo-ubuntu-20.04:latest %post apt-get update -y apt-get install -y \ python3-pip \ python3-numpy python3 -m pip install cowsay
Note that we are starting with the same
ubuntu base we used in previous
%post statement includes our installation commands, including
numpy packages, and then using
pip to install
To learn more about definition files, see Exercise 3.1
Build the Container¶
Once the definition file is complete, we can build the container.
- Run the following command to build the container:
$ apptainer build py-cowsay.sif py-cowsay.def
As with the Docker image in the previous exercise, the first argument is the name to give to the newly create image file and the second argument is how to build the container image - in this case, the definition file.
Testing the Image Locally¶
Do you remember how to interactively test an image? Look back at Exercise 1.1 and guess what command would allow us to test our new container.
$ singularity shell first-image.sif
Then try running the
If it produces an output, our container works! We can now exit (by typing
exit) and submit a job.
Submit a Job¶
Make a copy of a submit file from a previous exercise in this section. Can you guess what options need to be used or modified?
Make sure you have the following (in addition to
outputand CPU and memory requests):
universe = container container_image = py-cowsay.sif executable = hello-cow.py
Submit the job and verify the output when it completes.
______________________ | Hello OSG User School! | ====================== \ \ ^__^ (oo)\_______ (__)\ )\/\ ||----w | || ||