Search code examples
mongodbdockerdockerfilepymongopymongo-3.x

Unable to connect mongodb container from python container


I am playing around with python docker sdk and my use case is to store data from one container to mongodb container. But whenever my container is running it is throwing an error.

pymongo.errors.ServerSelectionTimeoutError: 172.17.0.4:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 615c9a3b22decbdcf97d4c6b, topology_type: Single, servers: [<ServerDescription ('172.17.0.4', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('172.17.0.4:27017: [Errno 111] Connection refused')>]>

I am trying to build my own image and couldn't notice mongod service in the container as well. I did not find any service file for it as well. Even after triggering "mongod" and also even after changing bingIP to 0.0.0.0, this hasn't worked.

Below is my dockerfile for the mongo

FROM ubuntu:latest

RUN apt-get update && apt-get upgrade
COPY ./UTC /etc/localtime
RUN DEBIAN_FRONTEND=noninteractive
RUN ["apt-get", "install", "-y", "--no-install-recommends", "tzdata", "openssh-server"]
RUN apt-get install -y vim wget apt-utils sudo gnupg
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN useradd -rm -s /bin/bash -g root -G sudo -u 1001 admin
RUN echo "admin:admin" | chpasswd
COPY id_rsa.pub /home/admin/.ssh/authorized_keys
COPY init_script.sh /home
RUN chown -R admin /home/admin/.ssh
RUN chmod 600 /home/admin/.ssh/authorized_keys

RUN wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | apt-key add -
RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-5.0.list
RUN apt-get update
RUN mkdir -p /data/db
USER root
RUN  sudo chown -R root /data/db

RUN apt-get install -y --no-install-recommends mongodb-org

EXPOSE 22
EXPOSE 27017
ENTRYPOINT bash -c /home/init_script.sh

Here is my init_script.sh

service ssh restart
mongod >> /dev/null

here is the python code that is trying to reach the mongodb container

def create_db_connect():
    info=open('/home/ip_file.json',)
    ip_info=json.load(info)
    ip=ip_info["/login_mongo"]
    url="mongodb://"+ip+":27017/"
    myclient = pymongo.MongoClient(url)
    return myclient

and the ip_file.json is a dynamically generated file which looks as below

{
    "/login_mongo": "172.17.0.3"
}

I am still trying to figure out whether mongo is properly installed or not. Since I am 2 days old in using mongodb, everything is a bit confusing.


Solution

  • I have resolved this issue. Thanks @DavidMaze for the inputs. I can notice that the mongod is only listening to localhost even after binding 0.0.0.0 IP in mongo.conf. The mongodb is not showing as a service and hence we cannot solidly apply those changes even after deleting /tmp/mongo* and mongo locks.

    So this is what I did. Instead of executing mongod, I have executed mongod --bind_ip_all. This statement allows to connect all remote hosts.

    Such a small logic. I feel like a dumb moron.