【问题标题】:Configure a docker container as environment variable in another container将 docker 容器配置为另一个容器中的环境变量
【发布时间】:2020-05-28 05:48:47
【问题描述】:

我有一个执行 octave-cli 的 docker 容器,我想要将该可执行文件作为另一个容器中的环境变量,以便我可以使用 python 脚本运行它。

我在我的 python 脚本中使用 oct2py 库,它需要一个带有 octave 可执行文件的环境变量。没有需要最新版本的 octave 的 PPA,因此我找到了一个可以很好地与我的 .m 文件配合使用的 octave docker 映像(mtmiller/octave)。现在我希望它在我的 python 容器中可用,以便脚本工作。

八度脚本示例

function hello()
    printf("hello,world!")
endfunction

Python 脚本

from oct2py import octave
octave.add('path/to/octavefile')
octave.hello()

输出

hello,world!

需要在 python 脚本之上运行

您必须在 PATH 环境变量中安装 GNU Octave。或者,您可以设置指向 octave-cli 可执行文件本身的 OCTAVE_EXECUTABLE 或 OCTAVE 环境变量。

现在,我有执行 octave 的 mtmiller/octave。如何在 python 容器的 PATH 中设置它?

【问题讨论】:

  • docker run docker-image command
  • 您是否使用 docker-compose 文件,您可以使用环境:- SPARK_MODE=worker ,同时举一个您正在尝试的示例
  • @JoranBeasley Docker 在 docker 里面?有没有办法将两个容器分开处理?
  • @vinsentparamanantham 如果我使用 docker-compose,我是否可以从 python 容器运行 octave 容器,因为它们都在同一个网络上?
  • 您能否详细说明您的问题并输入一些代码或图片或您传递给程序的变量。

标签: python docker dockerfile containers octave


【解决方案1】:

我怀疑 octave 提供了一个 web api 包的东西....但我不知道,但你可以在你的 octave 服务器上运行一个轻量级的烧瓶 api 服务器

八度码头图像

octave-server.py

import flask
import subprocess
from oct2py import octave
octave.add('path/to/octavefile')

app = flask.Flask(__name__)
@app.route("hello.m")
def hello_fn_call():
    users_name = flask.request.args.get("name")
    return json.dumps(octave.hello(users_name))

if __name__ == "__main__":
    # you should really serve this with nginx + gunicorn or something
    app.run(debug=True)

python docker 镜像

octave_rest.py

import requests
def say_hello(username):
    return requests.get("http://other-image:5000/hello.m",{"name":username}).json()
if __name__ == "__main__":
    print(say_hello("BOB"))
    # something like "Hello, Bob"

如果你真的想在你的 octave 程序中打印字符串,你需要捕获标准输出,这个装饰器应该会有所帮助(你也可以手动完成所有操作)

from contextlib import redirect_stdout # py3.4+ only
import io

...

@app.route("hello.m")
def hello_fn_call():
    users_name = flask.request.args.get("name")    
    f = io.StringIO()
    with redirect_stdout(f):
        hello(users_name) # capture stdout
    return json.dumps(f.getvalue())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2016-05-30
    • 2021-08-01
    • 1970-01-01
    • 2015-09-18
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多