title: How do I use Python to launch an interactive Docker container?
body: <p>I am working with a Docker image which I launch in interactive mode like so: <code>docker run -it --rm ubuntu bash</code></p>
  
  <p>The actual image I work with has many complicated parameters, which is why I wrote a script to construct the full <code>docker run</code> command and launch it for me. As the logic grew more complicated, I want to migrate the script from bash to Python.</p>
  
  <p>Using <code>docker-py</code>, I prepared everything to run the image. Seems like using <code>docker.containers.run</code> for interactive shells is <a href="https://github.com/docker/docker-py/issues/390" rel="noreferrer">not supported</a>, however. Using <code>subprocess</code> instead seems logical, so I tried the following:</p>
  
  <pre><code>import subprocess
  
  subprocess.Popen(['docker', 'run', '-it', '--rm', 'ubuntu', 'bash'])
  </code></pre>
  
  <p>But this gives me:</p>
  
  <pre><code>$ python3 docker_run_test.py 
  $ unable to setup input stream: unable to set IO streams as raw terminal: input/output error
  $
  </code></pre>
  
  <p>Note that the error message appears in a different shell prompt from the python command.</p>
  
  <p>How do I make <code>python3 docker_run_test.py</code> to do equivalent of running <code>docker run -it --rm ubuntu bash</code>?</p>