Python subprocess piping to stdin
Question: I'm trying to use python Popen to achieve what looks like this using the
command line.

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

The goal is to pipe the "hello" text into the `docker exec` command and have
it written to the docker container.

I've tried this but can't seem to get it to work.

    
    
    import subprocess
    from subprocess import Popen, PIPE, STDOUT
    
    p = Popen(('docker', 'exec', '-i', 'nginx-ssl', 'sh', '-c', 'cat >/text.txt'), stdin=subprocess.PIPE)
    p.stdin.write('Hello')
    p.stdin.close()
    


Answer: You need to give `stdin` the new line also:

    
    
    p.stdin.write('Hello\n')
    

That is the same thing even with `sys.stdout`. You don't need to give `print`
a new line because it does that for you, but any writing to a file that you do
manually, you need to include it. You should use `p.communicate('Hello')`
instead, though. It's made for that.

