Equivalent of sourcing ~/.bashrc when building Dockerfile

Viewed 267

I have built a docker image (lobis/geant4-root which is on dockerhub) based on Ubuntu 20.04 where I have installed some software and placed the corresponding source XXX in the ~/.bashrc file. When I run a container with this image in interactive mode with docker run -it lobis/geant4-root all the environment variables are correctly set as you would expect on a login shell. Example:

Running docker run -it lobis/geant4-root:

root@99261664efb1:/# geant4-config --version
10.7.1

However, when I try to build another image that uses this one as a base, I cannot find a way to load the variables from ~/.bashrc to use them on the RUN commands. I have seen many questions related to this issue but couldn't get any solution to work.

I have tried changing the final line of the base Dockerfile to SHELL ["/bin/bash", "-c", "-l"] without success and tried to run my commands on the derived Dockerfile using a combination of RUN bash -l -c 'command' and RUN source ~/.bashrc && bash command without success. Also I tried setting up ENV BASH_ENV "/root/.bashrc".

This Dockerfile should output 10.7.1 on the build log indicating that the correct version of the software is found, but no output is produced, and all subsequent commands that require this software fail.

FROM lobis/geant4-root:latest
# some of the things I tried
ENV BASH_ENV "/root/.bashrc"
SHELL ["/bin/bash", "-l"]
RUN ["/bin/bash", "-l", "geant4-config --version"]
RUN geant4-config --version
CMD ["/bin/bash"]

I am guessing this is a fairly common use case, as in my experience some software usually rely on loading though a source script, so please let me know the proper way to do this. Preferably I would like avoiding having to modify the base docker image.

Thanks.

0 Answers
Related