ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Automatically source different setup.bash files when a starting singularity image

asked 2019-04-24 06:01:26 -0500

rickstaa gravatar image

updated 2019-04-25 15:24:27 -0500

Problem description

I am trying to source the setup.bash file and other setup.bash files automatically when shelling into a singularity image. I, therefore, added the following line to my singularity recipe file:

%environment

    ## Source ros setup file ##
    . /opt/ros/kinetic/setup.sh

After building the singularity image this source command ends up in the *90-enviroments.sh script, which is then sourced when the singularity shell is opened. Unfortunately, the environmental variables that are supposed to be set when sourcing the setup.bash file are not available when I shell into the container using the singularity shell --nv container.simg command. For example, when I am trying to use the roslaunch command I get the following error:

bash: roslaunch: command not found

I found the following solution on StackOverflow but I was hoping there is something similar to the runscript argument that enables me to source files when shelling into a singularity container.

Version of Singularity:

3.0.3

Expected behavior

I expected the /opt/ros/kinetic/setup.bash file to be sourced when the container is loaded using shell --nv container.simg

Actual behavior

Although the source command ends up in the *90-enviroments.sh script none of the enviromental variables that are supposed to be added to my workspace when sourcing the /opt/ros/kinetic/setup.bash file is available in my workspace. These commands appear to be available again when I run the bash command inside the singularity image. I, therefore, suspect that the singularity image uses a slightly different /bin/bash shell. The echo $SHELL command both has /bin/bash as an output in both the singularity shell and the bash shell running inside the singularity image.

Steps to reproduce the behaviour

Add one of the following source commands to the %enviroment section of the singularity recipe file:

  • . /file_you_you_want_to_source.sh
  • source /file_you_you_want_to_source.sh
  • bash -c "source /file_you_you_want_to_source.sh"
  • sh -c "source /file_you_you_want_to_source.sh"
  • bash -c ". /file_you_you_want_to_source.sh"
  • sh -c ". /file_you_you_want_to_source.sh"

EDIT:

I migrated my issue to the singularity Github issue page

edit retag flag offensive close merge delete

Comments

3

Two observations:

  1. this reads more like a Singularity question than a ROS one
  2. "none of these commands [..] work" is too vague: please provide accurate descriptions of what did or did not work and include error messages if you were presented any
gvdhoorn gravatar image gvdhoorn  ( 2019-04-24 06:47:59 -0500 )edit

@gvdhoorn Thanks for your comment I updated the question and also migrated my question to the singularity GitHub issues page.

rickstaa gravatar image rickstaa  ( 2019-04-24 07:13:10 -0500 )edit
1

migrated my question to the singularity GitHub issues page.

if you did, then please post a link to that issue here in a comment so we can keep things connected.

gvdhoorn gravatar image gvdhoorn  ( 2019-04-24 07:16:22 -0500 )edit

This issue has been migrated to the singularity GitHub issues page

rickstaa gravatar image rickstaa  ( 2019-04-24 07:19:57 -0500 )edit
1

The official O(S)R(F) Docker images use ENTRYPOINT for this (here fi), with this script.

The Singularity documentation discusses how ENTRYPOINT and CMD are converted / can be used in Singularity images and containers: Singularity and Docker: Building images for Singularity from Docker Registries - Working with Definition Files - Directing Execution.

gvdhoorn gravatar image gvdhoorn  ( 2019-04-24 10:13:03 -0500 )edit

@gvdhoorn Thanks a lot for pointing me to the right documentation. I now see I had a wrong understanding of when to use the run, shell and start commands. After thoroughly going to the documentation and testing out different options I came to the following conclusion on what to do in my case:

  • Other than the option depicted by the StackOverflow question there is no easy method of directly sourcing files when invoking a container using the shell command.
  • I think it is, therefore, a better idea to include a %runscript and %startscript in my recipe file and following interacting with the created container using the run and start instance commands. This gives me full control over whether I want to use the ENTRYPOINT and CMD options supplied by the docker file or use my own run/startup commands.
rickstaa gravatar image rickstaa  ( 2019-04-24 10:32:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-04-25 10:47:52 -0500

rickstaa gravatar image

updated 2021-05-29 11:29:32 -0500

As this question contains a singularity related issue it was discussed on the github singularity issue page. The solution I created, based on comments made by @gvdhoorn and @jmstover, can be found below.

Solution

Unfortunately as the singularity %runscript uses the /bin/sh you need a separate script to source a setup.bash file. I, therefore, in the runscript now open a /bin/bash shell using the -rcfile argument. Following in this rcfile called .singularity_bashrc I source the setup.bash files. In singularity versions >3.2.x you now also have the ability to directly do this from within the runscript by using a shebang line.

Steps

1). Clone your .bashrc_file inside your singularity container.

### Clone .singularity_bashrc repository file ##
bash -c "cd / \
    && git clone https://github.com/rickstaa/pandasim_singularity_recipes \
    && cp /pandasim_singularity_recipes/.singularity_bashrc . \
    && rm -rf pandasim_singularity_recipes"

Alternatively, you can also add the following code to the %post section of your singularity recipe to create the .bashrc file from within your recipe file.

## Add extra commands to singularity entrypoint script
touch /.singularity_bashrc.sh
chmod +x /singularity_bashrc.sh
cat <<-EOF >> /singularity_bashrc.sh
    #!/bin/bash
    set -e

    # setup ros environment
    source "/opt/ros/\$ROS_DISTRO/setup.bash"
    echo "Ros setup bash file sourced."

    # Setup Panda_simulation enviroment (chained to franka_ros and moveit)
    if [ -f "/pandasim_ws/devel/setup.bash" ]; then
        source "/pandasim_ws/devel/setup.bash"
        echo "Panda_simulation setup bash file sourced."
    fi
EOF

!!!!Use tabs instead of 4 spaces. More information on heredoc list indenting can be found here !!!!

2). Add the following code in the %runscript section:

## Run custom entrypoint BASH script
OCI_ENTRYPOINT='"/custom_entrypoint.sh"'
OCI_CMD='"/bin/bash"'
SINGULARITY_OCI_RUN="${OCI_ENTRYPOINT} ${OCI_CMD} '-rcfile' '/.singularity_bashrc'"
eval ${SINGULARITY_OCI_RUN}

3). Build the singularity container using the following build command:

sudo singularity build --sandbox singularity_container singularity_recipe.def

4). Now you can call the singularity container using the singularity run command:

singularity run --nv singularity_container
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-04-24 06:01:26 -0500

Seen: 2,305 times

Last updated: May 29 '21