如何使用Jupyter Notebooks中可用的Python 2和Python 3创建docker容器?

时间:2019-10-18 01:37:11

标签: python docker jupyter-notebook anaconda

我正在尝试创建一个具有anaconda并同时支持python 2和3的Jupyter笔记本的docker容器。我创建了一个基于正式的anaconda python 3容器的容器,如下所示:

FROM continuumio/anaconda3:latest

WORKDIR /app/
COPY requirements.txt /app/

RUN pip install --upgrade pip && \
    pip install -r requirements.txt

一旦在容器上,我可以通过输入以下命令来使python 2和3与Jupyter笔记本一起使用:

conda create -y -n py2 python=2.7
conda activate py2
conda install -y notebook ipykernel
ipython kernel install --user
conda deactivate

然后,当我回到基础并运行jupyter kernelspec list时,我看到:

(base) root@1683850aacf0:/app# jupyter kernelspec list
Available kernels:
  python2    /root/.local/share/jupyter/kernels/python2
  python3    /root/.local/share/jupyter/kernels/python3

,当我打开jupyter笔记本服务器时,我同时看到了python 2和3选项。这是我想进入的状态。我试图将所有这些都变成docker命令,如下所示:

RUN    conda create -y -n py2 python=2.7
RUN    conda activate py2
RUN    conda install -y notebook ipykernel
RUN    ipython kernel install --user
RUN    conda deactivate

但是运行命令来激活或停用(RUN conda activate py2)conda环境会给我一个错误:

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

在命令前添加RUN conda init bash不会更改错误消息。

此外,基于此SO question,我尝试了:

RUN conda create -y -n py3 python=3.7 ipykernel
RUN conda create -y -n py2 python=2.7 ipykernel

但是在构建并输入容器之后,我只会看到python 3环境:

(base) root@b301d8ab5f1e:/app# jupyter kernelspec list
Available kernels:
  python3    /opt/conda/share/jupyter/kernels/python3

我可以激活py2并看到该内核,但不能同时看到这两个内核:

(py2) root@b301d8ab5f1e:/app# jupyter kernelspec list
Available kernels:
  python2    /opt/conda/envs/py2/share/jupyter/kernels/python2

我还应该尝试什么?

编辑:

我尝试按照以下建议将外壳指定为Adiii:

FROM continuumio/anaconda3:latest

WORKDIR /app/
COPY requirements.txt /app/

RUN pip install --upgrade pip && \
    pip install -r requirements.txt

ENV BASH_ENV ~/.bashrc
SHELL ["/bin/bash", "-c"]
RUN    conda create -y -n py2 python=2.7
RUN    conda activate py2
RUN    conda install -y notebook ipykernel
RUN    ipython kernel install --user
RUN    conda deactivate

这允许容器构建,但是由于某种原因,没有python 2.7环境:

(base) root@31169f698f14:/app# jupyter kernelspec list
Available kernels:
  python3    /root/.local/share/jupyter/kernels/python3
(base) root@31169f698f14:/app# conda info --envs
# conda environments:
#
base                  *  /opt/conda
py2                      /opt/conda/envs/py2

(base) root@31169f698f14:/app# conda activate py2
(py2) root@31169f698f14:/app# jupyter kernelspec list
Available kernels:
  python3    /root/.local/share/jupyter/kernels/python3

2 个答案:

答案 0 :(得分:0)

在此issue中,您需要在Dockerfile中分隔SHELL指令,例如SHELL ["/bin/bash", "-c"]。问题可能是由于RUN命令中的默认Shell为sh

  

这与上面的解决方案相似,但是避免了某些   每个RUN命令中的样板:

ENV BASH_ENV ~/.bashrc
SHELL ["/bin/bash", "-c"]

那么类似的东西应该可以按预期工作:

RUN conda activate my-env && conda info --envs
  

或者,永久设置环境(包括用于交互式   壳),您可以:

RUN echo "conda activate my-env" >> ~/.bashrc

Dockerfile

FROM continuumio/anaconda3:latest

WORKDIR /app/

RUN pip install --upgrade pip
ENV BASH_ENV ~/.bashrc
SHELL ["/bin/bash", "-c"]
RUN    conda create -y -n py2 python=2.7
RUN    conda activate py2
RUN    conda install -y notebook ipykernel
RUN    ipython kernel install --user
RUN    conda deactivate

答案 1 :(得分:0)

这就是最终的结果:

RUN conda env create -f py2_env.yaml
RUN conda env create -f py3_env.yaml
RUN /bin/bash -c "conda init bash && source /root/.bashrc && conda activate py2 && conda install -y notebook ipykernel && ipython kernel install --user && conda deactivate"
RUN /bin/bash -c "conda init bash && source /root/.bashrc && conda activate py3 && conda install -y notebook ipykernel && ipython kernel install --user && conda deactivate"