title: Capturing output of python script run inside a docker container
body: <p>The aim here is to use a docker container as a secure sandbox to run untrusted python scripts in, but to do so from within python using the docker-py module, and be able to capture the output of that script. </p>
  
  <p>I'm running a python script foo.py inside a docker container (it's set as the <code>ENTRYPOINT</code> command in my Dockerfile, so it's executed as soon as the container is run) and am unable to capture the output of that script. When I run the container via the normal CLI using </p>
  
  <pre><code>docker run -v /host_dirpath:/cont_dirpath my_image
  </code></pre>
  
  <p>(<code>host_dirpath</code> is the directory containing foo.py) I get the expected output of foo.py printed to stdout, which is just a dictionary of key-value pairs. However, I'm trying to do this from within python using the docker-py module, and somehow the script output is not being captured by the <code>logs</code> method. Here's the python code I'm using: </p>
  
  <pre><code>from docker import Client
  
  docker = Client(base_url='unix://var/run/docker.sock',
                version='1.10',
                timeout=10)
  
  contid = docker.create_container('my_image', volumes={"/cont_dirpath":""})
  docker.start(contid, binds={"/host_dirpath": {"bind": "/cont_dirpath"} })
  
  print "Docker logs: " + str(docker.logs(contid))
  </code></pre>
  
  <p>Which just results in "Docker logs: " - nothing is being captured in the logs, neither stdout nor stderr (I tried raising an exception inside foo.py to test this).</p>
  
  <p>The results I'm after are calculated by foo.py and are currently just printed to stdout with a python <code>print</code> statement. How can I get this to be included in the docker container logs so I can read it from within python? Or capture this output some other way from outside the container? </p>
  
  <p>Any help would be greatly appreciated. Thanks in advance!</p>
  
  <p>EDIT: </p>
  
  <p>Still no luck with docker-py, but it is working well when running the container with the normal CLI using subprocess.Popen - the output is indeed correctly grabbed by stdout when doing this. </p>