简体   繁体   English

Docker,CentOS:如何在bash shell中自动使用环境模块加载模块?

[英]Docker, CentOS: How to automate loading of modules with environment-modules, in bash shell?

I would like to automate the loading of modules within a CentOS Docker container. 我想自动在CentOS Docker容器中加载模块。

Normally, I would put the commands in the .bashrc / .bash_profile , but I can't seem to get this to work. 通常,我会将命令放在.bashrc / .bash_profile ,但似乎无法正常工作。

Here is the start of my current Dockerfile: 这是我当前的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. 但是我只想让docker run -t my_image:tag /bin/bash -c "mpiexec"即可工作。

I have tried adding numerous combinations of echoing commands to eg /root/.bashrc or /app/.bash_profile , but can't seem to get this to work. 我尝试过将回显命令的多种组合添加到/root/.bashrc/app/.bash_profile ,但似乎无法/app/.bash_profile正常工作。

On the docker run command you describe, bash is started as a non-login shell in a non-interactive mode. 在您描述的docker run命令上, bash作为非登录shell以非交互模式启动。 In this context bash does not evaluate its initialization configuration files like ~/.bash_profile or ~/.bashrc . 在这种情况下, bash不会评估其初始化配置文件,例如~/.bash_profile~/.bashrc

To adapt bash initialization in this context the BASH_ENV variable could be used. 为了适应这种情况下的bash初始化,可以使用BASH_ENV变量。 At startup in a non-interactive mode, bash sources the file pointed by this variable if it is set. 在非交互模式下启动时, bash获取此变量所指向的文件(如果已设置)。

So I would suggest to adapt the definition of your docker image like below to: 因此,我建议将您的docker映像的定义修改如下:

  • create a ~/.bashenv file to hold the environment-modules initialization commands and load of mpi modulefile 创建一个~/.bashenv文件来保存环境模块的初始化命令并加载mpi modulefile
  • then declaring BASH_ENV variable pointing to /root/.bashenv in the image definition to have it set when running a command over the created container 然后在创建的容器上运行命令时,声明映像定义中指向/root/.bashenv BASH_ENV变量进行设置
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
    && 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM