【发布时间】:2020-07-07 17:59:13
【问题描述】:
我正在处理一个 django docker 映像,我想使用命令行执行一个函数,我知道我可以进入 shell python docker 容器并运行这样的函数:
-
docker container ls查找容器 id 并在下一条命令中使用 -
docker container exec -it <container_id> python3 manage.py shell然后:
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from xx import foo
>>> foo() # foo() has been excecuted
但是,我想知道我是否可以使用类似 python 标志 -c 的东西来帮助运行一小段 python 代码作为这样的命令行:
-
python3 -c "from xx import foo; foo()"
我尝试使用 linux 管插入 cmd:
echo 'from xx import foo; foo()' | docker container exec -it <container_id> python3 manage.py shell,但是不行。
非常感谢您的建议,感谢您的宝贵时间。
【问题讨论】:
-
您可以从自定义管理命令中调用该函数吗? docs.djangoproject.com/en/3.0/howto/custom-management-commands
标签: python django linux docker