【问题标题】:Docker, CentOS: How to automate loading of modules with environment-modules, in bash shell?Docker,CentOS:如何在 bash shell 中使用环境模块自动加载模块?
【发布时间】:2019-12-26 01:40:22
【问题描述】:

我想在 CentOS Docker 容器中自动加载模块。

通常,我会将命令放在.bashrc / .bash_profile 中,但我似乎无法让它工作。

这是我当前 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

...

这是有效的命令:

docker run -t my_image:tag /bin/bash -c "source /usr/share/Modules/init/bash; module load mpi/mpich-x86_64; mpiexec"

但我只希望 docker run -t my_image:tag /bin/bash -c "mpiexec" 工作。

我尝试添加许多回显命令的组合,例如/root/.bashrc/app/.bash_profile,但似乎无法正常工作。

【问题讨论】:

  • man bashINVOCATION 部分描述了加载不同配置文件的方式和时间。

标签: bash docker centos centos7 environment-modules


【解决方案1】:

在您描述的docker run 命令上,bash 以非交互模式作为非登录 shell 启动。在这种情况下,bash 不会评估其初始化配置文件,例如 ~/.bash_profile~/.bashrc

要在这种情况下适应 bash 初始化,可以使用 BASH_ENV 变量。在非交互模式下启动时,bash 获取此变量指向的文件(如果已设置)。

因此,我建议将您的 docker 映像的定义调整为:

  • 创建一个~/.bashenv 文件来保存环境模块初始化命令和mpi 模块文件的加载
  • 然后在映像定义中声明指向/root/.bashenvBASH_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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2011-04-15
    • 1970-01-01
    • 2015-10-17
    相关资源
    最近更新 更多