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

时间:2016-08-25 21:09:08

标签: python docker openwhisk

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

我在其他编程语言中看到过几个例子,在C和Node.js中有一个很好的black box骨架方法。但我想更多地了解OpenWhisk如何与容器交互,如果可能的话,只使用Python。

1 个答案:

答案 0 :(得分:4)

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

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

以下是摘要。我已经更详细地写了here on GitHub

程序

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

  • 确保它是可执行的(chmod a+x test.py)。
  • 确保它在第一行得到了shebang。
  • 确保它在本地运行。
    • e.g。 ./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

中列出的任何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图层,而不是在构建图像时从缓存中提取。

相关问题