简体   繁体   English

Dockerfile CMD脚本的运行方式与在容器中手动运行的脚本不同

[英]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 更新

  • Running with ENTRYPOINT instead of CMD makes no difference 使用ENTRYPOINT而不是CMD运行没有区别
  • Setting 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.

相关问题 Docker 容器中的 Python 脚本在不通过 Docker CLI 手动调用时运行良好 - Python script in Docker container runs fine when called manually not via Docker CLI 如何在 dockerfile CMD 脚本中执行 npm start - How to execute npm start within dockerfile CMD script python脚本运行后自动运行CMD:: - Automatically running CMD after python script running:: 为什么python脚本在Spyder vs cmd提示符下的工作方式不同 - Why does python script works differently in Spyder vs cmd prompt 使用cmd文件运行python脚本会导致ImportError - Running python script with cmd file results in ImportError 批处理运行python脚本..最后如何关闭cmd窗口? - Batch runs python script.. how to close cmd window at end? Python 脚本在 anaconda3 上运行时无法在 cmd 上运行 - Python script doesn't run on cmd when it runs on anaconda3 Python 脚本可以通过 cmd 正确运行,但不能通过双击运行 - Python script runs correctly via cmd but not on double-clicking Python脚本使用双击和IDLE运行,但不运行Windows CMD Shell - Python script runs with double-click and IDLE but not windows CMD shell Python 底图脚本与 Anaconda cmd 提示符一起运行,但不与 VS Code 一起运行 - Python Basemap script runs with Anaconda cmd prompt but not with VS Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM