Docker Compose - pymongo - server_type:Unknown

Asked

Viewed 68 times

0

I want to place my application in TWO Docker containers. One with mongodb (official image) and one with the Ubuntu image, which runs a series of scripts to populate the database.

I’m having trouble connecting to mongodb in the other container. Everything goes up normally, but when making the connection, the following error appears:

pymongo.errors.Serverselectiontimeouterror: mongodb:27017: [Errno -2] Name or service not known, Timeout: 30s, Topology Description: <Topologydescription id: 5fc91e204a30bae1451e3afd, topology_type: Single, Servers: [<Serverdescription ('mongodb', 27017) server_type: Unknown, rtt: None, error=Autoreconnect('mongodb:27017: [Errno -2] Name or service not known')

Follow my Docker-Compose.yml

version: '3.1'

services:
  mongodb:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: *****
    
    ports:
      - 27017:27017
    volumes:
      - /mnt/docker_volumes/
  
  CNPJ_to_mongo:
        build: .
        container_name: "cnpj_to_mongo"
        image: python:latest
        environment:
            - NODE_ENV=development
            - FOO=bar
        volumes:
            - /mnt/docker_volumes/
        
        links:
            - mongodb
        depends_on:
            - mongodb

And the app’s Dockerfile:

FROM ubuntu:latest

MAINTAINER Bruno Prado <[email protected]>

CMD echo Start configuration of the workspace!

WORKDIR /home/
COPY ./ /home/

VOLUME /mnt/docker_volume/

RUN apt-get update && apt-get install -y python3 &&\
    apt-get install -y python3-pip &&\
    pip3 install pymongo &&\
    python3 teste.py

The excerpt from the script that connects to the database

from pymongo import MongoClient

client = MongoClient(host='mongodb',port=27017,username = 'user',
                    password = '*****') 

Documentation of the Docker:

Each container for a service joins the default network and is Both Reachable by other containers on that network, and discoverable by then at a hostname identical to the container name.

I put on the host the name of the mongodb container, the port and the password. Where did I go wrong?

  • Dude, try to change your python code to what’s in this gist https://gist.github.com/jfbueno/984871fe4e1ab7023bf86b1a4f711f37. And, of course, install lib Retry by Pip on your Dockerfile. If it works, let me know and I’ll write you an answer explaining.

  • I tried the way you said . And it was the same mistake..

  • I forgot a detail, your dockerfile needs to look different. Check there in gist https://gist.github.com/jfbueno/984871fe4e1ab7023bf86b1a4f711f37

  • Now everything is absolutely normal. But when I enter the mongodb container it is empty. The script has run but not entered.... I’ve been able to populate a mongodb container from localhost, but from another container it’s hard.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.