使用 docker-compose 启动 Jupyter Notebook 时激活 Docker 容器内的 conda env

2023-12-09

我有以下内容Dockerfile.

FROM continuumio/miniconda3:4.5.11

# create a new user (defaults to 'al-khawarizmi')
USER root
ARG username=al-khawarizmi
RUN useradd --create-home --home-dir /home/${username} ${username}
ENV HOME /home/${username}

# switch to newly created user to avoid running container as root
USER ${username}
WORKDIR $HOME

# build and activate the specified conda environment from a file (defaults to 'environment.yml')
ARG environment=environment.yml
COPY ${environment} .
RUN conda env create --file ${environment} && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \ 
    echo "conda activate $(head -1 ${environment} | cut -d' ' -f2)" >> ~/.bashrc

Dockerfile 允许用户指定 conda 环境文件作为构建参数。这将是一个典型的environment.yml file.

name: nessie-py

channels:
  - conda-forge
  - defaults

dependencies:
  - python=3.6
  - "notebook=5.7.*"
  - "matplotlib=3.0.*"
  - "numpy=1.15.*"
  - "pandas=0.23.*"

用户可以按照标准方式运行该镜像,conda环境将自动激活。跑步

$ docker run -it image_name:image_tag

在激活 conda 环境的情况下,在 Docker 容器内生成 bash 提示符。

(environment_name)$

现在我想用docker-compose在容器内启动 Jupyter 笔记本服务器(使用 conda 环境文件构建,指定 Jupyter 作为依赖项)。

当我使用以下内容时docker-compose.yml

version: "3.7"

services:
  notebook-server:
    build:
      context: ./
    ports:
      - "8888:8888"
    volumes:
      - ./:/home/al-khawarizmi
    command: jupyter notebook --no-browser ip=0.0.0.0  

我收到以下错误。

$ docker-compose up
Creating network "nessie-py_default" with the default driver
Creating nessie-py_notebook-server_1 ... done
Attaching to nessie-py_notebook-server_1
notebook-server_1  | [FATAL tini (7)] exec jupyter failed: No such file or directory
nessie-py_notebook-server_1 exited with code 127

我怀疑这个错误是指conda环境没有激活。然后我尝试添加tty: true and stdin_open: true to the docker-compose.yml认为这应该在运行之前调用交互式 bash 提示符command。这导致了与上面相同的错误。

我也尝试定义一个start-notebook.sh在运行笔记本之前显式激活 conda 环境的脚本。

#!/bin/bash
set -e

# activate the environment and start the notebook
conda activate nessie-py
jupyter notebook --no-browser ip=0.0.0.0

导致不同的错误

$ docker-compose up
Creating network "nessie-py_default" with the default driver
Creating nessie-py_notebook-server_1 ... done
Attaching to nessie-py_notebook-server_1
notebook-server_1  | 
notebook-server_1  | CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
notebook-server_1  | If your shell is Bash or a Bourne variant, enable conda for the current user with
notebook-server_1  | 
notebook-server_1  |     $ echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc
notebook-server_1  | 
notebook-server_1  | or, for all users, enable conda with
notebook-server_1  | 
notebook-server_1  |     $ sudo ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh
notebook-server_1  | 
notebook-server_1  | The options above will permanently enable the 'conda' command, but they do NOT
notebook-server_1  | put conda's base (root) environment on PATH.  To do so, run
notebook-server_1  | 
notebook-server_1  |     $ conda activate
notebook-server_1  | 
notebook-server_1  | in your terminal, or to put the base environment on PATH permanently, run
notebook-server_1  | 
notebook-server_1  |     $ echo "conda activate" >> ~/.bashrc
notebook-server_1  | 
notebook-server_1  | Previous to conda 4.4, the recommended way to activate conda was to modify PATH in
notebook-server_1  | your ~/.bashrc file.  You should manually remove the line that looks like
notebook-server_1  | 
notebook-server_1  |     export PATH="/opt/conda/bin:$PATH"
notebook-server_1  | 
notebook-server_1  | ^^^ The above line should NO LONGER be in your ~/.bashrc file! ^^^
notebook-server_1  | 
notebook-server_1  | 
nessie-py_notebook-server_1 exited with code 1

这个错误表明bash没有采购~/.bashrc在运行脚本之前。

我尝试明确采购/opt/conda/etc/profile.d/conda.sh在激活 conda 环境之前。

#!/bin/bash
set -e

# activate the environment and start the notebook
. /opt/conda/etc/profile.d/conda.sh
conda activate nessie-py
jupyter notebook --no-browser ip=0.0.0.0

这会导致不同的错误!

$ docker-compose up
Creating network "nessie-py_default" with the default driver
Creating nessie-py_notebook-server_1 ... done
Attaching to nessie-py_notebook-server_1
notebook-server_1  | Could not find conda environment: nessie-py
notebook-server_1  | You can list all discoverable environments with `conda info --envs`.
notebook-server_1  | 
nessie-py_notebook-server_1 exited with code 1

我可以通过运行来检查容器中可以发现哪些 conda env

$ docker run -it nessie-py conda info --envs

这说明环境确实存在。

$ docker run -it nessie-py_notebook-server conda info --envs
# conda environments:
#
nessie-py                /home/al-khawarizmi/.conda/envs/nessie-py
base                  *  /opt/conda

我现在没有主意了。这应该是可能的。Here是一个项目的示例docker-compose.yml文件,一个指定 conda 环境并启动 Jupyter 笔记本服务器的 Dockerfile。

我需要的额外复杂性包括向 Dockerfile 添加非 root 用户并创建新的 conda 环境,而不是更新默认环境base康达环境。


发生的情况是以下原因造成的:

  1. In the docker-compose.yml你有一个错字ip=0.0.0.0应该是--ip=0.0.0.0 instead

  2. 将主机的文件夹绑定到容器中是覆盖的.bashrc。一个简单的更改就是安装到子目录中

  3. 您需要以交互模式运行 bash (-i) 以便.bashrc被正确阅读

例如,这些点的变化反映在您的docker-compose.yml:

version: "3.7"

    services:
      notebook-server:
        build:
          context: ./
        ports:
          - "8888:8888"
        volumes:
          - ./:/home/al-khawarizmi/hosthome
        command: bash -ic 'jupyter notebook --no-browser --ip=0.0.0.0'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 docker-compose 启动 Jupyter Notebook 时激活 Docker 容器内的 conda env 的相关文章

随机推荐