python - 如何在 IBM Cloud 函数中运行 docker 映像?
问题描述
我有一个想要在 IBM Cloud 函数中运行的简单 Python 程序。唉,它需要两个库(O365 和 PySnow),所以我必须对它进行 Dockerize 化,并且它需要能够接受来自 STDIN 的 Json 提要。我成功地做到了:
FROM python:3
ADD requirements.txt ./
RUN pip install -r requirements.txt
ADD ./main ./main
WORKDIR /main
CMD ["python", "main.py"]
这运行:cat env_var.json | docker run -i f9bf70b8fc89
我已将 Docker 容器添加到 IBM Cloud Functions,如下所示:
ibmcloud fn action create e2t-bridge --docker [username]/e2t-bridge
但是,当我运行它时,它会超时。
现在我确实看到了一个可能的解决方案路线,我将它作为一个 Openwhisk 应用程序进行 dockerize。但是为此我需要从我的 Python 应用程序创建一个二进制文件,然后将其加载到一个相当复杂的 Openwhisk 框架中,我想?
但是拥有一个可以简单运行的文件是我的 Docker 的重点,因此创建解释语言的二进制文件然后将其添加到 Openwhisk docker 中感觉非常笨重。
解决这个问题的最佳方法是什么?
解决方案
事实证明,您不需要创建二进制文件,只需像这样编辑 OpenWhisk 框架:
# Dockerfile for example whisk docker action
FROM openwhisk/dockerskeleton
ENV FLASK_PROXY_PORT 8080
### Add source file(s)
ADD requirements.txt /action/requirements.txt
RUN cd /action; pip install -r requirements.txt
# Move the file to
ADD ./main /action
# Rename our executable Python action
ADD /main/main.py /action/exec
CMD ["/bin/bash", "-c", "cd actionProxy && python -u actionproxy.py"]
并确保您的 Python 代码接受来自标准输入的 Json 提要:
json_input = json.loads(sys.argv[1])
推荐阅读
- jupyter-notebook - nbviewer 400:错误请求,读取 JSON 笔记本时出错
- javascript - Javascript console.log(globalThis.object) 未定义,但 console.log(globalThis) 显示它存在
- java - 如果 HTTP 协议没有 EOF 字符,为什么 HttpURLConnection 中的 readline while 循环不会阻塞?
- java - 将 for 循环从 JAVA 转换为 Python 处理的问题
- xaml - 列表包括另一个列表 xamarin 表单
- flutter - 如何使用 Flutter 与 ERC721 智能合约交互?
- github - Wakatime Stats Tracker 在我的 GitHub 个人资料 README.md 中不起作用
- python - 列出具有不同结果的操作
- autofixture - 在 Autofixture 中每次测试运行时生成相同的“随机”值
- google-cloud-platform - 如何删除 gcloud auth list 中显示的帐户?