In Apptainer, how to save a modified `.sif` (as `apt install`ed a package for instance) as another `.sif`?
May 20, 9:50am
In Apptainer (formerly Singularity), once you've modified a .sif container (e.g., by installing a package with apt install inside the container), you can save the modified container as a new .sif file using the following steps:
1. Enter the Container in Writable Mode
To make changes (like installing packages), you need to enter the container in writable mode (if it's not already writable).
If the container is read-only, you can use an overlay or the --writable flag (if supported by your Apptainer version and filesystem).
bash
apptainer shell --writable your_container.sif
Note: The --writable flag requires a writable filesystem (e.g., ext3, xfs). If this fails, use an overlay (see below).
2. Use an Overlay (Recommended for Non-Writable Filesystems)
If your filesystem doesn't support --writable, use an overlay to make temporary changes, then commit them to a new .sif:
bash
# Create a writable overlay directory
mkdir -p /tmp/overlay
# Enter the container with the overlay
apptainer shell --overlay /tmp/overlay your_container.sif
Inside the container, make your changes (e.g., apt install <package>).
3. Save the Modified Container as a New .sif
Once your changes are made, exit the container and use the apptainer build command to save the modified state as a new .sif file.
Option A: Build from the Modified Overlay
If you used an overlay, build a new .sif from the overlay and the original container:
Note: This only works if you used --overlay and the changes are persistent in the overlay. For a more reliable approach, use a sandbox directory (see below).
Option B: Use a Sandbox Directory (Most Reliable)
Convert the .sif to a sandbox directory (writable):