简体   繁体   English

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

[英]Activating a conda env inside a Docker container when using docker-compose to start Jupyter notebook

I have the following Dockerfile .我有以下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

The Dockerfile allows the user to specify a conda environment file as a build arg. Dockerfile 允许用户将 conda 环境文件指定为构建参数。 Here would be a typical environment.yml file .这是一个典型的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.*"

The user can run the image in the standard way and the conda environment will be automatically activated.用户可以按照标准方式运行镜像,conda 环境会自动激活。 Running跑步

$ docker run -it image_name:image_tag

yields a bash prompt within the Docker container with the conda environment activated.在激活 conda 环境的 Docker 容器中产生一个 bash 提示。

(environment_name)$

Now I would like to use docker-compose to start a Jupyter notebook server within the container (built with a conda environment file specifying Jupyter as a dependency).现在我想使用 docker docker-compose在容器内启动 Jupyter notebook 服务器(使用 conda 环境文件构建,指定 Jupyter 作为依赖项)。

When I use the following docker-compose.yml当我使用以下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  

I get the following error.我收到以下错误。

$ 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

I suspected that this error meant that the conda environment is not activated.我怀疑这个错误意味着没有激活conda环境。 I then tried adding tty: true and stdin_open: true to the docker-compose.yml thinking that this should invoke and interactive bash prompt prior to running the command .然后我尝试将tty: truestdin_open: truedocker-compose.yml认为这应该在运行command之前调用和交互式 bash 提示。 This resulted in the same error as above.这导致了与上述相同的错误。

I also tried defining a start-notebook.sh script that explicitly activates the conda environment prior to running the notebook.我还尝试定义一个start-notebook.sh脚本,该脚本在运行笔记本之前明确激活start-notebook.sh环境。

#!/bin/bash
set -e

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

results in a different error导致不同的错误

$ 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

This error suggests that bash is not sourcing ~/.bashrc prior to running the script.此错误表明bash在运行脚本之前没有获取~/.bashrc

I tried explicitly sourcing /opt/conda/etc/profile.d/conda.sh prior to activating the conda environment.在激活/opt/conda/etc/profile.d/conda.sh环境之前,我尝试明确采购/opt/conda/etc/profile.d/conda.sh

#!/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

which results in a different error!这会导致不同的错误!

$ 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

I can check to see which conda envs are discoverable in the container by running我可以通过运行来检查容器中可以发现哪些 conda envs

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

which says that the environment does indeed exist.这说明环境确实存在。

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

I am out of ideas at this point.我现在没有想法。 This should be possible.这应该是可能的。 Here is an example of a project with a docker-compose.yml file, a Dockerfile that specifies a conda environment and starts a Jupyter notebook server. 是一个带有docker-compose.yml文件的项目示例,该文件指定 conda 环境并启动 Jupyter Notebook 服务器。

The additional complexities that I need include adding a non-root user to the Dockerfile and creating a new conda environment instead of updating the default base conda environment.我需要的额外复杂性包括将非 root 用户添加到 Dockerfile 并创建新的 conda 环境,而不是更新默认的base conda 环境。

What happens is consequence of:发生的后果是:

  1. In the docker-compose.yml you've a typo in ip=0.0.0.0 which should be --ip=0.0.0.0 insteaddocker-compose.yml您在ip=0.0.0.0有一个错字,应该是--ip=0.0.0.0

  2. Binding the host's folder into the container is overriding .bashrc .将主机的文件夹绑定到容器是覆盖.bashrc An easy change would be mounting into a subdirectory一个简单的更改是挂载到子目录中

  3. You need to run bash in interactive mode ( -i ) so that .bashrc is properly read您需要以交互模式( -i )运行 bash 以便正确读取.bashrc

As an example, changes on these points reflected in your docker-compose.yml :例如,这些点的更改反映在您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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用 docker-compose 'command:' 运行时,Supervisord 在 Docker 容器内退出,但从 bash 运行时不会退出 - Supervisord exits inside Docker container when run with docker-compose 'command:', but not when its run from bash 在虚拟环境中安装 ipykernel 并运行 jupyter notebook - 不使用 conda - Installing a ipykernel and running jupyter notebook inside a virtual env - not using conda 本地 Jupyter 笔记本中的 SageMaker:无法使用 AWS 托管的 XGBoost 容器(“KeyError:'S3DistributionType'”和“运行失败:['docker-compose'”) - SageMaker in local Jupyter notebook: cannot use AWS hosted XGBoost container (“KeyError: 'S3DistributionType'” and “Failed to run: ['docker-compose'”) docker-compose exec导致[Errno 2]没有此类文件或目录:docker容器中的'docker-compose':'docker-compose' - docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container 使用docker-compose up与docker-compose run从容器内部执行容器中的命令 - executing commands in containers from within a container using docker-compose up vs docker-compose run 在给定的 docker-compose 容器中执行 Python 脚本 - Execute Python script inside a given docker-compose container 将环境变量传递给 docker-compose - Pass env variable to docker-compose 无法访问 docker 容器内的 jupyter 笔记本 - Can't access jupyter notebook inside docker container 使用docker-compose时调试链接的docker容器 - Debugging linked docker containers when using docker-compose 从 docker-compose 运行时容器以代码 0 退出 - Container exited with code 0 when run from docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM