Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

610
Vistas
How to connect to Mongo inside a Docker container using pymongo?

I pulled the official mongo image from the Docker website and started a mongo container named dataiomongo.

I now want to connect to the mongodb inside the container using pymongo.

This is the python script I wrote:

from pprint import pprint
from pymongo import MongoClient

client = MongoClient('localhost', port=27017)

db = client.admin

server = db.command("serverStatus")
pprint(server)

The error that came is:

Traceback (most recent call last):
  File "D:/dataio/test_mongo.py", line 8, in <module>
    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

How do I go about connecting to the mongodb inside the docker container?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

run mongo

First you need to run mongo

$ docker run --rm --name my-mongo -it -p 27017:27017 mongo:latest

as a daemon

$ docker run --name my-mongo -d mongo:latest

connect to the previous container.. with another container

$ docker run -it --link my-mongo:mongo --rm mongo:latest sh -c 'exec mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"'

Insert Data into db

Insert the data into the db

Connect db with python

client = MongoClient()
client.server_info()
db = client.yourdbname
over 4 years ago · Santiago Trujillo Denunciar

0

I think you miss -p 27017:27017 flag. docker run -p 27017:27017 --name mymongo -d mongo .

over 4 years ago · Santiago Trujillo Denunciar

0

Make sure you bind the 27017 container port to host port via -p 27017:27017 flag.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda