从dockerfi激活singularity容器中的conda环境

2024-03-28 22:34:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个已有的docker映像中建立一个singularity容器,在这个容器中,一运行容器,名为“tensorflow”的conda环境就会被激活。我在这个话题上找到了一些答案here。不幸的是,在这篇文章中,他们只解释了如何设置singularity.def文件来激活conda环境。但是,我只想修改现有的Dockerfile,然后从中构建一个singularity图像。在

到目前为止,我所做的就是这样设置Dockerfile:

FROM opensuse/tumbleweed

ENV PATH /opt/conda/bin:$PATH
ENV PATH /opt/conda/envs/tensorflow/bin:$PATH

# Add conda environment files (.yml)
COPY ["./conda_environments/", "."]

# Install with zypper
RUN zypper install -y sudo wget bzip2 vim tree which util-linux

# Get installation file
RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-2019.07-Linux-x86_64.sh -O ~/anaconda.sh

# Install anaconda at /opt/conda
RUN /bin/bash ~/anaconda.sh -b -p "/opt/conda"

# Remove installation file
RUN rm ~/anaconda.sh

# Make conda command available to all users
RUN ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

# Create tensorflow environment
RUN conda env create -f tensorflow.yml

# Activate conda environment with interactive bash session
RUN echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc
RUN echo "conda activate tensorflow" >> ~/.bashrc

# Default command
CMD ["/bin/bash"]

构建docker映像后,我运行docker容器:

^{pr2}$

进入容器时:

docker exec -it my_container bash

结果如期而至。shell会话在“tensorflow”环境处于活动状态时直接启动,该环境由(tensorflow)前缀表示。在

要从这个docker图像构建一个奇点图像,我使用:

sudo singularity build opensuse_conda.sif docker-daemon://opensuse_conda:latest

并运行容器:

sudo singularity run opensuse_conda.sif

这就是问题发生的地方。默认情况下,将激活“基本”环境,而不是“tensorflow”环境。但是,我宁愿在运行singularity容器时激活“tensorflow”环境。在

如何修改Dockerfile,以便在运行docker容器和singularity容器时,默认环境为“tensorflow”?在

非常感谢你的帮助!在


Tags: pathdockerrundockerfilebashbin环境tensorflow
1条回答
网友
1楼 · 发布于 2024-03-28 22:34:12

您的问题是,.bashrc只有在您启动交互式shell时才会被读取,而在容器使用默认命令运行时不会被读取。有关背景信息,请参见this answer。在

有一组bash startup files可以在其中放入conda activate tensorflow命令。我建议您自己定义一个文件,并将文件名放入BASH_ENV环境变量中。从Dockerfile可以轻松地完成这两个任务。在

相关问题 更多 >