Using the Singularity containers to run commands

Running specific commands within a container

We can use the singularity inspect command to see the run script that a container is configured to run by default. What if we want to run a different command within a container?

If we know the path of an executable that we want to run within a container, we can use the singularity exec command. For example, using the fastqc-0.11.9--0.sif container that we’ve already downloaded, we can run the following within the current directory where the fastqc-0.9.11--0.sif file is located

singularity exec fastqc-0.11.9--0.sif fastqc -h

Note that the use of singularity exec has overriden any run script set within the image metadata and the command that we specified as an argument to singularity exec has been run instead.

The difference between singularity run and singularity exec

Above we used the singularity exec command. In earlier sections of this course we used singularity run. To clarify, the difference between these two commands is:

  • singularity run: This will run the default command set for containers based on the specified image. This default command is set within the image metadata when the image is built (we’ll see more about this in later episodes). You do not specify a command to run when using singularity run, you simply specify the image file name. As we saw earlier, you can use the singularity inspect command to see what command is run by default when starting a new container based on an image.
  • singularity exec: This will start a container based on the specified image and run the command provided on the command line following singularity exec <image file name>. This will override any default command specified within the image metadata that would otherwise be run if you used singularity run.

Singularity run is not so common for HPC uses but will be used for instances (servers).

Interactive shell

If you want to open an interactive shell within a container, Singularity provides the singularity shell command.

singularity shell fastqc-0.11.9--0.sif

Singularity> whoami
Singularity> ls
<listing current working directory of the host>

Use the exit command to exit from the container shell.