构建期间在 Dockerfile 中激活和切换 Anaconda 环境

时间:2023-03-03
本文介绍了构建期间在 Dockerfile 中激活和切换 Anaconda 环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几个小时,但无法弄清楚如何在构建过程中在 Dockerfile 中激活和切换 anaconda 环境

I've been trying for hours and can't figure out how to activate and switch anaconda environments in a Dockerfile during the build process

这是初始代码:

FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu16.04

# Set user
ENV SETUSER myuser

RUN useradd -m $SETUSER
USER $SETUSER
WORKDIR /home/$SETUSER

# Install Anaconda
RUN wget https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh
RUN bash Anaconda3-2019.03-Linux-x86_64.sh -b
RUN rm Anaconda3-2019.03-Linux-x86_64.sh

# Set path to conda
ENV CONDA_ENV_NAME mynewenv
RUN /home/$SETUSER/anaconda3/condabin/conda create -q --name $CONDA_ENV_NAME python=3.6 && 
    /home/$SETUSER/anaconda3/condabin/conda clean --yes --all
RUN /home/$SETUSER/anaconda3/condabin/conda activate base #Just for testing anaconda environment

一开始Docker中的anaconda会抱怨shell设置不正确,所以在conda create命令后我添加了:

At first, anaconda in Docker will complain that the shell is not setup properly, so after the conda create command I added:

RUN /home/$SETUSER/anaconda3/condabin/conda init bash
RUN /bin/bash -c "source /home/$SETUSER/.bashrc"
RUN /home/$SETUSER/anaconda3/condabin/conda activate base

在构建 docker 镜像后运行 3 个命令有效(即在调用 docker run container-name 后交互运行),但由于某种原因,它在 构建 容器时不起作用.我发现 $PATH 变量在构建过程中没有被更新,所以在构建时和构建后比较我的 $PATH.

Running the 3 commands after building the docker image works (i.e. running interactively after calling docker run container-name), but for some reason it does not work when building the container. I figured out that the $PATH variable was not being updated during the build, so comparing my $PATH when building and after building.

ENV PATH /home/$SETUSER/anaconda3/envs/$CONDA_ENV_NAME/bin:$PATH
ENV PATH /home/$SETUSER/anaconda3/condabin:$PATH
ENV PATH /home/$SETUSER/anaconda3/bin:$PATH
RUN conda init bash
RUN /bin/bash -c "source /home/$SETUSER/.bashrc"
RUN conda activate base

现在,构建时的 Docker $PATH 和构建后运行容器时交互修改时的 $PATH 是相同的,但我仍然收到 shell 未正确设置错误.

Now, the Docker $PATH when building and the $PATH when modified interactively when running the container after-building is the same, but I'm still getting the shell not properly setup error.

CommandNotFoundError:您的 shell 未正确配置为使用conda activate".要初始化你的 shell,运行$ 康达初始化目前支持的 shell 有:- 重击- 鱼-tcsh- xonsh-zsh- 电源外壳有关更多信息和选项,请参阅conda init --help".重要提示:运行conda init"后,您可能需要关闭并重新启动 shell.

为什么这不起作用???

Why is this not working???

我已经看到可能有使用 miniconda docker 模板的解决方法,但我不能使用它.Docker搭建过程中如何创建和切换anaconda环境?谢谢!

I've seen that there may be workaround using a miniconda docker template, but I cannot use that. How do I create and switch anaconda environment during the Docker building process? Thanks!

推荐答案

你的 Dockerfile 中有太多的 RUN 命令.不仅仅是每个 RUN 在图像中创建一个新层.也是每个 RUN 命令都会启动一个新的 shell,而 conda activate 只适用于当前的 shell.

You've got way too many RUN commands in your Dockerfile. It's not just that each RUN creates a new layer in the image. It's also that each RUN command starts a new shell, and conda activate applies only to the current shell.

您应该将操作的逻辑组组合成一个 RUN 命令.使用 && 组合命令,使用 换行以提高可读性:

You should combine logical groups of actions into a single RUN command. Use && to combine commands, and to break lines for readability:

RUN conda activate <myenv> 
 && conda install <whatever> 
 && ...

请记住:在该 RUN 命令结束时,shell 将消失.因此,如果您想在之后对该 conda 环境执行其他操作,则必须再次运行 conda activate ,或者使用 -n <myenv> 放置一些东西进入一个环境而不先激活它.

Keep in mind: at the end of that RUN command, the shell will be gone. So if you want to do something else to that conda environment afterwards, you've got to run conda activate again, or else use -n <myenv> to put something into an environment without activating it first.

当你从镜像启动一个容器时,你还必须在容器内调用 conda activate.

When you start a container from the image, you will also have to call conda activate inside the container.

这篇关于构建期间在 Dockerfile 中激活和切换 Anaconda 环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:查看 conda 交易的历史记录? 下一篇:Conda 和 Visual Studio 代码调试

相关文章

最新文章