Unable to connect mongo-express to mongo in docker

460 views Asked by At

In order to run a unifi network application docker service. First I'm trying to start a mongo service and monitor the db with monge-express. However, I keep running into authentication problems.

Using docker-compose with a .env file to start the containers.

To comply with the requirements of the unifi service I run a mongo 4.4.18. (System runs Raspbian 64-bit on RPI4)

.env file

# envirenment variables for docker compose

PUID="1000"
PGID="1000"
TZ="Europe/Amsterdam"
RESTART_POLICY_DEFAULT="unless-stopped"

DOCKER_FOLDER="/media/docker"

### Mongo & Unifi
MONGO_USER="unifi"
MONGO_PASSWORD="unifi-pw"
MONGO_DB="unifi"
# init info
MONGO_INITDB_ROOT_USERNAME="root"
MONGO_INITDB_ROOT_PASSWORD="example"

docker compose file - compose.network.yml

# network related containers
version: "3"
name: network

services:
  portainer:
    container_name: portainer
    image: portainer/portainer-ce:linux-arm  # specific container for ARM
    volumes:
      - ${DOCKER_FOLDER}/portainer/data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    restart: always
    ports:
      - "9000:9000"

  mongo:
    container_name: mongo
    image: arm64v8/mongo:4.4.18  # unifi network application officially supports versions 3.6 up to 4.4
    restart: unless-stopped
    volumes:
      - ${DOCKER_FOLDER}/mongo/datadir:/data/db
      - ${DOCKER_FOLDER}/mongo/datadir/init-mongo.sh  # mongo < 6
 
  mongo-express:
    container_name: mongo-express
    image: mongo-express
    restart: unless-stopped
    depends_on:
      - mongo
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_SERVER: mongo
      ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_INITDB_ROOT_USERNAME}  # =root
      ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}  # =example
      ME_CONFIG_MONGODB_URL: mongo://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongo:27017/  # =mongo://root:example@mongo:27017/

  unifi-network-application:
    image: lscr.io/linuxserver/unifi-network-application:latest
    container_name: unifi-network-application
    depends_on: 
      - mongo
    environment:
      - PUID=${PUID}
      - PGID=${PGID}
      - TZ=${TZ}
      - MONGO_USER=${MONGO_USER}
      - MONGO_PASS=${MONGO_PASSWORD}
      - MONGO_HOST=mongo
      - MONGO_PORT=27017
      - MONGO_DBNAME=${MONGO_DB}
      - MEM_LIMIT=1024 #optional
      - MEM_STARTUP=1024 #optional
    volumes:
      - ${DOCKER_FOLDER}/unifi-network-application/config:/config
    ports:
      - 8443:8443
      - 3478:3478/udp
      - 10001:10001/udp
      - 8080:8080
      - 1900:1900/udp #optional
      - 8843:8843 #optional
      - 8880:8880 #optional
      - 6789:6789 #optional
      - 5514:5514/udp #optional
    restart: unless-stopped

init-mongo.sh

based on: https://gist.github.com/x-yuri/c229b3f9e8282ea12cecbbe923cc61c1

q_MONGO_USER=`jq --arg v "$MONGO_USER" -n '$v'`
q_MONGO_PASSWORD=`jq --arg v "$MONGO_PASSWORD" -n '$v'`
q_MONGO_DB=`jq --arg v "$MONGO_DB" -n '$v'`
mongo -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" admin <<EOF
    use $q_MONGO_DB;
    db.createUser({
        user: $q_MONGO_USER,
        pwd: $q_MONGO_PASSWORD,
        roles: ["readWrite"],
    });
EOF

Starting the containers

first I empty the mongo/datadir folder and then add the init-mongo.sh.

$ docker compose -f compose.network.yml up -d mongo
$ docker compose -f compose.network.yml up -d mongo-express

(Portainer is already running)

Portainer Logs

Mongo service

WARNING: MongoDB 5.0+ requires ARMv8.2-A or higher, and your current system does not appear to implement any of the common features for that!
  ...removed urls...
t={"$date":"2023-09-21T20:40:53.716+00:00"} s=I c=CONTROL id=23285 ctx=main msg=Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
t={"$date":"2023-09-21T20:40:53.725+00:00"} s=I c=NETWORK id=4648601 ctx=main msg=Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize.
t={"$date":"2023-09-21T20:40:53.726+00:00"} s=I c=STORAGE id=4615611 ctx=initandlisten msg=MongoDB starting attr={"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"4207011a0fa3"}
t={"$date":"2023-09-21T20:40:53.726+00:00"} s=I c=CONTROL id=23403 ctx=initandlisten msg=Build Info attr={"buildInfo":{"version":"4.4.18","gitVersion":"8ed32b5c2c68ebe7f8ae2ebe8d23f36037a17dea","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"aarch64","target_arch":"aarch64"}}}
t={"$date":"2023-09-21T20:40:53.726+00:00"} s=I c=CONTROL id=51765 ctx=initandlisten msg=Operating System attr={"os":{"name":"Ubuntu","version":"20.04"}}
t={"$date":"2023-09-21T20:40:53.726+00:00"} s=I c=CONTROL id=21951 ctx=initandlisten msg=Options set by command line attr={"options":{"net":{"bindIp":"*"}}}
t={"$date":"2023-09-21T20:40:53.728+00:00"} s=I c=STORAGE id=22297 ctx=initandlisten msg=Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem tags=["startupWarnings"]
t={"$date":"2023-09-21T20:40:53.729+00:00"} s=I c=STORAGE id=22315 ctx=initandlisten msg=Opening WiredTiger attr={"config":"create,cache_size=1385M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}
t={"$date":"2023-09-21T20:40:55.593+00:00"} s=I c=STORAGE id=22430 ctx=initandlisten msg=WiredTiger message attr={"message":"[1695328855:593806][1:0x7f9962a980], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global recovery timestamp: (0, 0)"}
t={"$date":"2023-09-21T20:40:55.593+00:00"} s=I c=STORAGE id=22430 ctx=initandlisten msg=WiredTiger message attr={"message":"[1695328855:593947][1:0x7f9962a980], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global oldest timestamp: (0, 0)"}
t={"$date":"2023-09-21T20:40:55.637+00:00"} s=I c=STORAGE id=4795906 ctx=initandlisten msg=WiredTiger opened attr={"durationMillis":1908}
t={"$date":"2023-09-21T20:40:55.637+00:00"} s=I c=RECOVERY id=23987 ctx=initandlisten msg=WiredTiger recoveryTimestamp attr={"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}
t={"$date":"2023-09-21T20:40:55.718+00:00"} s=I c=STORAGE id=22262 ctx=initandlisten msg=Timestamp monitor starting
t={"$date":"2023-09-21T20:40:55.747+00:00"} s=W c=CONTROL id=22120 ctx=initandlisten msg=Access control is not enabled for the database. Read and write access to data and configuration is unrestricted tags=["startupWarnings"]
t={"$date":"2023-09-21T20:40:55.749+00:00"} s=I c=STORAGE id=20320 ctx=initandlisten msg=createCollection attr={"namespace":"admin.system.version","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"27acff09-7d1d-40aa-a316-df36efda61e3"}},"options":{"uuid":{"$uuid":"27acff09-7d1d-40aa-a316-df36efda61e3"}}}
t={"$date":"2023-09-21T20:40:55.809+00:00"} s=I c=INDEX id=20345 ctx=initandlisten msg=Index build: done building attr={"buildUUID":null,"namespace":"admin.system.version","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}
t={"$date":"2023-09-21T20:40:55.810+00:00"} s=I c=COMMAND id=20459 ctx=initandlisten msg=Setting featureCompatibilityVersion attr={"newVersion":"4.4"}
t={"$date":"2023-09-21T20:40:55.811+00:00"} s=I c=STORAGE id=20536 ctx=initandlisten msg=Flow Control is enabled on this deployment
t={"$date":"2023-09-21T20:40:55.813+00:00"} s=I c=STORAGE id=20320 ctx=initandlisten msg=createCollection attr={"namespace":"local.startup_log","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"ea1c146a-189a-4c81-84bb-4535a4cd62f2"}},"options":{"capped":true,"size":10485760}}
t={"$date":"2023-09-21T20:40:55.873+00:00"} s=I c=INDEX id=20345 ctx=initandlisten msg=Index build: done building attr={"buildUUID":null,"namespace":"local.startup_log","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}
t={"$date":"2023-09-21T20:40:55.874+00:00"} s=I c=FTDC id=20625 ctx=initandlisten msg=Initializing full-time diagnostic data capture attr={"dataDirectory":"/data/db/diagnostic.data"}
t={"$date":"2023-09-21T20:40:55.876+00:00"} s=I c=REPL id=6015317 ctx=initandlisten msg=Setting new configuration state attr={"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}
t={"$date":"2023-09-21T20:40:55.880+00:00"} s=I c=CONTROL id=20712 ctx=LogicalSessionCacheReap msg=Sessions collection is not set up; waiting until next sessions reap interval attr={"error":"NamespaceNotFound: config.system.sessions does not exist"}
t={"$date":"2023-09-21T20:40:55.880+00:00"} s=I c=NETWORK id=23015 ctx=listener msg=Listening on attr={"address":"/tmp/mongodb-27017.sock"}
t={"$date":"2023-09-21T20:40:55.881+00:00"} s=I c=NETWORK id=23015 ctx=listener msg=Listening on attr={"address":"0.0.0.0"}
t={"$date":"2023-09-21T20:40:55.881+00:00"} s=I c=NETWORK id=23016 ctx=listener msg=Waiting for connections attr={"port":27017,"ssl":"off"}
t={"$date":"2023-09-21T20:40:55.880+00:00"} s=I c=STORAGE id=20320 ctx=LogicalSessionCacheRefresh msg=createCollection attr={"namespace":"config.system.sessions","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"cb5481d9-99c2-4110-9353-9f1a935af095"}},"options":{}}
t={"$date":"2023-09-21T20:40:55.988+00:00"} s=I c=INDEX id=20345 ctx=LogicalSessionCacheRefresh msg=Index build: done building attr={"buildUUID":null,"namespace":"config.system.sessions","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}
t={"$date":"2023-09-21T20:40:55.988+00:00"} s=I c=INDEX id=20345 ctx=LogicalSessionCacheRefresh msg=Index build: done building attr={"buildUUID":null,"namespace":"config.system.sessions","index":"lsidTTLIndex","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}
t={"$date":"2023-09-21T20:40:55.988+00:00"} s=I c=COMMAND id=51803 ctx=LogicalSessionCacheRefresh msg=Slow query attr={"type":"command","ns":"config.system.sessions","command":{"createIndexes":"system.sessions","indexes":[{"key":{"lastUse":1},"name":"lsidTTLIndex","expireAfterSeconds":1800}],"writeConcern":{},"$db":"config"},"numYields":0,"reslen":114,"locks":{"ParallelBatchWriterMode":{"acquireCount":{"r":5}},"FeatureCompatibilityVersion":{"acquireCount":{"r":2,"w":3}},"ReplicationStateTransition":{"acquireCount":{"w":5}},"Global":{"acquireCount":{"r":2,"w":3}},"Database":{"acquireCount":{"r":2,"w":3}},"Collection":{"acquireCount":{"r":3,"w":2}},"Mutex":{"acquireCount":{"r":6}}},"flowControl":{"acquireCount":1,"timeAcquiringMicros":3},"storage":{},"protocol":"op_msg","durationMillis":107}
t={"$date":"2023-09-21T20:40:56.794+00:00"} s=I c=NETWORK id=22943 ctx=listener msg=Connection accepted attr={"remote":"172.29.0.4:44882","connectionId":1,"connectionCount":1}
t={"$date":"2023-09-21T20:40:56.821+00:00"} s=I c=NETWORK id=51800 ctx=conn1 msg=client metadata attr={"remote":"172.29.0.4:44882","client":"conn1","doc":{"driver":{"name":"nodejs","version":"3.7.3"},"os":{"type":"Linux","name":"linux","architecture":"arm64","version":"6.1.21-v8+"},"platform":"'Node.js v12.22.7, LE (legacy)"}}
t={"$date":"2023-09-21T20:40:56.823+00:00"} s=I c=ACCESS id=20251 ctx=conn1 msg=Supported SASL mechanisms requested for unknown user attr={"user":"root@admin"}
t={"$date":"2023-09-21T20:40:56.823+00:00"} s=I c=ACCESS id=20249 ctx=conn1 msg=Authentication failed attr={"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.29.0.4:44882","extraInfo":{},"error":"UserNotFound: Could not find user \"root\" for db \"admin\""}
t={"$date":"2023-09-21T20:40:56.857+00:00"} s=I c=ACCESS id=20249 ctx=conn1 msg=Authentication failed attr={"mechanism":"SCRAM-SHA-1","speculative":false,"principalName":"root","authenticationDatabase":"admin","remote":"172.29.0.4:44882","extraInfo":{},"error":"UserNotFound: Could not find user \"root\" for db \"admin\""}
t={"$date":"2023-09-21T20:40:56.878+00:00"} s=I c=NETWORK id=22944 ctx=conn1 msg=Connection ended attr={"remote":"172.29.0.4:44882","connectionId":1,"connectionCount":0}

Mongo-express service

Welcome to mongo-express
------------------------
(node:8) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Could not connect to database using connectionString: mongodb://root:example@mongo:27017/"
(node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
    at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
    at Connection.emit (events.js:314:20)
    at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10)
    at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connection.js:625:15)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed'
}]
    at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
    at Pool.emit (events.js:314:20)
    at /node_modules/mongodb/lib/core/connection/pool.js:564:14
    at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
    at callback (/node_modules/mongodb/lib/core/connection/connect.js:75:5)
    at /node_modules/mongodb/lib/core/connection/connect.js:158:27
    at /node_modules/mongodb/lib/core/auth/scram.js:110:14
    at _callback (/node_modules/mongodb/lib/core/connection/connection.js:333:7)
    at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:9)
    at Connection.emit (events.js:314:20)
    at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10)
    at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connection.js:625:15)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
(node:8) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

segment repeats.

tried

I've tried the .js file example (but realised this is only for mongo > 6) Tried the .sh file example, but ran into the errors above. looked at this post but couldn't quit work it out. tried removing the unifi-network network.

...and lots of trial and error.

expected

running mongo service with a unifi database that can be managed via the mongo-express service.

0

There are 0 answers