dockerfile 中创建并激活 conda 环境
ide
本文字数:793 字 | 阅读时长 ≈ 4 min

dockerfile 中创建并激活 conda 环境

ide
本文字数:793 字 | 阅读时长 ≈ 4 min

如果你创建的是深度学习镜像,建议基于nvidia/cuda进行创建,下面就是基于此镜像进行创建的

在写 Dockerfile 的时候我们希望自动安装 Miniconda,并且创建一个叫做 torch 的环境,并且安装相应的包,下面是我写的 Dockerfile 文件

1. 第一个 Dockerfile

第一个 Dockerfile 的编写如下

#!/bin/bash

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

...

RUN conda create -n torch -y python=3.8
# 用于激活环境,conda activate命令无效
RUN conda activate torch
RUN pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

...

然后出现了以下的问题,意思好像是没有初始化 conda 的 shell,一把来说我们都是用 bash,docker 初始化的时候默认是用的是 sh,所以我们加入 conda init bash 再试一次

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

2. 第二个Dockerfile

#!/bin/bash

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

...

RUN conda create -n torch -y python=3.8
# 用于激活环境,conda activate命令无效
RUN conda init bash \
    && conda activate torch
RUN pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

...

很可惜的是,依然出现了上面的错误

3. 最终的Dockerfile

如果没有办法在Dockerfile中激活环境,就没有办法安装相应的包,经过查找发现可以使用conda run -n myenv command在Dockerfile中激活conda环境,下面给出其中的问题链接

链接1
链接2

最终版本如下所示

#!/bin/bash

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

...

RUN conda create -n torch -y python=3.8
# 用于激活环境,conda activate命令无效
SHELL ["conda", "run", "-n", "ffmpeg_env", "/bin/bash", "-c"]
# 成功激活
RUN  conda activate torch
RUN pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

...

下面给出一个在dockerfile中创建conda环境的dockerfile

#!/bin/bash

FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04
MAINTAINER wangyh "1061771439@qq.com"

RUN apt-get update \
    && apt-get install -y sudo \
    && apt-get install -y wget \
    && apt-get install -y vim

RUN cd /home \
    && wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && bash Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh

ENV PATH=/root/miniconda3/bin:$PATH

RUN conda --version \
    && echo "channels:" > ~/.condarc \
    && echo " - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/" >> ~/.condarc \
    && echo " - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge" >> ~/.condarc \
    && echo " - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/" >> ~/.condarc \
    && echo " - defaults" >> ~/.condarc \
    && echo "set show_channel_urls: yes" >> ~/.condarc \
    && conda env list

ADD requirements.txt /home

SHELL ["/bin/bash", "-c"]
RUN conda create -n torch -y python=3.8
# 用于激活环境,conda activate命令无效
# 参考https://saigezixun.com/article/91/
SHELL ["conda", "run", "-n", "torch", "/bin/bash", "-c"]

Run conda list
RUN pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
4月 06, 2025
3月 10, 2025
12月 31, 2024