且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Docker - 如何通过API将输入发送到程序

更新时间:2022-10-29 23:10:42

事实上,您可以使用 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.