Closed ScalableSec closed 3 months ago
Thanks for opening your first issue here! Be sure to follow the relevant issue templates, or risk having this issue marked as invalid.
@matteo-g hinted me to this init script that worked for me. I am not sure why it works, if it is that only one user is created (and not the same user twice) or that the role is dbOwner
instead of only readWrite
:
var user = "unifi";
var pwd = "password";
var data_db = "unifi";
var cfg_db = db + "_stat";
db.getSiblingDB(data_db).createUser(
{
user: user,
pwd: pwd,
roles: [
{
role: "dbOwner",
db: data_db
},
{
role: "dbOwner",
db: cfg_db
}
]
}
);
NOTE: Maybe someone from the Mongo and JS community can make this cleaner?
~Reproduced. And then, docker compose exec mongodb sh
+ start mongo shell + run the script commands = creates everything as needed, so I don't think the script itself is the problem - more that it's not executed?~
It was, obviously, my mistake. Make sure that the script is referenced through an absolute, host path, and with the proper permissions for mongo to open it. Make sure the script is not using commands that would not be available in the mongo version that is executing. Carefully check for errors in the mongo log. Mongo is a pain.
Hi all,
same here with init script. (password changed to non-speaking)
db.getSiblingDB("unifi").createUser({user: "unifiuser", pwd: "Mongo2me", roles: [{role: "dbOwner", db: "unifi"}]}); db.getSiblingDB("unifi_stat").createUser({user: "unifiuser", pwd: "Mongo2me", roles: [{role: "dbOwner", db: "unifi_stat"}]});
file permission (not sure it matters)
-rw-r--r-- 1 root root 242 Jan 4 15:24 mongo-init.js
docker-compose
version: "3.4"
networks:
unifi-network:
driver: bridge
services:
mongodb:
image: mongo:4.4.26
container_name: unifi-db
volumes:
- /srv/docker/unifi/unifi-db/config:/data/db
- /srv/docker/unifi-db/initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
restart: unless-stopped
networks:
- unifi-network
unifi-network-application:
depends_on:
mongodb:
condition: service_started
image: lscr.io/linuxserver/unifi-network-application:7.5.187-ls15
container_name: unifi-network-application
environment:
- PUID=1001
- PGID=1005
- TZ=Europe/Amsterdam
- MONGO_USER=unifiuser
- MONGO_PASS=Mongo2me
- MONGO_HOST=mongodb
- MONGO_PORT=27017
- MONGO_DBNAME=unifi
volumes:
- /srv/docker/unifi/app/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
networks:
- unifi-network
restart: unless-stopped
Logs from DB:
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/mongo-init.js t={"$date":"2024-01-04T14:36:44.982+00:00"} s=I c=NETWORK id=22943 ctx=listener msg=Connection accepted attr={"remote":"127.0.0.1:47152","connectionId":2,"connectionCount":1} t={"$date":"2024-01-04T14:36:44.982+00:00"} s=I c=NETWORK id=51800 ctx=conn2 msg=client metadata attr={"remote":"127.0.0.1:47152","client":"conn2","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.26"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"20.04"}}} t={"$date":"2024-01-04T14:36:44.985Z"} s=E c=- id=22780 ctx=main msg=directory [{filename}] **doesn't have any *.js files attr={"filename":"/docker-entrypoint-initdb.d/mongo-init.js"}** failed to load: /docker-entrypoint-initdb.d/mongo-init.js
What kind of attribute is expected or this description missleading?
Unfortunately I am completely new to docker which means I am not yet able to deep dive into it as I missing knowledge about possibilities to debug etc.
Edit: Obviously issue found: 1.) init file path was incorrect 2.) As I used portainer my "env_file" was missing
So: I deleted all volumes and docker related to unifi and mongo Hint: by default volume is here: /var/lib/docker/volumes _Hint2: Not only delete it from docker but also from filesystem
Working conf
version: "3.4"
networks:
unifi-network:
driver: bridge
services:
mongodb:
# working_dir: /app
image: mongo:4.4.26
container_name: unifi-db
volumes:
- /srv/docker/unifi/unifi-db/config:/data/db
- /srv/docker/unifi/unifi-db/initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
restart: unless-stopped
networks:
- unifi-network
unifi-network-application:
# working_dir: /app
depends_on:
mongodb:
condition: service_started
image: lscr.io/linuxserver/unifi-network-application:7.5.187-ls15
container_name: unifi-network-application
environment:
- PUID=1001
- PGID=1005
- TZ=Europe/Amsterdam
- MONGO_USER=unifiuser
- MONGO_PASS=Mongo2me
- MONGO_HOST=mongodb
- MONGO_PORT=27017
- MONGO_DBNAME=unifi
volumes:
- /srv/docker/unifi/app/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
networks:
- unifi-network
restart: unless-stopped
db.getSiblingDB("unifi").createUser({user: "unifiuser", pwd: "Mongo2me", roles: [{role: "dbOwner", db: "unifi"}]});
db.getSiblingDB("unifi_stat").createUser({user: "unifiuser", pwd: "Mongo2me", roles: [{role: "dbOwner", db: "unifi_stat"}]});
This issue has been automatically marked as stale because it has not had recent activity. This might be due to missing feedback from OP. It will be closed if no further activity occurs. Thank you for your contributions.
I've gone through this and can start mongo with the provided init file and it works fine. As the mongo server is outside of what we publish, it may be best to work on this with their support.
I've gone through this and can start mongo with the provided init file and it works fine. As the mongo server is outside of what we publish, it may be best to work on this with their support.
Dear @homerr : which init file are you referring? I'm struggling for weeks know trying to get this stuff working. NO WAY. The mongo-init.js is well mounted and accessible from within the container, but still have the [custom-init] No custom files found. This file is just not taken into account and of course my database isn't created and I can't go onward. I'm using Portainer as well.
No idea why... Totally lost. I've never spent so much time trying a stack to get working just because of that stupid Mongo database...
[custom-init] No custom files found.
message is referring something else entirely. Also, that message is displayed in the log of the unifi container, not mongo. Not sure why you'd expect the unifi container to log a message about mounting a file into an entirely different container.
You didn't post any info on your compose so I can only guess. My guess is, you didn't follow the readme. You need to start with an empty data folder for mongo as the db creation only happens on first start. And you need to make sure you're not setting any db or user related env vars for mongo. Both are explained in the readme.
[custom-init] No custom files found.
message is referring something else entirely. Also, that message is displayed in the log of the unifi container, not mongo. Not sure why you'd expect the unifi container to log a message about mounting a file into an entirely different container.You didn't post any info on your compose so I can only guess. My guess is, you didn't follow the readme. You need to start with an empty data folder for mongo as the db creation only happens on first start. And you need to make sure you're not setting any db or user related env vars for mongo. Both are explained in the readme.
You are right. If you get too worked up about a problem, you end up not thinking straight. Actually I found the root cause. It was linked to my use of Docker secrets in my Portainer docker-compose file... I didn't find yet why, but for some reason the mongo container seems not accepting secrets. If I put the password directly, then it works... Below is my config:
version: '3.8'
services:
unifi_netapp_db:
image: mongo:6.0
container_name: unifi_netapp_db
hostname: unifi_netapp_db
volumes:
- /mnt/docker/unifi-netapp/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- /mnt/docker/unifi-netapp/db-data/db:/data/db
restart: unless-stopped
networks:
- unifi_network
unifi_netapp_core:
image: lscr.io/linuxserver/unifi-network-application:latest
hostname: unifi_netapp_core
container_name: unifi_netapp_core
depends_on:
unifi_netapp_db:
condition: service_started
environment:
TZ: 'Europe/Paris'
MONGO_USER: mongo
MONGO_PASS: /run/secrets/mongo-password # Doesn't work...
#MONGO_PASS: "******" -> this case works fine
MONGO_HOST: unifi_netapp_db
MONGO_PORT: 27017
MONGO_DBNAME: unifi
secrets:
- mongo-password
volumes:
- /mnt/docker/unifi-netapp/core-data:/config
# ports:
# - 3478:3478/udp # Mandatory
# - 10001:10001/udp # Mandatory
# - 8080:8080 # Mandatory
# - 8443:8443 # GUI access being behind the reverse proxy
# - 8843:8843 # Optional
# - 8880:8880 # Optional
restart: unless-stopped
networks:
- proxy_network
- unifi_network
networks:
unifi_network:
driver: bridge
proxy_network:
external: true # proxy_network need to be created beforehand in Portainer interface
secrets:
mongo-password:
file: /mnt/docker/unifi-netapp/mongo_pass.txt
If not working with secrets, I will try using environment variables...
This issue is locked due to inactivity
Is there an existing issue for this?
Current Behavior
There's an "init-mongo.js" file in a shared folder that the user that has launched "docker-compose.yml" file has rwx access to. When I open a bash terminal in the mongodb container, I can verify that the "init-mongo.js" file exists in the container with the following contents:
This file exists in the /docker-entrypoint-initdb.d directory inside the container with the following permissions:
However, the mongodb container reports that it couldn't load the init-mongo.js file:
This leads to both databases not being created. the database reports that it can't find user unifiuser because of that, when the unifi-network-application container tries to access the mongodb container:
docker-compose.yml:
init-mongo.js:
Expected Behavior
The mongodb container should load the init-mongo.js file and create the unifi and unifi_stat databases with it's corresponding users.
Steps To Reproduce
Environment
CPU architecture
x86-64
Docker creation
Container logs