Can't connect to mongodb for Unifi Network Application in Docker

1.8k views Asked by At

I can't get the Unifi Network Application to work. I have two docker compose files:

One file is to create the mongodb part, unifi_db.yml

---
version: "3.1"
services:
  unifi-db:
    image: docker.io/mongo:4.4.18
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD_FILE : /run/secrets/mongodb_admin_user_password
    container_name: unifi-db
    volumes:
      - db_data:/data/db
        # - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
    restart: unless-stopped
    secrets:
      - mongodb_admin_user_password
volumes:
  db_data:

secrets:
  mongodb_admin_user_password:
    file: /etc/docker/mongodb_admin_user_password.txt

The other one to instantiate the Unifi Network Application:

---
version: "3.1"
services:
  unifi-network-application:
    image: lscr.io/linuxserver/unifi-network-application:latest
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/Amsterdam
      - MONGO_USER=unifi
      - FILE__MONGO_PASS=/run/secrets/mongodb_unifi_user_password
      - MONGO_HOST=unifi-db
      - MONGO_PORT=27017
      - MONGO_DBNAME=unifi
      - MEM_LIMIT=1024 #optional
      - MEM_STARTUP=1024 #optional
    volumes:
      - db_data:/data/db
    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
    secrets:
      - mongodb_unifi_user_password
volumes:
  db_data:

secrets:
  mongodb_unifi_user_password:
    file: /etc/docker/mongodb_unifi_user_password.txt

Both compose files work fine. Once the mongodb container is up and running, I connected to it with:

mongo -u admin -p

I enter my password and I'm in. Here I executed the following command to create a user called unifi, owner of a db called unifi:

db.getSiblingDB("unifi").createUser({user: "unifi", pwd: "KJDNFWI4R3R", roles: [{role: "dbOwner", db: "unifi"}, {role: "dbOwner", db: "unifi_stat"}]});

And I get a successful response:

Successfully added user: {
    "user" : "unifi",
    "roles" : [
        {
            "role" : "dbOwner",
            "db" : "unifi"
        },
        {
            "role" : "dbOwner",
            "db" : "unifi_stat"
        }
    ]
}

However, the Unifi Network application can't connect to the database!

I can't understand what I'm doing wrong. I looked everywhere and I really can't get the connection to work.

In the logs from the Unifi Network Application I see this:

[2023-10-17 17:25:10,082] <launcher> INFO  db     - Connecting to mongodb://unifi:~MONGO_PASS~@unifi-db:27017/unifi
[2023-10-17 17:25:11,255] <launcher> INFO  db     - db connection established...
[2023-10-17 17:25:12,634] <launcher> ERROR db     - Got error while connecting to db: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='unifi', source='unifi', password=<hidden>, mechanismProperties=<hidden>}

What am I doing wrong? Are the docker volumes set correctly? Am I injecting the secrets in the right way? I created the unifi db user in the right way?

2

There are 2 answers

0
matteo-g On BEST ANSWER

After debugging and checking, I found the issue. The log gave me a ring bell when I saw

[2023-10-17 17:25:10,082] <launcher> INFO  db     - Connecting to mongodb://unifi:~MONGO_PASS~@unifi-db:27017/unifi

That means that the secret is not passed correctly. I checked the secret and i tried to cat it noticing a trailing new new line (a EOL char) since the prompt was a new line afte the cat command.

What I did was removing the EOL char from the secret and it connected immediately.

How did I remove the EOL char from the secret file? Like this:

perl -pi -e 'chomp if eof' mongodb_unifi_user_password.txt

This was done in collaboration with some guys from the linuxserver team who were really helpfull helping me out.

4
M. V. On

Try setting MONGO_TLS=false environment variable for unifi-network-application service.

Let me know if that works for you.