How to make Apptainer keep `apt install`ed packages?
May 20, 8:42am
When you install packages using apt install inside an Apptainer (formerly Singularity) container, those changes are ephemeral by default—they disappear when the container exits. To persist the installed packages, you need to create a new image from your modified container.
Here’s how to do it:
1. Start an Apptainer Container in Writable Mode
First, start a container with --writable to allow modifications:
bash
apptainer shell --writable my_container.sif
Inside the container, install your packages:
bash
apt update && apt install -y <package_name>
2. Save the Modified Container as a New Image
After installing the packages, exit the container and save it as a new image to persist the changes:
Note: This only works if the original container was built from a definition file (Singularity or apptainer recipe). If the original container was a SIF file (immutable), you need to rebuild it from scratch with a definition file.
3. (Recommended) Use a Definition File
The best practice is to edit the definition file (Singularity or apptainer recipe) and rebuild the container.