我正在尝试从现有的Docker镜像中设置一个Singularity容器,在其中运行名为“tensorflow”的conda环境。当我运行容器时,该环境将被激活。我在这个主题上找到了一些答案 (链接)。不幸的是,在这篇文章中,他们只解释了如何设置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容器:
docker run -t -d --rm --name=my_container opensuse_conda:latest
使用以下命令进入容器:
docker exec -it my_container bash
结果正如预期。 shell 会话直接启动 "tensorflow" 环境,这由 (tensorflow) 前缀表示。
要从此 Docker 镜像构建 Singularity 镜像,我使用:
sudo singularity build opensuse_conda.sif docker-daemon://opensuse_conda:latest
并使用以下命令来运行容器:
sudo singularity run opensuse_conda.sif
这就是问题所在。默认情况下会激活“base”环境,而不是“tensorflow”环境。然而,当我运行singularity容器时,我希望激活“tensorflow”环境。
请问如何修改Dockerfile,使得无论是运行docker容器还是singularity容器,默认环境都是“tensorflow”?
非常感谢您的帮助!
singularity run opensuse_conda.sif bash -c“python test.py”
以确保激活“tensorflow”环境?对我来说这很有效,但我想知道是否真的必须使用bash -c
命令。 - Constihdocker run -it <image> /bin/bash
时,这是正确的。正如预期的那样,我可以获得一个交互式会话,并且 "tensorflow" 环境处于活动状态。然而,运行singularity run <image>.sif /bin/bash
会产生一个交互式会话,但没有任何环境处于活动状态。我必须首先输入conda activate tensorflow
才能进入我的自定义环境。为什么会这样呢?看起来像是我在 Dockerfile 中创建的 .bashrc 文件在启动交互式 singularity 容器会话时没有被读取。 - Constih--norc
和自定义的$PS1
,以便您知道您正在使用 Singularity 镜像而不是主机。当您使用bash -c ...
作为运行命令时,您将绕过通常使用--norc
的位置,这就是为什么您的运行环境与您期望的一样的原因。 - tsnowlanbash -c ...
自动激活环境的方法? - web