如果你创建的是深度学习镜像,建议基于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 <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'.
第二个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环境,下面给出其中的问题链接
最终版本如下所示
#!/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
...