I would like to automate the loading of modules within a CentOS Docker container.
Normally, I would put the commands in the .bashrc
/ .bash_profile
, but I can't seem to get this to work.
Here is the start of my current Dockerfile:
FROM centos:7.6.1810
RUN yum update -y && yum clean all
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm \
&& yum install -y python36u python36u-libs python36u-devel python36u-pip \
&& yum install -y environment-modules mpich mpich-devel gcc-c++ \
&& yum install -y git
RUN echo "source /usr/share/Modules/init/bash" >> /root/.bash_profile \
&& echo "module load mpi/mpich-x86_64" >> /root/.bash_profile \
&& update-alternatives --install /usr/bin/python python /usr/bin/python2 50 \
&& update-alternatives --install /usr/bin/python python /usr/bin/python3.6 60
WORKDIR /app
...
and this is the command that works:
docker run -t my_image:tag /bin/bash -c "source /usr/share/Modules/init/bash; module load mpi/mpich-x86_64; mpiexec"
But I would like just docker run -t my_image:tag /bin/bash -c "mpiexec"
to work.
I have tried adding numerous combinations of echoing commands to e.g. /root/.bashrc
or /app/.bash_profile
, but can't seem to get this to work.
On the docker run
command you describe, bash
is started as a non-login shell in a non-interactive mode. In this context bash
does not evaluate its initialization configuration files like ~/.bash_profile
or ~/.bashrc
.
To adapt bash
initialization in this context the BASH_ENV
variable could be used. At startup in a non-interactive mode, bash
sources the file pointed by this variable if it is set.
So I would suggest to adapt the definition of your docker image like below to:
~/.bashenv
file to hold the environment-modules initialization commands and load of mpi modulefileBASH_ENV
variable pointing to /root/.bashenv
in the image definition to have it set when running a command over the created containerFROM centos:7.6.1810
RUN yum update -y && yum clean all
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm \
&& yum install -y python36u python36u-libs python36u-devel python36u-pip \
&& yum install -y environment-modules mpich mpich-devel gcc-c++ \
&& yum install -y git
&& update-alternatives --install /usr/bin/python python /usr/bin/python2 50 \
&& update-alternatives --install /usr/bin/python python /usr/bin/python3.6 60 \
&& echo "source /usr/share/Modules/init/bash" >> /root/.bashenv \
&& echo "module load mpi/mpich-x86_64" >> /root/.bashenv \
&& echo "[[ -s ~/.bashenv ]] && source ~/.bashenv" >> /root/.bash_profile \
&& echo "[[ -s ~/.bashenv ]] && source ~/.bashenv" >> /root/.bashrc
ENV BASH_ENV=/root/.bashenv
WORKDIR /app