title: How to stop a specific docker container using Python
body: <p>I have a Docker compose file that starts a database, webserver, and a worker. Our users may need to run additional workers and I have that working (using "...up --scale worker=x"). The user may also want to stop a specific worker. This is where I run into trouble. On the command line, I can use "docker stop worker_name" and that works okay but not when I'm calling it from Python using subprocess. I've seen reference to using docker-compose stop but doesn't docker-compose go by your yml file? How could you use it to stop a single container? We're using Python 3.7.1 on Ubuntu 18.04. Docker version is 18.06.</p>
  
  <p>Here's a snippet from Python for stopping the worker.</p>
  
  <pre><code>kwargs - { 'shell': True,
     'stdout': subprocess.PIPE,
     'stderr': subprocess.PIPE,
     'universal_newlines': True
     }
  worker_name = 'worker_1'
  cmd = f'docker stop {worker_name}'
  proc = subprocess.Popen(cmd, **kwargs)
  out, err = proc.communicate()
  self.logger.info(f'out = {out}')
  self.logger.info(f'err = {err}')
  </code></pre>