question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pipeing using exec_create, exec_start stdin, stdout

See original GitHub issue

If I use the docker CL this works perfectly for me.

echo "hello" | docker exec -i $3 sh -c 'cat >/text.txt'

Now I want to use docker-py and have this so far:

ex = cli.exec_create(container='nginx-ssl', cmd='cat >/carlskii.txt')
print cli.exec_inspect(ex)
ls = cli.exec_start(exec_id=ex["Id"], tty=True)

So how do I pass the “hello” or whatever data into the exec command so that it replicates the CL command ?

Incidentally this also works perfectly locally:

p = Popen(('docker', 'exec', '-i', 'nginx-ssl', 'sh', '-c', 'cat >text.txt'), stdin=subprocess.PIPE)
p.communicate('hello\n')
p.stdin.close()

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
carlskiicommented, Jun 9, 2016

@zbyte64 Could you provide a complete example here. I’m still struggling to get this to work.

Going back to my original question, I need to mimic this but using Python:

echo "hello" | docker exec -i $3 sh -c 'cat >/text.txt'

6reactions
zbyte64commented, May 16, 2016

Sorry, let me clarify. To write to stdin one calls attach_socket with the appropriate flags:

socket = client.attach_socket(container_id, {
    'stdin': 1,
    'stream': 1,
})

But the socket you get back cannot be written to. Instead you must use os.write to send data to stdin:

os.write(socket.fileno(), b'echo hello world')

This usage seems awkward. But If you use TLS then you instead do:

socket.send(b'echo hello world')
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write to stdin of a service - Unix & Linux Stack Exchange
You can just point ExecStart to a shell script that will set up the pipe and chainload the real executable. · I know...
Read more >
How to Pipe Output to a File When Running as a Systemd ...
Try: ExecStart=/usr/bin/sh -c "/apppath/appname > /filepath/ ...
Read more >
How to pipe Systemd service standard output from ExecStart ...
[Solved]-How to pipe Systemd service standard output from ExecStart to shell, ex. bash?-bash ... you can't really pipe stuff in ExecStart , but...
Read more >
Docker exec API: using stdin to upload a file
Hi, A file can be uploaded to a container using the docker command line as follows echo bar > /tmp/foo.txt cat /tmp/foo.txt |...
Read more >
Can systemd manage a pipeline? - Server Fault
My solution requires support for the fd option to StandardOutput= , which is available in (at least) systemd version 232 but not in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found