Docker - 如何通过API将输入发送到程序 [英] Docker - how to send input to a program via the API

查看:224
本文介绍了Docker - 如何通过API将输入发送到程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Docker的远程API来创建一个容器,在其中运行一个Python程序,附加到容器并将写入stdout的输出流传输到Web上。



现在,我想让我的Python程序接受来自stdin的用户输入。例如。

  import sys 
name = sys.stdin.readline()
print你的名字是: + name

如何将用户输入传递给通过API在Docker容器内运行的Python程序?我没有看到任何API端点,这将允许我将输入传递到在docker容器内运行的进程。



谢谢。

解决方案

事实上,您可以使用 docker attach 附加容器的标准。例如:



启动收听容器:

  docker run -i -t ubuntu:precise / bin / bash -c'读取FOO; echo $ FOO;'

然后在另一个终端:

 #lookup container id 
docker ps

#附加容器
docker attach 91495c6374b1

您现在已经挂钩到收听容器的标签,可以输入东西,一切都可以正常工作。



关于使用远程API ...这可能是使用 / containers / {id} / attach 端点与 stdin = 1 可能 stream = 1 。不能让它工作,仍然在理解这个背后的代码,但从我可以看到在实现这一定是可能的。


I have been using Docker's remote API to create a container, run a Python program in it, attach to the container and stream the output written to stdout to the web.

Now, I wanted my Python program to accept user input from stdin. E.g.

import sys
name = sys.stdin.readline()
print "Your name is: " + name

How do I pass the user input to this Python program running inside a Docker container via the API? I don't see any API end-points which will allow me to pass "input" to a process running inside a docker container.

Thanks.

解决方案

You can in fact attach the stdin of a container using docker attach. Example:

Start a "listening" container:

docker run -i -t ubuntu:precise /bin/bash -c 'read FOO; echo $FOO;'

Then in another terminal:

# lookup container id
docker ps

# attach the container
docker attach 91495c6374b1

You are now hooked to the listening container's stdin and can type thing and everything should work.

As to doing that using the remote API... I think this is possible using the /containers/{id}/attach endpoint with stdin=1 and possibly stream=1. Couldn't get it to work and still working on understanding the go code behind this, but from what I can see in the implementation this should definitely be possible.

这篇关于Docker - 如何通过API将输入发送到程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆