无法在dockerfile中创建conda env [英] Cant create conda env in dockerfile

查看:382
本文介绍了无法在dockerfile中创建conda env的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序文件夹中有一个environment.yml

I have an environment.yml in my applications folder

我的dockerfile中有这个

I have this in my dockerfile:

RUN conda env create
RUN source activate myenvfromymlfile

尽管未激活环境,但运行容器时.如果我do conda env list是否看到/opt/conda已激活:

When I run the container though the env is not activated. If I do conda env list Is see /opt/conda is activated:

root@9c7181cf86aa:/app# conda env list
# conda environments:
#
myenvfromymlfile         /opt/conda/envs/myenvfromymlfile
root                  *  /opt/conda

如果我连接到容器,则可以手动运行source activate myenvfromymlfile,它可以工作,但是为什么在RUN指令中不起作用?

If I attach to the container I can manually run source activate myenvfromymlfile and it works, but why doesn't that work in the RUN directive??

在示例中,我经常在需要conda的dockerfile中看到这种情况:

In examples, I see this often in dockerfiles that require conda:

CMD [ "source activate your-environment && exec python application.py" ]

有人可以解释为什么必须使用&&使它成为一个命令?为什么在RUN指令中运行源激活"无效?我想让我的dockerfile看起来像这样:

Can someone explain why it is necessary to use && to make it a single command? And why running "source activate" in a RUN directive does not work? I want to have my dockerfile look like this:

RUN conda env create
RUN source activate myenvfromymlfile
ENTRYPOINT ["python"]
CMD ["application.py"]

推荐答案

考虑以下Dockerfile

Consider the below Dockerfile

RUN conda env create
RUN source activate myenvfromymlfile
ENTRYPOINT ["python"]
CMD ["application.py"]

声明#1 conda env create.创建环境并更改磁盘上的文件.

Statement #1 conda env create. Create the environment and changes files on the disk.

声明#2 source activate myenvfromymlfile.在bash会话中加载一些东西.这里没有磁盘更改

Statement #2 source activate myenvfromymlfile. Loads some stuff in the bash sessions. No disk changes done here

声明#3和#4指定运行容器时发生的情况

Statement #3 and #4 specifies what happens when you run the container

ENTRYPOINT ["python"]
CMD ["application.py"]

现在,当您运行容器时.您在步骤2中所做的任何操作都不存在,因为启动了外壳程序以运行步骤2,当外壳完成时,外壳已关闭.现在,当您运行映像时,将启动一个新的shell,并且它是全新的shell,现在您不知道过去在您的dockerfile中运行了source activate myenvfromymlfile

So now when you run the container. Anything that you did in step#2 is not there, because a shell was launched to run step #2, when it completed the shell was closed. Now when you run the image a new shell is started and it is brand new shell with now no knowledge that in past inside your dockerfile you ran source activate myenvfromymlfile

现在,您要在创建的环境中运行此application.py. docker的默认外壳为sh -c.因此,当您如下设置CMD

Now you want to run this application.py in the environment you created. The default shell of docker is sh -c. So when you set CMD as below

CMD [ "source activate your-environment && exec python application.py" ]

在容器开头执行的最终命令变为

The final command executed at start of container becomes

sh -c "source activate your-environment && exec python application.py"

在当前shell中激活环境,然后运行程序.

Which activates the environment in current shell and then runs your program.

这篇关于无法在dockerfile中创建conda env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆