I am using the following workflow on RedHat in terminal:
- open a singularity image:
singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img
- export some display inside the singularity:
export DISPLAY=:0.0
- export a path to gmsh inside the singularity:
export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH"
I want to put everything as a bash script. My first (not working) approach is the following (all in a singularity_script.sh file):
#!/bin/bash
function singularity_script(){
singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img
export DISPLAY=:0.0
export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH"
}
I execute it by sourcing and then calling the function:
chmod +x singularity_script.sh
. singularity_script.sh
singularity_script
But of course, this does not work as the exports are done (I think?) in the parent terminal and not the son singularity. So I do not get the display and path exported correctly in singularity.
Any way to fix this? I guess the solution would be to automatically run the script inside the container at startup of the container, but how to do this simply?
Got it! The solution by @tormodlandet had one problem, i.e. the singularity container died once the command called with the
-c
option were run.I can get it to work by executing the following instead:
This does the command I want in the singularity, and then spawns a
gnome-terminal
from within the singularity container that does not die until it is exited.Missing the
/bin/bash -norc
at the end means that the singularity container dies after the last command.So in order to call the useful commands from a script rather than the plain command, simply use:
where there is in the current working directory a
script_singularity.sh
file containing the commands to run:Edit
If you want to still have a good looking terminal, you can provide a
bashrc
config file. For example:where
singularity_bashrc
is your bashrc to use. For example, it works great with (this is verbose to add here and there are many places with much more detailed bashrc explanations, but this was requested in some comments):If you have access to change the Singularity image (or rebuild it), you can change the runscript. Otherwise you could put your bash script at a location that is mounted inside the Singularity image (probably the current working directory at least depending on your setup). Then run something like this (using exec)