docker-library/mongo

is the documentation broken?

mchlkoval opened this issue · 12 comments

I've been trying to connect to my local docker instance this morning without much avail after following the steps laid out in the official documentation. I made my dockerfile look like so:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

Exact copy, I keep getting connection timeout. Server selection timeout to be exact per the express logs. HOWEVER, if I were to use an older docker compose file I had from some time ago:

version: '3.9'

services:
  mongo_invenutory:
    image: mongo
    restart: always
    ports:
      - "27017:27017"    
    environment:
      MONGO_INITDB_ROOT_USERNAME: kovalsky
      MONGO_INITDB_ROOT_PASSWORD: kovalsky      
    volumes:
        - type: volume
          source: mongodb_data_volume
          target: /data/invenutoryDb    
volumes:
  mongodb_data_volume:      

I can connect just fine. The ctx I was trying to use in the Compass GUI application was: mongodb://root:example@mongo:27017/. I have also tried adding a specific authSource didn't do it. Connecting to the latter variant works well by using either mongodb://kovalsky:kovalsky@localhost:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&ssl=false or the same without the query string.

Where exactly am I going awry?

The first example works for me

@LaurentGoderre , I've made sure to update my docker desktop and I am still facing the same issue:
image

It ends with it stating getaddrinfo ENOTFOUND mongo

Though if I add ports to the above mongo, then I can connectusing mongodb://root:example@localhost:27017. Of course I assume this isn't ideal to just expose ports as such.

The hostname mongo is only exposed to the other containers in the project. If you want to connect via an app on the host then you need to add the port mapping.

I wonder also if it is PC related as my own rig works fine, but work laptop refuses no matter what. Now, regarding what you mentioned about the hostname part with mongo. Does this mean it is only exposed to the other containers defined in my compose file ( a bit new to docker)?

If so, how would I then have my locally running api hit my mongo container if I decide against also exposing port 27017?

If you are running on a Linux host, you can hit the container's ip address directly.

$ docker container inspect mongo --format '{{ .NetworkSettings.IPAddress }}
172.17.0.2

But there could be more than one network that the container is on, so might need to look through all the network settings:

$ docker container inspect mongo --format '{{ json .NetworkSettings }}' | jq

In the docker-compose example the mongo-express container won't start because it is getting Authentication failed error. Does anyone else have the same problem?

@svili the documentation as is or did you try to customize the db name?

Running the example docker-compose on the documentation as is.
I'm getting this:

mongo-express-1  | Could not connect to database using connectionString: mongodb://root:example@mongo:27017/"
mongo-express-1  | /node_modules/mongodb/lib/cmap/connection.js:231
mongo-express-1  |                     callback(new error_1.MongoServerError(document));
mongo-express-1  |                              ^
mongo-express-1  | 
mongo-express-1  | MongoServerError: Authentication failed.
mongo-express-1  |     at Connection.onMessage (/node_modules/mongodb/lib/cmap/connection.js:231:30)
mongo-express-1  |     at MessageStream.<anonymous> (/node_modules/mongodb/lib/cmap/connection.js:61:60)
mongo-express-1  |     at MessageStream.emit (node:events:517:28)
mongo-express-1  |     at processIncomingData (/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
mongo-express-1  |     at MessageStream._write (/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
mongo-express-1  |     at writeOrBuffer (node:internal/streams/writable:392:12)
mongo-express-1  |     at _write (node:internal/streams/writable:333:10)
mongo-express-1  |     at Writable.write (node:internal/streams/writable:337:10)
mongo-express-1  |     at Socket.ondata (node:internal/streams/readable:777:22)
mongo-express-1  |     at Socket.emit (node:events:517:28) {
mongo-express-1  |   ok: 0,
mongo-express-1  |   code: 18,
mongo-express-1  |   codeName: 'AuthenticationFailed',
mongo-express-1  |   connectionGeneration: 0,
mongo-express-1  |   [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
mongo-express-1  | }
mongo-express-1  | 
mongo-express-1  | Node.js v18.18.2
mongo-express-1 exited with code 1

I'm on Linux, updated my docker to the latest version, trying to run it with the docker compose plugin.

@svili Can you try explicitly linking the services?

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    links:
      - mongo
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

It's the same.
What I noticed though, which is really weird, is running the mongo server via normal docker works and I can auth into it. So with this, authentication is successful via PyMongo at least:
docker run -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=example -p 27017:27017 mongo

While via this most minimal docker-compose, which is supposed to be equivalent, I get UserNotFound: Could not find user \"root\" for db \"admin\", as if it doesn't create the user.

version: '3.1'

services:

  mongo:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    ports:
      - 27017:27017

I'm running this via docker compose up --build --remove-orphans --force-recreate and don't use volumes, didn't store anything so I'm pretty much out of ideas on what the problem is.

There is still a volume created for persistence so you might need to add -v to your docker-compose down