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:
First, start a container with --writable to allow modifications:
bashapptainer shell --writable my_container.sif
Inside the container, install your packages:
bashapt update && apt install -y <package_name>
After installing the packages, exit the container and save it as a new image to persist the changes:
bashapptainer build my_modified_container.sif my_container.sif
Note: This only works if the original container was built from a definition file (
Singularityorapptainerrecipe). If the original container was a SIF file (immutable), you need to rebuild it from scratch with a definition file.
The best practice is to edit the definition file (Singularity or apptainer recipe) and rebuild the container.
my_container.def):singularityBootstrap: ubuntu OSVersion: 22.04 MirrorURL: http://archive.ubuntu.com/ubuntu/ Include: apt %post apt update && apt install -y <package_name> rm -rf /var/lib/apt/lists/*
Then, rebuild the container:
bashapptainer build my_new_container.sif my_container.def
If you want temporary persistence (e.g., for testing), you can use an overlay:
bashapptainer shell --overlay my_overlay.sif my_container.sif
my_overlay.sif until you delete it.--writable or --overlay.Le Chat can make mistakes. Check answers. Learn more