MongoDB Authentication
Setup authentication on your docker mongodb with the docker-compose.override.yml file
This guide will demonstrate how to use the docker-compose.override.yml file to allows us to enable explicit authentication for MongoDB.
For more info about the override file, please consult: Docker Compose Override
Notes:
- The default configuration is secure by blocking external port access, but we can take it a step further with access credentials.
- As noted by the developers of MongoDB themselves, authentication in MongoDB is fairly complex. We will be taking a simple approach that will be good enough for most cases, especially for existing configurations of LibreChat. To learn more about how mongodb authentication works with docker, see here: https://hub.docker.com/_/mongo/
- This guide focuses exclusively on terminal-based setup procedures.
- While the steps outlined may also be applicable to Docker Desktop environments, or with non-Docker, local MongoDB, or other container setups, details specific to those scenarios are not provided.
There are 3 basic steps:
- Create an admin user within your mongodb container
- Enable authentication and create a "readWrite" user for "LibreChat"
- Configure the MONGO_URI with newly created user
TL;DR
These are all the necessary commands if you'd like to run through these quickly or for reference:
Example
Example docker-compose.override.yml file using the librechat.yaml config file, MongoDB Authentication, and mongo-express for managing your MongoDB database:
Step 1: Creating an Admin User
First, we must stop the default containers from running, and only run the mongodb container.
Note: The
-dflag detaches the current terminal instance as the container runs in the background. If you would like to see the mongodb log outputs, omit it and continue in a separate terminal.
Once running, we will enter the container's terminal and execute mongosh:
You should see the following output:
Optional: While we're here, we can disable telemetry for mongodb if desired, which is anonymous usage data collected and sent to MongoDB periodically:
Execute the command below.
Notes:
- All subsequent commands should be run in the current terminal session, regardless of the environment (Docker, Linux,
mongosh, etc.)- I will represent the actual terminal view with # example input/output or simply showing the output in some cases
Command:
Example input/output:
Now, we must access the admin database, which mongodb creates by default to create our admin user:
switched to db admin
Replace the credentials as desired and keep in your secure records for the rest of the guide.
Run command to create the admin user:
You should see an "ok" output.
You can also confirm the admin was created by running show users:
:warning: Important: if you are using mongo-express to manage your database (guide here), you need the additional permissions for the mongo-express service to run correctly:
Exit the Mongosh/Container Terminal by running exit:
And shut down the running container:
Step 2: Enabling Authentication and Creating a User with readWrite Access
We must now create/edit the docker-compose.override.yml file to enable authentication for our mongodb container. You can use this configuration to start or reference:
After configuring the override file as above, run the mongodb container again:
And access mongosh as the admin user:
Confirm you are authenticated:
Switch to the "LibreChat" database
Note: This the default database unless you changed it via the MONGO_URI; default URI:
MONGO_URI=mongodb://mongodb:27017/LibreChat
Now we'll create the actual credentials to be used by our Mongo connection string, which will be limited to read/write access of the "LibreChat" database. As before, replace the example with your desired credentials:
db.createUser({ user: 'user', pwd: 'userpasswd', roles: [ { role: "readWrite", db: "LibreChat" } ] });
You should see an "ok" output again.
You can verify the user creation with the show users command.
Exit the Mongosh/Container Terminal again with exit, and bring the container down:
I had an issue where the newly created user would not persist after creating it. To solve this, I simply repeated the steps to ensure it was created. Here they are for your convenience:
If it's still not persisting, you can try running the commands with all containers running, but note that the LibreChat container will be in an error/retrying state.
Step 3: Update the MONGO_URI to Use the New Credentials
Finally, we add the new connection string with our newly created credentials to our docker-compose.override.yml file under the api service:
So our override file looks like this now:
You should now run docker compose up successfully authenticated with read/write access to the LibreChat database
Example successful connection:
If you're having Authentication errors, run the last part of Step 2 again. I'm not sure why it's finicky but it will work after a few tries.
How is this guide?