5

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

我见过其他编程语言中的几个示例,以及 C 和 Node.js 中出色的黑盒框架方法。但我想更多地了解 OpenWhisk 如何与容器交互,如果可能的话,只使用 Python。

4

1 回答 1

6

现在(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 映像。

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

于 2016-08-25T21:09:08.473 回答