condas `source activate virtualenv` 在 Dockerfile 中不起作用

编程入门 行业动态 更新时间:2024-10-09 00:42:54
本文介绍了condas `source activate virtualenv` 在 Dockerfile 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试基于公众 continuumio/anaconda3 容器.

I'm trying to setup a simple docker image (I'm quite new to docker, so please correct my possible misconceptions) based on the public continuumio/anaconda3 container.

Dockerfile:

FROM continuumio/anaconda3:latest # update conda and setup environment RUN conda update conda -y && conda env list && conda create -n testenv pip -y && source activate testenv && conda env list

通过 docker build -t test 构建和映像. 以错误结束:

/bin/sh: 1: source: not found

在激活新的虚拟环境时.

when activating the new virtual environment.

按照这个答案我试过了:

FROM continuumio/anaconda3:latest # update conda and setup environment RUN conda update conda -y && conda env list && conda create -y -n testenv pip && /bin/bash -c "source activate testenv" && conda env list

这似乎一开始有效,因为它输出:prepending/opt/conda/envs/testenv/bin to PATH,但是 conda env list 以及 ass echo $PATH 清楚地表明它没有:

This seems to work at first, as it outputs: prepending /opt/conda/envs/testenv/bin to PATH, but conda env list as well ass echo $PATH clearly show that it doesn't:

[...] # conda environments: # testenv /opt/conda/envs/testenv root * /opt/conda ---> 80a77e55a11f Removing intermediate container 33982c006f94 Step 3 : RUN echo $PATH ---> Running in a30bb3706731 /opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

docker 文件作为 MWE 开箱即用.我很欣赏任何想法.谢谢!

The docker files work out of the box as a MWE. I appreciate any ideas. Thanks!

推荐答案

方法一:使用自定义入口点脚本SHELL

我开发了一种新的、改进的方法,它比 conda"、run" 语法更好.

Method 1: use SHELL with a custom entrypoint script

I have developed a new, improved approach which better than the "conda", "run" syntax.

此 gist 提供的示例 dockerfile.它的工作原理是利用自定义入口点脚本在 exec 执行 RUN 节的参数之前设置环境.

Sample dockerfile available at this gist. It works by leveraging a custom entrypoint script to set up the environment before execing the arguments of the RUN stanza.

shell 是(简单地说)一个可以作为任意程序入口点的进程.exec "$@" 允许我们启动一个新进程,继承父进程的所有环境.在这种情况下,这意味着我们激活 conda(它基本上会破坏一堆环境变量),然后运行 ​​/bin/bash -c CONTENTS_OF_DOCKER_RUN.

A shell is (put very simply) a process which can act as an entrypoint for arbitrary programs. exec "$@" allows us to launch a new process, inheriting all of the environment of the parent process. In this case, this means we activate conda (which basically mangles a bunch of environment variables), then run /bin/bash -c CONTENTS_OF_DOCKER_RUN.

这是我之前的方法,由 Itamar Turner-Trauring 提供;非常感谢他们!

Here is my previous approach, courtesy of Itamar Turner-Trauring; many thanks to them!

# Create the environment: COPY environment.yml . RUN conda env create -f environment.yml # Set the default docker build shell to run as the conda wrapped process SHELL ["conda", "run", "-n", "vigilant_detect", "/bin/bash", "-c"] # Set your entrypoint to use the conda environment as well ENTRYPOINT ["conda", "run", "-n", "myenv", "python", "run.py"]

修改 ENV 可能不是最好的方法,因为 conda 喜欢自己控制环境变量.此外,您的自定义 conda env 可能会激活其他脚本以进一步调整环境.

Modifying ENV may not be the best approach since conda likes to take control of environment variables itself. Additionally, your custom conda env may activate other scripts to further modulate the environment.

这利用 conda run 以将条目添加到环境的 PATH 并运行环境可能包含的任何激活脚本"在启动新的 bash shell 之前.

This leverages conda run to "add entries to PATH for the environment and run any activation scripts that the environment may contain" before starting the new bash shell.

使用 conda 可能是一种令人沮丧的体验,因为这两种工具都想有效地垄断环境,从理论上讲,您永远不需要在容器中使用 conda.但是最后期限和技术债务是一个问题,有时你只需要完成它,有时 conda 是提供依赖项的最简单方法(看看你,GDAL).

Using conda can be a frustrating experience, since both tools effectively want to monopolize the environment, and theoretically, you shouldn't ever need conda inside a container. But deadlines and technical debt being a thing, sometimes you just gotta get it done, and sometimes conda is the easiest way to provision dependencies (looking at you, GDAL).

更多推荐

condas `source activate virtualenv` 在 Dockerfile 中不起作用

本文发布于:2023-11-25 11:51:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   source   condas   activate   Dockerfile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!