python - 从 dockerfile 激活奇点容器中的 conda 环境

标签 python docker conda singularity-container

我正在尝试从现有的 docker 镜像设置一个奇点容器,其中一个名为“tensorflow”的 conda 环境在我运行容器时立即被激活。我找到了有关此主题的一些答案 here .不幸的是,在这篇文章中,他们只解释了他们将如何设置 singularity .def 文件以默认激活 conda 环境。但是,我只想修改我现有的 Dockerfile,然后从中构建一个奇点图像。

到目前为止,我尝试过的是像这样设置 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 容器:

docker run -t -d --rm --name=my_container opensuse_conda:latest

然后进入容器:

docker exec -it my_container bash

结果符合预期。 shell session 直接启动,“tensorflow”环境处于事件状态,由 (tensorflow) 前缀指示。

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

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

并运行容器:

sudo singularity run opensuse_conda.sif

这就是问题所在。默认情况下激活“base”环境而不是“tensorflow”环境。但是,我宁愿在运行奇点容器时激活“tensorflow”环境。

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

非常感谢您的帮助!

最佳答案

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

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

关于python - 从 dockerfile 激活奇点容器中的 conda 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57416579/

相关文章:

docker - Nginx https发布docker-compose

python - 属性错误: type object 'pandana.cyaccess.cyaccess' has no attribute '__reduce_cython__'

python - 使用 int|abs 时 Ansible Jinja 错误

virtualization - 精简的流程虚拟化

docker - 在 Gitlab 管道中完成测试后,Cypress 不会退出

python - 如何在 ubuntu 上更改 conda 基本环境路径?

python - conda环境安装Tensorflow 2.0

python - 叶时间 slider 不会删除 map 中先前添加的标记

python - 从 pandas groupby 返回聚合数据框

python - 根据子串匹配列