How to use the command 'singularity' (with examples)
Singularity is a command-line tool for managing Singularity containers and images. It allows users to build, pull, and run containers. Singularity is widely used in scientific computing and research, as it provides a secure and reproducible way to package and distribute software and data.
Use case 1: Download a remote image from Sylabs Cloud
Code:
singularity pull --name image.sif library://godlovedc/funny/lolcow:latest
Motivation:
Downloading a remote image from Sylabs Cloud allows users to easily access and use pre-built Singularity images without the need to build them from scratch. This use case is especially useful for users who need specific software or data that is already available as a Singularity image.
Explanation:
singularity pull
: This command is used to download a Singularity image from a remote source.--name image.sif
: This argument specifies the name of the downloaded image. In this example, the image will be saved as “image.sif”.library://godlovedc/funny/lolcow:latest
: This is the URL of the remote image in the Sylabs Cloud. The “:latest” tag indicates that the latest version of the image should be downloaded.
Example output:
The Singularity image “image.sif” will be downloaded from the Sylabs Cloud and saved locally.
Use case 2: Rebuild a remote image using the latest Singularity image format
Code:
singularity build image.sif docker://godlovedc/lolcow
Motivation:
Rebuilding a remote image using the latest Singularity image format ensures compatibility and updated features. This use case is useful when a remote image is in an older Singularity format or when specific modifications or customizations are required.
Explanation:
singularity build
: This command is used to build a Singularity image.image.sif
: This specifies the name of the rebuilt image.docker://godlovedc/lolcow
: This is the URL of the remote Docker image that will be used to build the Singularity image.
Example output:
The remote Docker image “godlovedc/lolcow” will be downloaded and converted into a Singularity image named “image.sif”.
Use case 3: Start a container from an image and get a shell inside it
Code:
singularity shell image.sif
Motivation:
Starting a container from an image and getting a shell inside it allows users to interact with the underlying system and perform various tasks. This use case is useful for debugging, running interactive commands, or exploring the container’s environment.
Explanation:
singularity shell
: This command is used to start a container and obtain a shell inside it.image.sif
: This specifies the name of the Singularity image to be used.
Example output:
A shell prompt will be displayed inside the container, providing an interactive environment within the Singularity image.
Use case 4: Start a container from an image and run a command
Code:
singularity exec image.sif command
Motivation:
Starting a container from an image and running a specific command allows users to execute a single task or script without needing to enter an interactive shell. This use case is useful for running batch jobs, carrying out specific computations, or executing scripts within the Singularity environment.
Explanation:
singularity exec
: This command is used to start a container and execute a command inside it.image.sif
: This specifies the name of the Singularity image to be used.command
: This is the command that will be executed inside the container.
Example output:
The specified command will be executed inside the container, and the output will be displayed in the terminal.
Use case 5: Start a container from an image and execute the internal runscript
Code:
singularity run image.sif
Motivation:
Starting a container from an image and executing the internal runscript allows users to run the default behavior defined by the image creator. This use case is particularly useful when working with images that have predefined scripts or commands to be executed upon container startup.
Explanation:
singularity run
: This command is used to start a container and execute the internal runscript without launching a shell.image.sif
: This specifies the name of the Singularity image to be used.
Example output:
The default behavior defined by the internal runscript will be executed, which can include various actions such as running a specific command, starting a web server, or launching a graphical application.
Use case 6: Build a Singularity image from a recipe file
Code:
sudo singularity build image.sif recipe
Motivation:
Building a Singularity image from a recipe file allows users to create custom images with specific software dependencies and configurations. This use case is useful for creating reproducible environments, distributing software, or packaging research workflows.
Explanation:
sudo singularity build
: This command is used to build a Singularity image.image.sif
: This specifies the name of the generated Singularity image.recipe
: This is the path to the recipe file used to build the image.
Example output:
The Singularity image “image.sif” will be created based on the instructions and specifications defined in the recipe file, incorporating the desired software, configurations, and dependencies.
Conclusion:
The “singularity” command provides a comprehensive set of functionalities for managing Singularity containers and images. With the examples provided, users can easily download remote images, rebuild images using the latest format, start containers and run commands, execute internal runscripts, and create custom images from recipe files. Singularity is a powerful tool for reproducible research and efficient software management in scientific computing.