简体   繁体   English

从 dockerfile 激活奇异容器中的 conda 环境

[英]Activate conda environment in singularity container from dockerfile

I'm trying to set up a singularity container from an existing docker image in which a conda environment named "tensorflow" is activated as soon as I run the container.我正在尝试从现有的 docker 镜像中设置一个奇点容器,其中一个名为“tensorflow”的 conda 环境在我运行容器后立即被激活。 I've found some answers on this topic here .我发现有关这个主题的一些答案在这里 Unfortunately, in this post they only explain how they would set up the the singularity .def file to activate the conda environment by default.不幸的是,在这篇文章中,他们只解释了如何设置奇点 .def 文件以默认激活 conda 环境。 However, I want to modify my existing Dockerfile only and then build a singularity image from it.但是,我只想修改我现有的 Dockerfile,然后从中构建一个奇异图像。

What I've tried so far is setting up the Dockerfile like this:到目前为止我所尝试的是像这样设置 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"]

After building the docker image I run the docker container with:构建 docker 镜像后,我使用以下命令运行 docker 容器:

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

and enter the container with:并使用以下命令输入容器:

docker exec -it my_container bash

The result is as expected.结果如预期。 The shell session is started directly with the "tensorflow" environment being active which is indicated by the (tensorflow) prefix. shell 会话直接在“tensorflow”环境处于活动状态时启动,由 (tensorflow) 前缀表示。

To build a singularity image from this docker image I use:要从这个 docker 镜像构建一个奇点镜像,我使用:

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

and run the container with:并使用以下命令运行容器:

sudo singularity run opensuse_conda.sif

This is where the problem occurs.这就是问题发生的地方。 Instead of the "tensorflow" environment the "base" environment is activated by default.默认情况下激活“基础”环境而不是“tensorflow”环境。 However, I would rather have the "tensorflow" environment being activated when I run the singularity container.但是,我宁愿在运行奇点容器时激活“tensorflow”环境。

How can I modify my Dockerfile so that when running both the docker container and the singularity container the default environment is "tensorflow"?如何修改我的 Dockerfile,以便在运行 docker 容器和奇异性容器时默认环境为“tensorflow”?

Thank you very much for your help!非常感谢您的帮助!

Your problem is that .bashrc will only be read when you start an interactive shell, but not when the container is running with the default command.您的问题是.bashrc只会在您启动交互式 shell 时被读取,而在容器使用默认命令运行时不会被读取。 See this answer for background information.有关背景信息,请参阅此答案

There are a bunch of bash startup files where you could put the conda activate tensorflow command in instead.有一堆bash 启动文件,您可以在其中放置conda activate tensorflow命令。 I recommend to define a file of your own, and put the filename into the BASH_ENV environment variable.我建议自己定义一个文件,并将文件名放入BASH_ENV环境变量中。 Both can easily be done from the Dockerfile.两者都可以从 Dockerfile 轻松完成。

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

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