在Docker中配置/设置/激活Python Conda环境 [英] Configure/Setting/Activate a Python Conda environment in Docker
问题描述
我正在尝试在docker容器内设置一个conda环境,但是在dockerfile中的环境创建阶段,它始终会失败,并显示以下错误: conda activate";失败,因为外壳未初始化
I am trying to set up a conda environment inside a docker container, but it always keep failing at the environment creation stage in the dockerfile with the error:
conda activate" fails because shell is not initialized
下面是我的Dockerfile:
Below is my Dockerfile:
FROM pytorch/pytorch:1.1.0-cuda10.0-cudnn7.5-runtime
WORKDIR /app
# SET BASH AS CURRENT SHELL
RUN chsh -s /bin/bash
SHELL ["/bin/bash", "-c"]
# INSTALL ANACONDA
RUN apt-get update -y
RUN apt-get install -y wget && \
apt-get clean
RUN rm -rf /opt/conda && \
wget --quiet https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh -O ~/anaconda.sh && \
/bin/bash ~/anaconda.sh -b -p /opt/conda && \
rm ~/anaconda.sh && \
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
find /opt/conda/ -follow -type f -name '*.a' -delete && \
find /opt/conda/ -follow -type f -name '*.js.map' -delete && \
/opt/conda/bin/conda clean -afy
COPY ./requirements.yml /tmp/requirements.yml
RUN conda update conda \
&& conda env create -f /tmp/requirements3.yml
RUN echo "conda activate nib_docker" >> ~/.bashrc
ENV PATH /opt/conda/envs/nib_docker/bin:$PATH
ENV CONDA_DEFAULT_ENV $nib_docker
COPY . .
CMD [ "python", "-u", "train.py" ]
推荐答案
最后弄清楚了:
要创建具有相应依赖项(conda和pip)的docker环境,您可以按照以下步骤操作:
To create a docker environment with its corresponding dependencies(both conda and pip), you can follow the below steps:
创建yml文件,其中列出了conda环境的详细信息(conda和pip).您可以使用以下命令将本地计算机中的现有conda环境导出到yml文件中在这里,我要在激活后导出我的 myconda
环境
Create the yml file, which lists the conda environment details (conda and pip). You can export an existing conda environment in your local machine to a yml file using the below command
Here, I'm exporting my myconda
environment after activating it
conda activate myconda
conda env export --no-builds > env_config.yml
# --no-builds --> added to make sure that the build versions are not part of the final yml file as the build versions differ based on your base OS (Linux, Windows,macOS).
YML文件如下所示:
The YML file would look something like this:
name: myconda #rename this if you want to change the name of enviroment in docker
channels:
- pytorch
- conda-forge
- defaults
dependencies:
- pip
- conda_package1=4.0
- conda_package2=5.0
- conda_package3=6.0
- pip:
- pip_package1==6.0
- pip_package2==7.0
- pip_package3==8.0
prefix: C:\Users\ABC\.conda\envs\myconda
我更喜欢将conda和pip依赖项分成两个单独的文件,这样,如果我想更改任何依赖项,我将仅更改各自的文件,以避免在创建docker映像时再次下载所有软件包.因此,这两个文件在编辑后现在看起来像这样:
I prefer to separate my conda and pip dependencies into two separate files so that if I want to change any dependency, I'll change the respective file only to avoid download all packages again when creating the docker image. So, the two files will now look like this after editing:
channels:
- pytorch
- conda-forge
- defaults
dependencies:
- pip
- conda_package1=4.0
- conda_package2=5.0
- conda_package3=6.0
# prefix: C:\Users\ABC\.conda\envs\myconda # not needed
env_pip_config.txt
pip_package1==6.0
pip_package2==7.0
pip_package3==8.0
最后是Dockerfile:
And finally, the Dockerfile:
## BASE IMAGE
FROM python:3.8
## SET WORKING DIRECTORY
WORKDIR /app
RUN apt-get update -y \
&& apt-get install -y wget \
&& apt-get clean
## CONDA INSTALLATION --> use the latest Anaconda version for linux from their official website. Google it buddy.
RUN rm -rf /opt/conda && \
wget --quiet https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh -O ~/anaconda.sh && \
/bin/bash ~/anaconda.sh -b -p /opt/conda && \
rm ~/anaconda.sh && \
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
find /opt/conda/ -follow -type f -name '*.a' -delete && \
find /opt/conda/ -follow -type f -name '*.js.map' -delete && \
/opt/conda/bin/conda clean -afy
## ADD CONDA PATH TO LINUX PATH
ENV PATH /opt/conda/bin:$PATH
## COPY ENV REQUIREMENTS FILES
COPY ./env_config.yml /tmp/env_config.yml
COPY ./env_pip_config.txt /tmp/env_pip_config.txt
## CREATE CONDA ENVIRONMENT USING YML FILE
RUN conda update conda \
&& conda env create -f /tmp/env_config.yml
## ADD CONDA ENV PATH TO LINUX PATH
ENV PATH /opt/conda/envs/myconda/bin:$PATH
ENV CONDA_DEFAULT_ENV myconda
# make sure to put your env name in place of myconda
## MAKE ALL BELOW RUN COMMANDS USE THE NEW CONDA ENVIRONMENT
SHELL ["conda", "run", "-n", "myconda", "/bin/bash", "-c"]
## INSTALL PIP DEPENDENCIES
RUN pip install -r /tmp/env_pip_config.txt
## COPY REST OF THE FILES
COPY . .
## FINALLY TIME TO EXECUTE!
CMD ["python", "myscript.py"]
这篇关于在Docker中配置/设置/激活Python Conda环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!