Skip to content
Advertisement

Python Mongo docker-compose Topology Error

I’m trying really simple example of mongo with python and got an error.

Dockerfile:

FROM python:3.8-slim

WORKDIR /code
COPY requirements.txt ./
RUN pip3 install pipenv
RUN pipenv install -r requirements.txt
COPY . .

ENTRYPOINT ["/bin/sh", "./run_test.sh"]

run_test.sh

#!/bin/bash

exec pipenv run python3 db_test.py

docker-compose.yaml:

version: '3.3'

volumes:
  dbdata:

services:
  db:
    image: mongo
    restart: always
    volumes:
      - "dbdata:/data/db"

  bot:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - db
    env_file:
      - .env

db_test.py:

from db import Game

if __name__ == '__main__':
    if not Game.objects():
        Game(_id=1, first_team="first", second_team="second").save()
    for game in Game.objects():
        print(game._id)
        print(game.first_team)
        print(game.second_team)

db.py:

import datetime as dt
import os

from mongoengine import connect, Document, IntField, StringField, DateTimeField
from dotenv import load_dotenv

load_dotenv()

connect(os.getenv("DB_NAME"), host=os.getenv("DB_HOST"), port=27017)


class Game(Document):
    _id = IntField(required=True, primary_key=True)
    first_team = StringField(required=False)
    second_team = StringField(required=False)
    date = DateTimeField(default=dt.datetime.now, required=False)


if __name__ == "__main__":
    pass

I’m doing docker-compose up and got this output:

Starting mlb_bot_db_1 ... done
Recreating mlb_bot_bot_1 ... done
Attaching to mlb_bot_db_1, mlb_bot_bot_1
db_1   | {"t":{"$date":"2022-05-29T15:31:07.661+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.663+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.670+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.670+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.675+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.675+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.675+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.675+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.678+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"d3d10018bf13"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.679+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"5.0.8","gitVersion":"c87e1c23421bf79614baf500fda6622bd90f674e","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.679+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.679+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"}}}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.682+00:00"},"s":"W",  "c":"STORAGE",  "id":22271,   "ctx":"initandlisten","msg":"Detected unclean shutdown - Lock file is not empty","attr":{"lockFile":"/data/db/mongod.lock"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.685+00:00"},"s":"I",  "c":"STORAGE",  "id":22270,   "ctx":"initandlisten","msg":"Storage engine to use detected by data files","attr":{"dbpath":"/data/db","storageEngine":"wiredTiger"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.685+00:00"},"s":"W",  "c":"STORAGE",  "id":22302,   "ctx":"initandlisten","msg":"Recovering data from the last clean checkpoint."}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.685+00:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
db_1   | {"t":{"$date":"2022-05-29T15:31:07.686+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=481M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:08.629+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838268:629657][1:0x7f2a83021c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 11 through 12"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:08.739+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838268:739087][1:0x7f2a83021c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 12 through 12"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:08.880+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838268:880331][1:0x7f2a83021c80], txn-recover: [WT_VERB_RECOVERY_ALL] Main recovery loop: starting at 11/2688 to 12/256"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:08.883+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838268:883919][1:0x7f2a83021c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 11 through 12"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.163+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838269:163282][1:0x7f2a83021c80], file:sizeStorer.wt, txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 12 through 12"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.262+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838269:262950][1:0x7f2a83021c80], file:sizeStorer.wt, txn-recover: [WT_VERB_RECOVERY_ALL] Set global recovery timestamp: (0, 0)"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.263+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838269:263516][1:0x7f2a83021c80], file:sizeStorer.wt, txn-recover: [WT_VERB_RECOVERY_ALL] Set global oldest timestamp: (0, 0)"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.296+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1653838269:296578][1:0x7f2a83021c80], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 6, snapshot max: 6 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 1714"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.309+00:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":1623}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.311+00:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.317+00:00"},"s":"I",  "c":"STORAGE",  "id":4366408, "ctx":"initandlisten","msg":"No table logging settings modifications are required for existing WiredTiger tables","attr":{"loggingEnabled":true}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.322+00:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.340+00:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.347+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true}}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.353+00:00"},"s":"I",  "c":"STORAGE",  "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.358+00:00"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.363+00:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.375+00:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.387+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.395+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:09.395+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
db_1   | {"t":{"$date":"2022-05-29T15:31:10.016+00:00"},"s":"I",  "c":"FTDC",     "id":20631,   "ctx":"ftdc","msg":"Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost","attr":{"error":{"code":0,"codeName":"OK"}}}
bot_1  | Loading .env environment variables...
bot_1  | Traceback (most recent call last):
bot_1  |   File "db_test.py", line 4, in <module>
bot_1  |     if not Game.objects():
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/mongoengine/queryset/manager.py", line 38, in __get__
bot_1  |     queryset = queryset_class(owner, owner._get_collection())
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/mongoengine/document.py", line 232, in _get_collection
bot_1  |     if cls._meta.get("auto_create_index", True) and db.client.is_primary:
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1055, in is_primary
bot_1  |     return self._server_property("is_writable")
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/pymongo/mongo_client.py", line 836, in _server_property
bot_1  |     server = self._topology.select_server(writable_server_selector)
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/pymongo/topology.py", line 251, in select_server
bot_1  |     servers = self.select_servers(selector, server_selection_timeout, address)
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/pymongo/topology.py", line 212, in select_servers
bot_1  |     server_descriptions = self._select_servers_loop(selector, server_timeout, address)
bot_1  |   File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/pymongo/topology.py", line 227, in _select_servers_loop
bot_1  |     raise ServerSelectionTimeoutError(
bot_1  | pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 629391bed324eb5fa3d28b0b, topology_type: Unknown, servers: [<ServerDescription ('127.0.0.1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>
mlb_bot_bot_1 exited with code 1

So it looks like it can’t connect to the database. I didn’t change ports or something, and another example works like a charm with those settings, so I don’t really know what I’m missing. Without docker, it works fine.

Advertisement

Answer

Solved by adding container_name to db service in docker-compose, and then using it name as host when connecting to in python code.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement