我正在尝试创建一个带有miniconda3的docker映像。我不想直接使用docker hub中提供的基本映像,而是想从头开始,创建自己的Dockerfile,并将continuumio/miniconda3映像的Dockerfle命令放在那里,它们是:
FROM debian:latest
# $ docker build . -t continuumio/miniconda3:latest -t continuumio/miniconda3:4.5.11
# $ docker run --rm -it continuumio/miniconda3:latest /bin/bash
# $ docker push continuumio/miniconda3:latest
# $ docker push continuumio/miniconda3:4.5.11
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH /opt/conda/bin:$PATH
RUN apt-get update --fix-missing &&
apt-get install -y wget bzip2 ca-certificates curl git &&
apt-get clean &&
rm -rf /var/lib/apt/lists/*
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh &&
/bin/bash ~/miniconda.sh -b -p /opt/conda &&
rm ~/miniconda.sh &&
/opt/conda/bin/conda clean -tipsy &&
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh &&
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc &&
echo "conda activate base" >> ~/.bashrc
ENV TINI_VERSION v0.16.1
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/bin/tini
RUN chmod +x /usr/bin/tini
ENTRYPOINT [ "/usr/bin/tini", "--" ]
CMD [ "/bin/bash" ]
构建和运行容器工作正常。作为参考,这里是容器中conda info
的输出:
(base) root@def48bd1ed5d:/# conda info
active environment : base
active env location : /opt/conda
shell level : 1
user config file : /root/.condarc
populated config files :
conda version : 4.5.11
conda-build version : not installed
python version : 3.7.0.final.0
base environment : /opt/conda (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/linux-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/pro/linux-64
https://repo.anaconda.com/pkgs/pro/noarch
package cache : /opt/conda/pkgs
/root/.conda/pkgs
envs directories : /opt/conda/envs
/root/.conda/envs
platform : linux-64
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Linux/4.4.0-187-generic debian/10 glibc/2.28
UID:GID : 0:0
netrc file : None
offline mode : False
每当我尝试使用conda
安装模块时,就会出现这个问题,例如conda install jupyter -y
。这个过程开始了,在安装过程中的某个时刻,我得到了这个错误:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Traceback (most recent call last):
File "/opt/conda/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
(base) root@def48bd1ed5d:/#
在这之后,安装似乎被破坏了。如果我再次尝试使用conda
命令调用exmapleconda info
,我会得到相同的错误:
(base) root@def48bd1ed5d:/# conda info
Traceback (most recent call last):
File "/opt/conda/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
(base) root@def48bd1ed5d:/#
miniconda3
版本不是最新版本您用来构建本地映像的Dockerfile将安装miniconda3-4.5.11
,而不是最新版本。你可以在这里找到:
...
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh &&
/bin/bash ~/miniconda.sh -b -p /opt/conda &&
...
同样以这种方式与docker
:
$ docker build --tag miniconda3:test .
$ docker docker run -i -t miniconda3:test /bin/bash
$ docker history --no-trunc miniconda3:test | grep Miniconda3
/bin/sh -c wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh && ...
好的,现在让我们看看官方continuumio/miniconda3
:
$ docker run -i -t continuumio/miniconda3 /bin/bash
然后:
$ docker history --no-trunc continuumio/miniconda3 | grep Miniconda3
/bin/sh -c wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && ...
正如您所看到的,DockerHub的continuumio/miniconda3
镜像安装了最新的miniconda3
4.8.2
,而不是4.5.11
版本。因此,从该Dockerfile
构建的本地映像将生成具有miniconda3:4.5.11
的容器。
python
版本的更改打破了conda
现在,让我们来弄清楚conda
失败的原因。首次构建并运行:
$ docker build --tag miniconda3:test .
$ docker docker run -i -t miniconda3:test /bin/bash
获取一些信息:
(base) root@61cafd17d954:/# conda info
active environment : base
active env location : /opt/conda
shell level : 1
user config file : /root/.condarc
populated config files :
conda version : 4.5.11
conda-build version : not installed
python version : 3.7.0.final.0
base environment : /opt/conda (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/linux-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/pro/linux-64
https://repo.anaconda.com/pkgs/pro/noarch
package cache : /opt/conda/pkgs
/root/.conda/pkgs
envs directories : /opt/conda/envs
/root/.conda/envs
platform : linux-64
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Linux/5.4.0-48-generic debian/10 glibc/2.28
UID:GID : 0:0
netrc file : None
offline mode : False
我们有conda:4.5.11
和python:3.7.0
。
现在,我们将安装jupyter
,例如:
(base) root@61cafd17d954:/# conda install jupyter
您可能会注意到,此安装将更新python
:
The following packages will be UPDATED:
...
python: 3.7.0-hc3d631a_0 --> 3.8.5-h7579374_1
...
如果继续,这将更新python
并破坏conda
:
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Traceback (most recent call last):
File "/opt/conda/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
这是一个众所周知的问题,您可以在"如何使用conda安装包来更改我的python版本并删除conda?"中找到有关此问题的更多信息?答案,在官方的conda repo中:没有名为conda.cli的模块。
更新conda
或将Dockerfile
用于miniconda3:latest
这个问题有三种可能的解决方案:
- 通过替换此行来编辑
Dockerfile
:
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh &&
带有
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh &&
使用
ContinuumIO
的最新官方Dockerfile
,您可以在此处找到它。使用前更新容器内的
conda
:
(base) root@61cafd17d954:/# conda update conda