0

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.

5
  • Is the database actually working? (Why do you redirect its output to nowhere?) You shouldn't ever need the container-private IP address, and it doesn't work except in one or two pretty specific environments; can you access it using a published port, or using container names if the client is running in a container too? How are you launching the container(s)? Commented Oct 5, 2021 at 22:38
  • @DavidMaze when I login to the container, I can see that the DB is running only after executing "mongod" in one tab or "mongod &". Isn't container IP needed to connect to mongoDB which is on another container? Earlier when I tried with direct mongo image, it worked. But now I am trying to configure DB and attach ssh option to it as well. I am launching the containers through python docker sdk. I can post you the entire code if needed. Commented Oct 7, 2021 at 7:38
  • Why do you need to ssh into the database server? (How would you ssh into the mongod process if you weren't running in a container?) And no, you shouldn't need the container IP any more than you need to know Stack Overflow's servers' IP addresses to ask here. Commented Oct 7, 2021 at 10:11
  • @DavidMaze let me explain the use case again. I have a container with mongodb running on it and I'd be pushing info from another container. As far as I know, by default mongod doesn't allow remote connections. so mongod should be launched along with the --bind_ip_all tag. I don't know how pymongo from another container will be able to connect to mongod on another container. If you know the answer, please let me know how to with an example by connecting one container to another. Commented Oct 9, 2021 at 14:19
  • Networking in Compose in the Docker documentation is probably a good starting point for reading about inter-container communication. Commented Oct 9, 2021 at 17:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.