Using Singularity containers to run commands
Overview
Teaching: 10 min
Exercises: 5 minQuestions
How do I run different commands within a container?
How do I access an interactive shell within a container?
Objectives
Learn how to run different commands when starting a container.
Learn how to open an interactive shell within a container environment.
Running specific commands within a container
We saw earlier that 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 lolcow.sif container that we’ve already pulled from Singularity Hub, we can run the following within the test directory where the lolcow.sif file is located:
remote$ singularity exec lolcow.sif /bin/echo "Hello, world"
Hello, world
Here we see that a container has been started from the lolcow.sif image and the /bin/echo command has been run within the container, passing the input Hello, world. The command has echoed the provided input to the console and the container has terminated.
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.
Basic exercise: Running a different command within the “hello-world” container
Can you run a container based on the
lolcow.sifimage that prints the current date and time?Solution
remote$ singularity exec lolcow.sif /bin/dateFri Jun 26 15:17:44 BST 2020
Difference between singularity run and singularity exec
Above, we used the singularity exec command. In earlier episodes 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 usingsingularity run, you simply specify the image file name. As we saw earlier, you can use thesingularity inspectcommand 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 followingsingularity exec <image file name>. This will override any default command specified within the image metadata that would otherwise be run if you usedsingularity run.
Opening an interactive shell within a container
If you want to open an interactive shell within a container, Singularity provides the singularity shell command. Again, using the lolcow.sif image, and within our test directory, we can run a shell within a container from the hello-world image:
remote$ singularity shell lolcow.sif
Singularity> whoami
[<your username>]
Singularity> ls
lolcow.sif
Singularity>
As shown above, we have opened a shell in a new container started from the lolcow.sif image. Note that the shell prompt has changed to show we are now within the Singularity container.
Use the exit command to exit from the container shell.
Key Points
The
singularity execis an alternative tosingularity runthat allows you to start a container running a specific command.The
singularity shellcommand can be used to start a container and run an interactive shell within it.