Skip to content

Commit 86e89f0

Browse files
committed
Update Mongo's authentication documentation to be much more explicit and link to more upstream resources
1 parent fe54ba3 commit 86e89f0

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

mongo/content.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,30 @@ When you start the `%%REPO%%` image, you can adjust the initialization of the Mo
9090

9191
### `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`
9292

93-
These variables, used in conjunction, create a new user and set that user's password. This user is created in the `admin` authentication database and given the role of `root`. Both variables are required for a user to be created. If both are present then MongoDB will start with authentication enabled: `mongod --auth`. Authentication in MongoDB is fairly complex, so more complex user setup is explicitly left to the user via `/docker-entrypoint-initdb.d/` (see *Initializing a fresh instance* below). The following is an example of using these two variables to create a MongoDB instance and then using the `mongo` cli to connect against the `admin` authentication database.
93+
These variables, used in conjunction, create a new user and set that user's password. This user is created in the `admin` [authentication database](https://docs.mongodb.com/manual/core/security-users/#user-authentication-database) and given [the role of `root`](https://docs.mongodb.com/manual/reference/built-in-roles/#root), which is [a "superuser" role](https://docs.mongodb.com/manual/core/security-built-in-roles/#superuser-roles).
9494

95-
```console
96-
$ docker run -d --name some-%%REPO%% -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret %%IMAGE%%
95+
The following is an example of using these two variables to create a MongoDB instance and then using the `mongo` cli to connect against the `admin` authentication database.
9796

98-
$ docker run -it --rm --link some-%%REPO%%:mongo %%IMAGE%% mongo --host mongo -u mongoadmin -p secret --authenticationDatabase admin some-db
97+
```console
98+
|
99+
$ docker run -d --name some-%%REPO%% \
100+
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
101+
-e MONGO_INITDB_ROOT_PASSWORD=secret \
102+
%%IMAGE%%
103+
104+
$ docker run -it --rm --link some-%%REPO%%:mongo %%IMAGE%% \
105+
mongo --host mongo \
106+
-u mongoadmin \
107+
-p secret \
108+
--authenticationDatabase admin \
109+
some-db
99110
> db.getName();
100111
some-db
101112
```
102113

103-
If you do not provide these two variables or do not set the `--auth` flag with your own custom user setup, then MongoDB will not require authentication. For more details about the functionality described here, please see the sections in the official documentation which describe [authentication](https://docs.mongodb.com/manual/core/authentication/) and [authorization](https://docs.mongodb.com/manual/core/authorization/) in more detail.
114+
Both variables are required for a user to be created. If both are present then MongoDB will start with authentication enabled (`mongod --auth`).
115+
116+
Authentication in MongoDB is fairly complex, so more complex user setup is explicitly left to the user via `/docker-entrypoint-initdb.d/` (see the *Initializing a fresh instance* and *Authentication* sections below for more details).
104117

105118
### `MONGO_INITDB_DATABASE`
106119

@@ -120,6 +133,18 @@ Currently, this is only supported for `MONGO_INITDB_ROOT_USERNAME` and `MONGO_IN
120133

121134
When a container is started for the first time it will execute files with extensions `.sh` and `.js` that are found in `/docker-entrypoint-initdb.d`. Files will be executed in alphabetical order. `.js` files will be executed by `mongo` using the database specified by the `MONGO_INITDB_DATABASE` variable, if it is present, or `test` otherwise. You may also switch databases within the `.js` script.
122135

136+
# Authentication
137+
138+
As noted above, authentication in MongoDB is fairly complex (although disabled by default). For details about how MongoDB handles authentication, please see the relevant upstream documentation:
139+
140+
- [`mongod --auth`](https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-auth)
141+
- [Security > Authentication](https://docs.mongodb.com/manual/core/authentication/)
142+
- [Security > Role-Based Access Control](https://docs.mongodb.com/manual/core/authorization/)
143+
- [Security > Role-Based Access Control > Built-In Roles](https://docs.mongodb.com/manual/core/security-built-in-roles/)
144+
- [Security > Enable Auth (tutorial)](https://docs.mongodb.com/manual/tutorial/enable-authentication/)
145+
146+
In addition to the `/docker-entrypoint-initdb.d` behavior documented above (which is a simple way to configure users for authentication for less complicated deployments), this image also supports `MONGO_INITDB_ROOT_USERNAME` and `MONGO_INITDB_ROOT_PASSWORD` for creating a simple user with [the role `root`](https://docs.mongodb.com/manual/reference/built-in-roles/#root) in the `admin` [authentication database](https://docs.mongodb.com/manual/core/security-users/#user-authentication-database), as described in the *Environment Variables* section above.
147+
123148
# Caveats
124149

125150
## Where to Store Data

0 commit comments

Comments
 (0)