[英]How to Start a Conda Environment with Docker Container
I am a docker
novice, so apologies if this is a silly question.我是docker
新手,如果这是一个愚蠢的问题,我深表歉意。
As background, I am using a docker image that I have no ability to edit or change.作为背景,我正在使用无法编辑或更改的 docker 图像。 I run the container with docker run [various-args] [image-name]
and the container is launched.我使用docker run [various-args] [image-name]
容器并启动容器。 If I subsequently run docker exec -it [ID] bash
, I can get a shell going from inside the container and it successfully executes a conda
environment needed for all my python code, so I can just run python script.py
and everything runs okay. If I subsequently run docker exec -it [ID] bash
, I can get a shell going from inside the container and it successfully executes a conda
environment needed for all my python code, so I can just run python script.py
and everything runs okay .
I wanted to automate this process for future use, so I wanted to put these commands into a single script so that I don't need to manually type or execute anything from within the environment.我想自动化这个过程以备将来使用,所以我想把这些命令放到一个脚本中,这样我就不需要在环境中手动输入或执行任何东西。 The solution I thought would work was this:我认为可行的解决方案是:
docker exec -it [ID] bash -c "python script.py"
but this doesn't work, giving an import error for the python code.但这不起作用,导致 python 代码的导入错误。 My assumption is that the conda environment is not activated, so I try to execute conda activate my-env
, which kicks back a new error of:我的假设是 conda 环境没有被激活,所以我尝试执行conda activate my-env
,这会返回一个新的错误:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
So, I follow the instructions and run conda init bash
first to see if that helps, but this error still kicks back eventually.所以,我按照说明先运行conda init bash
看看是否有帮助,但这个错误最终还是会出现。 Ultimately, it seems like if I execute a bash shell first, then manually start running python code everything is fine, but if I try to do it all at once the conda environment cannot be set up for somme reason.最终,如果我先执行 bash shell,然后手动开始运行 python 代码,一切都很好,但如果我尝试立即执行所有操作,原因是 conda 无法立即设置。 Is there a way to make this work without editing the image itself, or is this something that would require rebuilding the image?有没有办法在不编辑图像本身的情况下完成这项工作,或者这是否需要重建图像?
Thanks ins advance!提前感谢ins!
The conda activate
function is defined by code added to .bashrc
by the conda init
command. conda conda activate
function 由 conda conda init
命令添加到.bashrc
的代码定义。 Bash will not source .bashrc
unless the -l
( --login
) flag is used.除非使用-l
( --login
) 标志,否则 Bash 不会获取.bashrc
。
However, rather than bothering with shell, Conda provides a conda run
command that executes within a specified environment.但是,Conda 并没有打扰 shell,而是提供了一个在指定环境中执行的 conda conda run
命令。 So, try something like所以,尝试类似的东西
docker exec -it [ID] conda run -n my-env python script.py
For interactive scripts, one may also need some of the additional conda run
flags, such as --live-stream
or --no-capture-output
.对于交互式脚本,可能还需要一些额外的conda run
标志,例如--live-stream
或--no-capture-output
。 See conda run -h
.请参阅conda run -h
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.