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:

bash
apptainer build new_container.sif your_container.sif

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)

  1. Convert the .sif to a sandbox directory (writable):
    bash
    apptainer build --sandbox my_sandbox your_container.sif
  2. Enter the sandbox and make changes:
    bash
    apptainer shell --writable my_sandbox
    (Install packages, modify files, etc.)
  3. Rebuild the sandbox as a new .sif:
    bash
    apptainer build new_container.sif my_sandbox
  4. Clean up (optional):
    bash
    rm -rf my_sandbox

4. Verify the New Container

Check that your changes are saved:

bash
apptainer exec new_container.sif apt list --installed | grep <your_package>

Key Notes

  • Overlay vs. Sandbox: Overlays are temporary; sandboxes are persistent and easier to convert back to .sif.
  • Root Privileges: Some operations (like apptainer build) require sudo or root access, depending on your Apptainer configuration.
  • Apptainer Version: Ensure you're using a recent version of Apptainer (1.0+), as older versions may have different syntax.
May 20, 9:50am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more