Answers: <p>Are you using dockerized Mongodb like this?</p>
  <p><a href="https://hub.docker.com/_/mongo" rel="nofollow noreferrer">https://hub.docker.com/_/mongo</a></p>
  <p>you must replace localhost in this line of code with mongodb container name:</p>
  <pre><code>client = MongoClient(&quot;mongodb://userName:password@&lt;mongodb cointaner name or ip&gt;/sampledb&quot;)
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-12-12T21:13:18.500Z
Answers: 2021-12-12T21:13:18.500Z
Answers: MHZarei
Body: <p>I have a simple python script which fetches certain values from the mongodb database. The script works fine on my local machine(host machine) but when i containarized my aplication it doesnt work. Here is my docker file.</p>
  <pre><code>FROM python:3.8-slim-buster
  WORKDIR /app
  COPY mysript.py .
  RUN pip3 install pymongo
  CMD [ &quot;python3&quot;, &quot;myscript.py&quot;]
  </code></pre>
  <p>My python code</p>
  <pre><code>from pymongo import MongoClient
  client = MongoClient(&quot;mongodb://userName:password@localhost/sampledb&quot;)
  db = client.sampledb
  tutorial1 = {&quot;title&quot;: &quot;Working With JSON Data in Python&quot;,&quot;author&quot;: &quot;Lucas&quot;}
  tutorial = db.sampledb
  result = tutorial.insert_one(tutorial1)
  
  for doc in tutorial.find():
    print(doc)
  </code></pre>
  <p>This code works fine on my local machine but not on the container. However i get an error like</p>
  <p><code>pymongo.errors.ServerSelectionTimeoutError: 192.168.1.12:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: &lt;TopologyDescription id: 61b38be61c25e1ffbb4b9b0f, topology_type: Unknown, servers: [&lt;ServerDescription ('192.168.1.12', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('192.168.1.12:27017: [Errno 111] Connection refused')&gt;]&gt;</code></p>
  <p>I have tried allowing by docker container IP by ufw firewall since its a connection refused issue but it doesnt work. I have also tried by changing the <code>localhost</code> field from the python code to docker container ip but that also doesnt helped me.</p>
  <p>Does someone has any insights on this? Any help would be appreciated.</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-12-10T17:51:05.260Z
Comments: How is your setup, 2 docker containers in the same VM ? different VMS? mongo somewhere else, like mongo atlas?
Comments: paltaa
Tags: <docker><python>
Title: Python not connecting to mongodb in docker container
UserDisplayName: Avinash Babu
ThreadText: # Python not connecting to mongodb in docker container
  
  (Asked by: username_0 on 2021-12-10)
  
  I have a simple python script which fetches certain values from the mongodb database. The script works fine on my local machine(host machine) but when i containarized my aplication it doesnt work. Here is my docker file.
  
  ```
  FROM python:3.8-slim-buster
  WORKDIR /app
  COPY mysript.py .
  RUN pip3 install pymongo
  CMD [ "python3", "myscript.py"]
  
  ```
  
  My python code
  
  ```
  from pymongo import MongoClient
  client = MongoClient("mongodb://userName:password@localhost/sampledb")
  db = client.sampledb
  tutorial1 = {"title": "Working With JSON Data in Python","author": "Lucas"}
  tutorial = db.sampledb
  result = tutorial.insert_one(tutorial1)
  
  for doc in tutorial.find():
    print(doc)
  
  ```
  
  This code works fine on my local machine but not on the container. However i get an error like
  
  `pymongo.errors.ServerSelectionTimeoutError: 192.168.1.12:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 61b38be61c25e1ffbb4b9b0f, topology_type: Unknown, servers: [<ServerDescription ('192.168.1.12', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('192.168.1.12:27017: [Errno 111] Connection refused')>]>`
  
  I have tried allowing by docker container IP by ufw firewall since its a connection refused issue but it doesnt work. I have also tried by changing the `localhost` field from the python code to docker container ip but that also doesnt helped me.
  
  Does someone has any insights on this? Any help would be appreciated.
  
  **Tags:** docker, python
  
  > **Comments:**
  > **username_1**: How is your setup, 2 docker containers in the same VM ? different VMS? mongo somewhere else, like mongo atlas?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-12-12. Score: 1
  
  Are you using dockerized Mongodb like this?
  
  <https://hub.docker.com/_/mongo>
  
  you must replace localhost in this line of code with mongodb container name:
  
  ```
  client = MongoClient("mongodb://userName:password@<mongodb cointaner name or ip>/sampledb")
  
  ```
  
  
  ---
  
  