Docker Community Forums

Share and learn in the Docker community.

Bash env setup scripts


(Tlbtlbtlb) #1

My Dockerfile sets up a package (ros-indigo-desktop-full) which includes environment setup scripts in bash.
I’d like to incorporate these settings (like ROS_PATH) into the environment of the container.

RUN . /opt/ros/indigo/setup.bash doesn’t work because the script requires bash, not the /bin/sh that Docker uses.

RUN ["/bin/bash", "-c", "echo '. /opt/ros/indigo/setup.bash' >> /root/.bashrc"] does what it suggests, but that .bashrc doesn’t get run when I start the container.

What’s the supported way of doing this?


(David Maze) #2

Write an entrypoint script that looks like

#!/bin/bash
. /opt/ros/indigo/setup.bash
exec "$@"

Make sure to COPY that file into your container and make it executable, and then name it as the ENTRYPOINT of your container.

Can you fix the script to only use portable POSIX shell features? In my experience these are usually gratuitous syntax differences (source instead of the standard ., an unnecessary function keyword), or are using features that suggest a more powerful language is called for.

Also in the name of technical correctness, Docker doesn’t use any shell at all, or require containers to have shells. If you’re using a container with a Debian or Ubuntu base, those distributions include a lighter-weight, standards-conforming shell as their /bin/sh.


(Tlbtlbtlb) #3

The env setup script invokes a tangle of Catkin scripts (the world’s second most overcomplicated build system, after Bower) so it’s hard to fix.

Your ENTRYPOINT solution works for running things in the container, thanks. It still leaves the problem of subsequent build steps. So I’ve ended up installing ROS with:

RUN apt-get install ros-XXX
ENTRYPOINT ["/ros/entrypoint.sh"]  # your script above, runs /ros/setup.bash
RUN ["/bin/bash", "-c", ". /ros/setup.bash && setup-step-1"]
RUN ["/bin/bash", "-c", ". /ros/setup.bash && setup-step-2"]
RUN ["/bin/bash", "-c", ". /ros/setup.bash && setup-step-3"]
...

which is OK, but might make it annoying to support multiple such setup scripts. I was dreaming of something like

RUN bash -c ". /opt/ros/setup.bash && env | grep ROS" | ENV -  # or some such syntax
RUN setup-step-1
RUN setup-step-2
RUN setup-step-3
...

but I can live without it.


(David Maze) #4

Is the container a Debianish base? You could try

RUN ln -sf bash /bin/sh
RUN . /opt/ros/setup.bash \
 && setup-step-1 \
 && setup-step-2 \
 && c.