Run sequential commands in Python with subprocess

2019-08-22 12:28发布

问题:

hope you can help. I need, in my Python script, to run the software container Docker with a specific image (Fenics in my case) and then to pass him a command to execute a script.

I've tried with subprocess:

   cmd1 = 'docker exec -ti -u fenics name_of_my_container /bin/bash -l'
    cmd2 = 'python2 shared/script_to_be_executed.py'
    process = subprocess.Popen(shlex.split(cmd1), 
              stdout=subprocess.PIPE,stdin=subprocess.PIPE, stderr = 
              subprocess.PIPE)
    process.stdin.write(cmd2)
    print(first_process.stdout.read())

But it doesn't do anything. Suggestions?

回答1:

Drop the -it flags in your call do docker, you don't want them. Also, don't try to send the command to execute into the container via stdin, but just pass the command to run in your call do docker exec.

I don't have a container running, so I'll use docker run instead, but the code below should give you a clue:

import subprocess
cmd = 'docker run python:3.6.4-jessie python -c print("hello")'.split()
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
print(out)

This will run python -c print("hello") in the container and capture the output, so the Python (3.6) script will itself print

b'hello\n'

It will also work in Python 2.7, I don't know which version you're using on the host machine :)

Regarding communicating with a subprocess, see the official docs subprocess.Popen.communicate. Since Python 3.5 there's also subprocess.run, which makes your life even easier.

HTH!