【发布时间】:2017-01-02 10:41:34
【问题描述】:
我有一个运行 Python 程序的 Docker 映像。我现在想将此容器作为 OpenWhisk 操作运行。我该怎么做呢?
我见过其他编程语言中的几个示例,以及 C 和 Node.js 中出色的 black box 骨架方法。但我想了解更多关于 OpenWhisk 如何与容器交互的信息,如果可能的话,只使用 Python。
【问题讨论】:
我有一个运行 Python 程序的 Docker 映像。我现在想将此容器作为 OpenWhisk 操作运行。我该怎么做呢?
我见过其他编程语言中的几个示例,以及 C 和 Node.js 中出色的 black box 骨架方法。但我想了解更多关于 OpenWhisk 如何与容器交互的信息,如果可能的话,只使用 Python。
【问题讨论】:
现在(2016 年 9 月)比我之前的回答要简单得多。
使用命令$ wsk sdk install docker 创建dockerSkeleton 目录后,您所要做的就是编辑Dockerfile 并确保您的Python(现在是2.7)接受参数并以适当的格式提供输出。
这里是一个摘要。我写的比较详细here on GitHub
文件test.py(或whatever_name.py,您将在下面编辑的Dockerfile中使用。)
chmod a+x test.py)。./test.py '{"tart":"tarty"}'{"allparams": {"tart": "tarty", "myparam": "myparam default"}}
#!/usr/bin/env python
import sys
import json
def main():
# accept multiple '--param's
params = json.loads(sys.argv[1])
# find 'myparam' if supplied on invocation
myparam = params.get('myparam', 'myparam default')
# add or update 'myparam' with default or
# what we were invoked with as a quoted string
params['myparam'] = '{}'.format(myparam)
# output result of this action
print(json.dumps({ 'allparams' : params}))
if __name__ == "__main__":
main()
将以下内容与提供的 Dockerfile 进行比较,以获取 Python 脚本 test.py 并准备好构建 docker 映像。
希望 cmets 解释这些差异。当前目录中的任何资产(数据文件或模块)都将成为图像的一部分,requirements.txt 中列出的任何 Python 依赖项也将成为图像的一部分
# Dockerfile for Python whisk docker action
FROM openwhisk/dockerskeleton
ENV FLASK_PROXY_PORT 8080
# Install our action's Python dependencies
ADD requirements.txt /action/requirements.txt
RUN cd /action; pip install -r requirements.txt
# Ensure source assets are not drawn from the cache
# after this date
ENV REFRESHED_AT 2016-09-05T13:59:39Z
# Add all source assets
ADD . /action
# Rename our executable Python action
ADD test.py /action/exec
# Leave CMD as is for Openwhisk
CMD ["/bin/bash", "-c", "cd actionProxy && python -u actionproxy.py"]
注意ENV REFRESHED_AT ...,我用它来确保重新拾取更新的test.py 层,而不是在构建图像时从缓存中提取。
【讨论】:
/init 和 /run,一旦你有机会设置你的函数,init 就会运行你需要的任何东西下载,或任何你想设置的全局状态,然后/run 将运行很多次而不运行`/init' 这就是我们所说的暖种子它容器这是另一个例子github.com/openwhisk/openwhisk/tree/master/core/pythonAction
/init 的信息。非常感谢。