我有一个运行 Python 程序的 Docker 映像。我现在想将此容器作为 OpenWhisk 操作运行。我该怎么做呢?
我见过其他编程语言中的几个示例,以及 C 和 Node.js 中出色的黑盒框架方法。但我想更多地了解 OpenWhisk 如何与容器交互,如果可能的话,只使用 Python。
现在(2016 年 9 月)比我之前的回答要简单得多。
使用命令创建dockerSkeleton
目录后,$ wsk sdk install docker
您所要做的就是编辑Dockerfile
并确保您的 Python(现在为 2.7)接受参数并以适当的格式提供输出。
这是一个摘要。我已经在 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 映像。
希望评论能解释这些差异。当前目录中的任何资产(数据文件或模块)都将成为图像的一部分,其中列出的任何 Python 依赖项也将成为图像的一部分requirements.txt
# 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
重新拾取更新的图层,而不是在构建图像时从缓存中绘制。