[英]Dockerfile CMD script runs differently to running script manually in container
I have a dockerfile with the following end 我有一个dockerfile,具有以下结尾
CMD python app/start.py arg1 arg2 arg3
And when I try calling run
(through compose) it gives me an error from my python script: 当我尝试调用run
(通过compose)时,它从python脚本中给我一个错误:
Traceback (most recent call last):
File "app/start.py", line 4, in <module>
from utils.foo import bar
ImportError: No module named 'utils'
However, when I call run ... bash
(again through compose) or set the compose command argument to bash
, and then in the bash terminal that comes up run: 但是,当我调用run ... bash
(再次通过compose)或将compose命令参数设置为bash
,然后在出现的bash终端中运行:
python app/start.py arg1 arg2 arg3
The script runs perfectly. 该脚本运行完美。
I assume this is something to do with how folder structure is handled on startup. 我认为这与启动时如何处理文件夹结构有关。 Is there something I don't understand about how CMD
works in dockerfiles? 关于CMD
在dockerfile中的工作方式,我是否不了解?
Updates 更新
ENTRYPOINT
instead of CMD
makes no difference 使用ENTRYPOINT
而不是CMD
运行没有区别 CMD
to bash -c "python app/start.py arg1 arg2 arg3"
makes no difference 将CMD
设置为bash -c "python app/start.py arg1 arg2 arg3"
没有区别 This is definitely not the best solution, but it ended up working for me. 这绝对不是最好的解决方案,但最终为我工作。
I simply created a new top level file start.py
with: 我只是使用以下命令创建了一个新的顶级文件start.py
:
from app.start import startup
startup()
And replaced the command: 并替换为命令:
CMD python start.py arg1, arg2, arg3
And it worked fine. 而且效果很好。 I feel this somewhat confirms my suspicion that it was something to do with the file hierarchy, but as to why this occurs only when running off CMD
still has me stumped. 我觉得这在某种程度上证实了我的怀疑,这与文件层次结构有关,但是关于为什么仅在运行CMD
时才会发生这种情况,我仍然感到困惑。 Any suggestions for a better solution would be much appreciated. 任何有关更好解决方案的建议将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.