如何发送到docker-py容器的stdin? [英] How to send to stdin of a docker-py container?

查看:50
本文介绍了如何发送到docker-py容器的stdin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个shell示例:

  echo"hello" |docker运行--rm -ti -a stdin busybox \/bin/sh -c"cat->/out" 

这将执行一个busybox容器并创建一个新文件 /out ,其内容为 hello .

我将如何使用docker-py完成此操作?

等效于 docker-py

  container = docker_client.create_container('busybox',stdin_open =真,命令='sh -c猫->/out"')docker_client.start(容器) 

stdin_open = True ,但是我在哪里写 'hello' ?

解决方案

当时无法将stdin附加到正在运行的容器.这已经改变了.

使用当前版本的docker-py,现在可以实现这种方式(aka slix的解决方法).摘自 GitHub 上的讨论,该讨论主要针对python 2.7./p>

在docker-py版本3.1.1的python 3中查看此示例

 导入docker,tarfile从io导入BytesIOdef test_send_data_via_stdin_into_container():客户端= docker.APIClient()#创建容器容器= client.create_container("busybox",stdin_open =真,命令='sh -c"cat->/received.txt"')client.start(容器)#将stdin附加到容器并发送数据original_text_to_send ='你好,这是从另一面's = client.attach_socket(container,params = {'stdin':1,'stream':1})s._sock.send(original_text_to_send.encode('utf-8'))s.close()#停止容器并从测试文件中收集数据client.stop(容器)client.wait(容器)raw_stream,状态= client.get_archive(容器,'/received.txt')tar_archive = BytesIO(b".join(((i表示我在raw_stream中的)))t = tarfile.open(mode ='r:',fileobj = tar_archive)text_from_container_file = t.extractfile('received.txt').read().decode('utf-8')client.remove_container(容器)#检查是否相等断言text_from_container_file == original_text_to_send如果__name__ =='__main__':test_send_data_via_stdin_into_container() 

Considering this shell example:

echo "hello" | docker run --rm -ti  -a stdin busybox \
    /bin/sh -c "cat - >/out"

This will execute a busybox container and create a new file /out with the contents hello.

How would I accomplish this with docker-py ?

The docker-py equivalent:

container = docker_client.create_container( 'busybox',
                                            stdin_open = True,
                                            command    = 'sh -c "cat - >/out"'
                                            )
docker_client.start( container )

There is stdin_open = True, but where do I write the 'hello' ?

解决方案

Back then it was not possible to attach stdin to a running container. This has changed.

With current version of docker-py this is now somehow possible (aka slix's workaround). This is taken from a discussion at GitHub which is focused on python 2.7.

See this example in python 3 with docker-py version 3.1.1

import docker, tarfile
from io import BytesIO

def test_send_data_via_stdin_into_container():
    client = docker.APIClient()

    # create container
    container = client.create_container(
        'busybox',
        stdin_open = True,
        command    = 'sh -c "cat - >/received.txt"')
    client.start(container)

    # attach stdin to container and send data
    original_text_to_send = 'hello this is from the other side'
    s = client.attach_socket(container, params={'stdin': 1, 'stream': 1})
    s._sock.send(original_text_to_send.encode('utf-8'))
    s.close()

    # stop container and collect data from the testfile
    client.stop(container)
    client.wait(container)
    raw_stream,status = client.get_archive(container,'/received.txt')
    tar_archive = BytesIO(b"".join((i for i in raw_stream)))
    t = tarfile.open(mode='r:', fileobj=tar_archive)
    text_from_container_file = t.extractfile('received.txt').read().decode('utf-8')
    client.remove_container(container)

    # check for equality
    assert text_from_container_file == original_text_to_send

if __name__ == '__main__':
    test_send_data_via_stdin_into_container()

这篇关于如何发送到docker-py容器的stdin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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