Home > Blockchain >  Timeout error for dockerized mongodb instance
Timeout error for dockerized mongodb instance

Time:07-13

I am unable to connect to the containerized instance of mongodb via flask app.

Here's my docker-compose.yml file:

version: '3.7'

services:
  db:
    image: mysql:8.0.27
    command: '--default-authentication-plugin=mysql_native_password'
    restart: always
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
      interval: 3s
      retries: 5
      start_period: 30s
    secrets:
      - db-password
    volumes:
      - ./db/backup:/docker-entrypoint-initdb.d/:ro
    networks:
      - backnet
    environment:
      - MYSQL_DATABASE=pythonlogin_advanced
      - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
    expose:
      - 3306
      - 33060

  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: unless-stopped
    environment:
      - MONGO_INITDB_DATABASE=dstudiox
    volumes:
      - db-data:/data/db
    ports:
      - "27017"
    

  app:
    build: .
    image: dstudiopro
    restart: unless-stopped
    secrets:
      - db-password
    ports:
      - 5000:5000
    volumes:
      - .:/app
    networks:
      - backnet
    depends_on:
      - db
      - mongodb
  


volumes:
  db-data:
secrets:
  db-password:
    file: db/password.txt
networks:
  backnet:

...and a snippet of the main.py file (the actual one is huge)

from flask import Flask, render_template, request, redirect, url_for, session, make_response
from flask_mysqldb import MySQL
from flask_mail import Mail, Message
import MySQLdb.cursors, re, uuid, hashlib, datetime, os, math, urllib, json
from importlib_metadata import metadata
from engine import training
import csv
import pickle
import pandas as pd
from gridfs import GridFS
from flask import Flask, render_template, url_for, request, session, redirect
import os
from flask_pymongo import PyMongo
import bcrypt
from bson import ObjectId
import re
import nltk


app = Flask(__name__)
app.secret_key = 'your secret key'
app.config['threaded'] = True
# app.config['MONGO_URI'] = 'mongodb://0.0.0.0:27017/dstudiox'
app.config['MONGO_URI'] = 'mongodb://mongodb:27017/dstudiox'

mongo = PyMongo(app)
model_fs = GridFS(mongo.db, collection='models')
vec_fs = GridFS(mongo.db, collection='vectorizer')
enc_fs = GridFS(mongo.db, collection='encoder')
admin_fs = GridFS(mongo.db, collection='admin')


app.config['MYSQL_HOST'] = 'db'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'pythonlog'

...and this is the error I get; I did try various versions of pymongo but didn't work. Interestingly, when I connect the dockerized instance of mongodb server from another Flask application (not dockerized), it works fine!

pymongo.errors.ServerSelectionTimeoutError: mongodb:27017: [Errno -3] Temporary failure in name resolution, Timeout: 30s, Topology Description: <TopologyDescription id: 62cdad14854209282c69dc83, topology_type: Unknown, servers: [<ServerDescription ('mongodb', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb:27017: [Errno -3] Temporary failure in name resolution')>]>

docker ps -a returns all containers showing up and running, including mongodb.

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                    PORTS                      NAMES
30506f105027   dstudiopro     "/bin/sh -c 'flask r…"   44 minutes ago   Up 44 minutes             0.0.0.0:5000->5000/tcp     dstudio_app_1
e8a2d8f390ac   mysql:8.0.27   "docker-entrypoint.s…"   44 minutes ago   Up 44 minutes (healthy)   3306/tcp, 33060/tcp        dstudio_db_1
81919717bf27   mongo:latest   "docker-entrypoint.s…"   44 minutes ago   Up 44 minutes             0.0.0.0:56367->27017/tcp   mongodb

CodePudding user response:

The two containers aren't on the same networks:, so they can't connect to each other. The app container is only on the backnet network, but the mongodb container doesn't have a networks: block so it is on a default network.

Compose provides a network named default for you, so the easiest way to address this issue is simply to remove all of the networks: blocks in the entire file. Networking in Compose in the Docker documentation has further description of this setup.

version: '3.8'
services:
  mongodb:
    image: mongo:latest
    restart: unless-stopped
    environment:
      - MONGO_INITDB_DATABASE=dstudiox
    volumes:
      - db-data:/data/db
    ports:
      - "27017"

  app:
    build: .
    restart: unless-stopped
    secrets:
      - db-password
    ports:
      - 5000:5000
    depends_on:
      - mongodb
    # no networks:

# no networks: at top level either
  • Related