栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Dockerfile中创建并激活conda环境

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Dockerfile中创建并激活conda环境

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

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

第一个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 

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'.
第二个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

...

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

最终的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

...
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/693580.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号