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

iai*_*inH 4 python docker openwhisk

我有一个运行Python程序的Docker映像。我现在想将此容器作为OpenWhisk操作运行。我该怎么做呢?

我已经看到了其他编程语言中的几个示例,以及C和Node.js中出色的黑盒框架方法。但我想了解更多有关OpenWhisk如何与容器进行交互的信息,如果可能的话,请仅使用Python。

iai*_*inH 5

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

dockerSkeleton使用命令创建目录后,$ wsk sdk install docker您要做的就是编辑,Dockerfile并确保您的Python(现在为2.7)接受参数并以适当的格式提供输出。

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

该程序

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

  • 确保它是可执行文件(chmod a+x test.py)。
  • 确保第一行上有shebang。
  • 确保它在本地运行。
    • 例如./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并准备好构建docker映像。

希望这些评论可以解释这些差异。当前目录中的任何资产(数据文件或模块)都将成为映像的一部分,而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"]
Run Code Online (Sandbox Code Playgroud)

注意ENV REFRESHED_AT ...,我用来确保test.py在构建图像时重新拾取更新的图层,而不是从缓存中绘制该图层。