Skip to content

2. The Container Cache

Overview

Questions

  • Why does Apptainer use a local cache?
  • Where does Apptainer store images?
  • How do I configure my cache to work on NeSI?

Objectives

  • Learn about Apptainer’s image cache.
  • Learn howto setup your cache on Mahuika

Verify the installation and check the version

$ apptainer --version
apptainer version 1.3.0

Using the image cache and temporary directories

Pull an ubuntu Linux image from DockerHub

code

$ apptainer pull docker://ubuntu 
INFO:    Converting OCI blobs to SIF format
INFO:    Starting build...
Getting image source signatures
Copying blob 3c645031de29 done   | 
Copying config 7af9ba4f0a done   | 
Writing manifest to image destination
2024/04/25 12:26:02  info unpack layer: sha256:3c645031de2917ade93ec54b118d5d3e45de72ef580b8f419a8cdc41e01d042c
INFO:    Creating SIF file...

So what we did here was to use the docker:// URL to tell apptainer to go to DockerHub and pull the Ubuntu Docker image. Apptainer pulls the image and converts it into the image file format used by Apptainer and Singularity: .sif. The image file is save in our current directory as ubuntu_latest.sif and a cached copy is in the $HOME/.apptainer/cache

Apptainer doesn’t have a local image repository in the same way as Docker, however, it does cache downloaded image files. Apptainer also uses a temporary directory for building images.

If you delete the .sif image that you have pulled from a remote image repository such as DockerHub, and then pull it again, provided the image is unchanged from the version you previously pulled, you will be given a copy of the image file from your local cache rather than the image being downloaded again from the remote source. This removes unnecessary network transfers and is particularly useful for large images which may take some time to transfer over the network. To demonstrate this, remove the ubuntu_latest.sif file stored in your directory and then issue the pull command again:

code

$ rm ubuntu_latest.sif
$ apptainer pull docker://ubuntu

Cleaning the Apptainer image cache

We can remove images from the cache using the apptainer cache clean command. Running the command without any options will display a warning and ask you to confirm that you want to remove everything from your cache. This is very useful if you are running low on space or do not want to keep old images on disk.

You can also remove specific images or all images of a particular type. Look at the output of apptainer cache clean --help for more information.

Apptainer Cache and Temporary files : How to change the default path

By default, Apptainer uses $HOME/.apptainer as the location for cache and temporary files.

You can change the location of the cache by setting environment variables to the cache and temporary directory locations you want to use. Those environment variables are: APPTAINER_CACHEDIR & APPTAINER_TMPDIR

Key points

  • Apptainer caches downloaded images so that an unchanged image isn’t downloaded again when it is requested using the apptainer pull command.

  • You can free up space in the cache by removing all locally cached images or by specifying individual images to remove.