itsafire
itsafire

Reputation: 6093

How to send to stdin of a docker-py 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' ?

Upvotes: 17

Views: 8443

Answers (2)

itsafire
itsafire

Reputation: 6093

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()

Upvotes: 8

Tiago
Tiago

Reputation: 765

Here's an updated solution:

#!/usr/bin/env python
import docker

# connect to docker
client = docker.APIClient()

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

# attach to the container stdin socket
s = client.attach_socket(container, params={'stdin': 1, 'stream': 1})

# send text
s.send('hello')

# close, stop and disconnect
s.close()
client.stop(container)
client.wait(container)
client.remove_container(container)

Upvotes: 0

Related Questions