title: Python docker module: How can I create a terminal shell to a docker container in python?
body: <p>I would like to create a discord/IRC bot to give random people access to a bash console on a server of mine. To make this a bit less insane I decided to use a docker container so hopefully my server doesn't get rm -rf ed. I am unfortunately stuck on getting IO to /bin/bash on the docker container however.</p>
  <pre><code>import docker
  import time
  import asyncio 
  client = docker.from_env()
  
  c = client.containers.run(
      image='disbox:main',
      command = &quot;neofetch&quot;,
      cpu_count = 1,
      mem_limit = &quot;1g&quot;,
      hostname=&quot;disbox&quot;,
      user = &quot;discord&quot;,
      entrypoint=&quot;/bin/bash&quot;,
      ports = {'80': 8080},
      detach = True
  )
  
  # wait for container to start
  time.sleep(5)
  container = client.containers.get(c.id)
  
  while True:
      cmd = input(&quot;\n:&quot;)
      res = container.exec_run(cmd, stream=True)
      for line in res:
          print(line.output)
  </code></pre>
  <p>This gives a Conflict for url error which I am not sure what that means. I have verified it is not already running elsewhere. I am running python with root (otherwise it gives me a perms error).</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/usr/lib/python3/dist-packages/docker/api/client.py&quot;, line 261, in _raise_for_status
      response.raise_for_status()
    File &quot;/usr/lib/python3/dist-packages/requests/models.py&quot;, line 940, in raise_for_status
      raise HTTPError(http_error_msg, response=self)
  requests.exceptions.HTTPError: 409 Client Error: Conflict for url: http+docker://localhost/v1.35/containers/f54feee310d0890b751d9544b020279e1ab35e470b98773f4b160b4c0a470d11/exec
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 3, in &lt;module&gt;
    File &quot;/usr/lib/python3/dist-packages/docker/models/containers.py&quot;, line 193, in exec_run
      resp = self.client.api.exec_create(
    File &quot;/usr/lib/python3/dist-packages/docker/utils/decorators.py&quot;, line 19, in wrapped
      return f(self, resource_id, *args, **kwargs)
    File &quot;/usr/lib/python3/dist-packages/docker/api/exec_api.py&quot;, line 80, in exec_create
      return self._result(res, True)
    File &quot;/usr/lib/python3/dist-packages/docker/api/client.py&quot;, line 267, in _result
      self._raise_for_status(response)
    File &quot;/usr/lib/python3/dist-packages/docker/api/client.py&quot;, line 263, in _raise_for_status
      raise create_api_error_from_http_exception(e)
    File &quot;/usr/lib/python3/dist-packages/docker/errors.py&quot;, line 31, in create_api_error_from_http_exception
      raise cls(e, response=response, explanation=explanation)
  docker.errors.APIError: 409 Client Error: Conflict (&quot;Container f54feee310d0890b751d9544b020279e1ab35e470b98773f4b160b4c0a470d11 is not running&quot;)
  </code></pre>
  <p>What this code snippet is intended to do is replicate a bash console in the python console. Where did I go wrong?</p>
  <p>edit: The image name is 100% correct its a custom ubuntu SE image with neofetch and some other stuff pre-loaded onto it.</p>