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:
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).
bashapptainer shell --writable your_container.sif
Note: The
--writableflag requires a writable filesystem (e.g.,ext3,xfs). If this fails, use an overlay (see below).
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>).
.sifOnce your changes are made, exit the container and use the apptainer build command to save the modified state as a new .sif file.
If you used an overlay, build a new .sif from the overlay and the original container:
bashapptainer build new_container.sif your_container.sif
Note: This only works if you used
--overlayand the changes are persistent in the overlay. For a more reliable approach, use a sandbox directory (see below).
.sif to a sandbox directory (writable):
bashapptainer build --sandbox my_sandbox your_container.sif
(Install packages, modify files, etc.)bashapptainer shell --writable my_sandbox
.sif:
bashapptainer build new_container.sif my_sandbox
bashrm -rf my_sandbox
Check that your changes are saved:
bashapptainer exec new_container.sif apt list --installed | grep <your_package>
.sif.apptainer build) require sudo or root access, depending on your Apptainer configuration.Le Chat can make mistakes. Check answers. Learn more