【问题标题】:How do I make a Python Docker image an OpenWhisk action?如何使 Python Docker 映像成为 OpenWhisk 操作?
【发布时间】:2017-01-02 10:41:34
【问题描述】:

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

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

【问题讨论】:

    标签: python docker openwhisk


    【解决方案1】:

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

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

    这里是一个摘要。我写的比较详细here on 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 映像。

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

    【讨论】:

    • 归结为,容器需要在端口 8080 上提供一个 REST API,其中有两个端点 /init/run,一旦你有机会设置你的函数,init 就会运行你需要的任何东西下载,或任何你想设置的全局状态,然后/run 将运行很多次而不运行`/init' 这就是我们所说的暖种子它容器这是另一个例子github.com/openwhisk/openwhisk/tree/master/core/pythonAction
    • @csantanapr 我根据您的输入更新了关于/init 的信息。非常感谢。
    • 我们上面的 cmets 与原始答案相关,该答案已被更简单的 2016 年 9 月 Openwhisk 无效。
    猜你喜欢
    • 2021-02-27
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多