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:

bash
apptainer build my_modified_container.sif my_container.sif

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.

Example Definition File (my_container.def):

singularity
Bootstrap: 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:

bash
apptainer build my_new_container.sif my_container.def

4. (Alternative) Use OverlayFS (for temporary persistence)

If you want temporary persistence (e.g., for testing), you can use an overlay:

bash
apptainer shell --overlay my_overlay.sif my_container.sif
  • Changes will persist in my_overlay.sif until you delete it.

Key Takeaways

  • Default behavior: Changes inside a container are lost when it exits.
  • Permanent changes: Rebuild the container from a definition file.
  • Temporary changes: Use --writable or --overlay.
May 20, 8:42am

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