Connection error when running MongoDB inside a container: connection refused

I have a Singularity container in which I build and run MongoDB, calling /usr/bin/mongo "$@" directly. When I do this, I get the error message

MongoDB shell version v4.2.11

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

2020-12-09T13:42:32.943+0000 E  QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

connect@src/mongo/shell/mongo.js:353:17

@(connect):2:6

2020-12-09T13:42:32.944+0000 F  -        [main] exception: connect failed

2020-12-09T13:42:32.944+0000 E  -        [main] exiting with code 1

Interestingly, when MongoDB already runs outside the container, I can also start it within the container, without that error happening.
Because of this, one theory is that I need to somehow open port 27017 in advance, which is what I will try out next. However, I wanted to post this question in the meantime in case this should not turn out to solve my issue.

If relevant, my recipe is this:

Bootstrap: docker
From: ubuntu:20.04

%files

%post

    apt update
    apt-get install -y wget
    apt-get install -y software-properties-common

    wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.2.list
    apt update
    apt-get install -y mongodb-org

%runscript
    /usr/bin/mongo "$@"

mongo is the client, mongod is the server.

So what you are doing is starting the client.

This product is already well containerised, but this is not the first self rolled container thread or the first singularity thread.

https://hub.docker.com/_/mongo

1 Like

Ah I’m stupid, thanks.

(As for the recipe, I need Singularity because I use other images from there which are later bundled with Singularity Compose.)

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.