如何使Python Docker镜像成为OpenWhisk操作?

我有一个运行Python程序的Docker镜像。 我现在想要运行这个容器作为OpenWhisk动作。 我该怎么做呢?

我已经看到了其他编程语言中的几个例子,以及C和Node.js中的一个优秀的黑盒子骨架方法。 但我想更多地了解OpenWhisk如何与容器交互,如果可能的话,只使用Python。

现在(9月2016)比我以前的答案简单得多。

使用$ wsk sdk install docker命令创builddockerSkeleton目录之后,您只需编辑Dockerfile并确保您的Python(2.7现在)正在接受参数并以适当的格式提供输出。

这是一个总结。 我已经在GitHub上更详细地写了它

该程序

文件test.py (或您将在下面编辑的Dockerfile中使用的whatever_name.py Dockerfile )。

  • 确保它是可执行的( chmod a+x test.py )。
  • 确保它已经在第一线了。
  • 确保它在本地运行。
    • 例如./test.py '{"tart":"tarty"}'
      产生JSON字典:
      {"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

将以下内容与提供的Dockerfile进行比较,以获取Python脚本test.py并准备好构buildDockerfile镜像。

希望这些评论能够解释这些差异。 当前目录中的任何资产(数据文件或模块)将成为图像的一部分,在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图层被重新拾取,而不是在构build图像时从caching中绘制。