問題描述
我已經嘗試了幾個小時,但無法弄清楚如何在構建過程中在 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模板網!