title: How to connect to Mongo inside a Docker container using pymongo?
body: <p>I pulled the official mongo image from the Docker website and started a mongo container named <code>dataiomongo</code>. </p>
  
  <p>I now want to connect to the mongodb inside the container using pymongo. </p>
  
  <p>This is the python script I wrote:</p>
  
  <pre><code>from pprint import pprint
  from pymongo import MongoClient
  
  client = MongoClient('localhost', port=27017)
  
  db = client.admin
  
  server = db.command("serverStatus")
  pprint(server)
  </code></pre>
  
  <p>The error that came is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "D:/dataio/test_mongo.py", line 8, in &lt;module&gt;
      server = db.command("serverStatus")
    File "D:\dataio\venv\lib\site-packages\pymongo\database.py", line 655, in command
      read_preference) as (sock_info, slave_ok):
    File "C:\Python27\Lib\contextlib.py", line 17, in __enter__
      return self.gen.next()
    File "D:\dataio\venv\lib\site-packages\pymongo\mongo_client.py", line 1135, in _socket_for_reads
      server = topology.select_server(read_preference)
    File "D:\dataio\venv\lib\site-packages\pymongo\topology.py", line 226, in select_server
      address))
    File "D:\dataio\venv\lib\site-packages\pymongo\topology.py", line 184, in select_servers
      selector, server_timeout, address)
    File "D:\dataio\venv\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop
      self._error_message(selector))
  pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 10061] No connection could be made because the target machine actively refused it
  </code></pre>
  
  <p>How do I go about connecting to the mongodb inside the docker container?</p>