Skip to content

Commit 946816d

Browse files
authored
Merge pull request #1127 from bchrobot/bchrobot-update-docs
Update Mongo docs for new configuration features
2 parents a57fb8d + 5349fea commit 946816d

File tree

2 files changed

+105
-30
lines changed

2 files changed

+105
-30
lines changed

mongo/content.md

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,118 @@ First developed by the software company 10gen (now MongoDB Inc.) in October 2007
1010

1111
# How to use this image
1212

13-
## start a mongo instance
13+
## Start a `%%REPO%%` server instance
1414

1515
```console
16-
$ docker run --name some-mongo -d %%IMAGE%%
16+
$ docker run --name some-%%REPO%% -d %%IMAGE%%:tag
1717
```
1818

19-
This image includes `EXPOSE 27017` (the mongo port), so standard container linking will make it automatically available to the linked containers (as the following examples illustrate).
19+
... where `some-%%REPO%%` is the name you want to assign to your container and tag is the tag specifying the MongoDB version you want. See the list above for relevant tags.
2020

21-
## connect to it from an application
21+
## Connect to MongoDB from another Docker container
22+
23+
The MongoDB server in the image listens on the standard MongoDB port, `27017`, so connecting via container linking or Docker networks will be the be the same as connecting to a remote `mongod`. The following example starts another MongoDB container instance and runs the `mongo` command line client against the original MongoDB container from the example above, allowing you to execute MongoDB statements against your database instance:
24+
25+
```console
26+
$ docker run -it --link some-%%REPO%%:mongo --rm %%IMAGE%% mongo --host mongo test
27+
```
28+
29+
... where `some-%%REPO%%` is the name of your original `mongo` container.
30+
31+
## %%STACK%%
32+
33+
Run `docker stack deploy -c stack.yml %%REPO%%` (or `docker-compose -f stack.yml up`), wait for it to initialize completely, and visit `http://swarm-ip:8081`, `http://localhost:8081`, or `http://host-ip:8081` (as appropriate).
34+
35+
## Container shell access and viewing MongoDB logs
36+
37+
The `docker exec` command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your `%%IMAGE%%` container:
2238

2339
```console
24-
$ docker run --name some-app --link some-mongo:mongo -d application-that-uses-mongo
40+
$ docker exec -it some-%%REPO%% bash
2541
```
2642

27-
## ... or via `mongo`
43+
The MongoDB Server log is available through Docker's container log:
2844

2945
```console
30-
$ docker run -it --link some-mongo:mongo --rm %%IMAGE%% sh -c 'exec mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"'
46+
$ docker logs some-%%REPO%%
3147
```
3248

3349
## Configuration
3450

35-
See the [official docs](https://docs.mongodb.com/manual/) for infomation on using and configuring MongoDB for things like replica sets and sharding.
51+
See the [MongoDB manual](https://docs.mongodb.com/manual/) for information on using and configuring MongoDB for things like replica sets and sharding.
3652

37-
Just add the `--storageEngine` argument if you want to use the WiredTiger storage engine in MongoDB 3.0 and above without making a config file. WiredTiger is the default storage engine in MongoDB 3.2 and above. Be sure to check the [docs](https://docs.mongodb.com/manual/release-notes/3.0-upgrade/#change-storage-engine-for-standalone-to-wiredtiger) on how to upgrade from older versions.
53+
## Customize configuration without configuration file
54+
55+
Most MongoDB configuration can be set through flags to `mongod`. The entrypoint of the image is created to pass its arguments along to `mongod`. See below an example of setting MongoDB to use a [smaller default file size](https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-smallfiles) via `docker run`.
3856

3957
```console
40-
$ docker run --name some-mongo -d %%IMAGE%% --storageEngine wiredTiger
58+
$ docker run --name some-%%REPO%% -d %%IMAGE%% --smallfiles
4159
```
4260

43-
### Authentication and Authorization
61+
And here is the same with a `docker-compose.yml` file
4462

45-
MongoDB does not require authentication by default, but it can be configured to do so. 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.
63+
```yaml
64+
version: '3.1'
65+
services:
66+
mongo:
67+
image: %%IMAGE%%
68+
command: --smallfiles
69+
```
4670
47-
#### Start the Database
71+
To see the full list of possible options, check the MonogDB manual on [`mongod`](https://docs.mongodb.com/manual/reference/program/mongod/) or check the `--help` output of `mongod`:
4872

4973
```console
50-
$ docker run --name some-mongo -d mongo --auth
74+
$ docker run -it --rm %%IMAGE%% --help
5175
```
5276

53-
#### Add the Initial Admin User
77+
## Using a custom MongoDB configuration file
78+
79+
For a more complicated configuration setup, you can still use the MongoDB configuration file. `mongod` does not read a configuration file by default, so the `--config` option with the path to the configuration file needs to be specified. Create a custom configuration file and put it in the container by either creating a custom Dockerfile `FROM %%IMAGE%%` or mounting it from the host machine to the container. See the MongoDB manual for a full list of [configuration file](https://docs.mongodb.com/manual/reference/configuration-options/) options.
80+
81+
For example, `/my/custom/mongod.conf` is the path to the custom configuration file. Then start the MongoDB container like the following:
5482

5583
```console
56-
$ docker exec -it some-mongo mongo admin
57-
connecting to: admin
58-
> db.createUser({ user: 'jsmith', pwd: 'some-initial-password', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
59-
Successfully added user: {
60-
"user" : "jsmith",
61-
"roles" : [
62-
{
63-
"role" : "userAdminAnyDatabase",
64-
"db" : "admin"
65-
}
66-
]
67-
}
84+
$ docker run --name some-%%REPO%% -v /my/custom:/etc/mongo -d %%IMAGE%% --config /etc/mongo/mongod.conf
6885
```
6986

70-
#### Connect Externally
87+
## Environment Variables
88+
89+
When you start the `%%REPO%%` image, you can adjust the initialization of the MongoDB instance by passing one or more environment variables on the `docker run` command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.
90+
91+
### `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`
92+
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.
7194

7295
```console
73-
$ docker run -it --rm --link some-mongo:mongo %%IMAGE%% mongo -u jsmith -p some-initial-password --authenticationDatabase admin some-mongo/some-db
96+
$ docker run -d --name some-%%REPO%% -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret %%IMAGE%%
97+
98+
$ docker run -it --rm --link some-%%REPO%%:mongo %%IMAGE%% mongo --host mongo -u mongoadmin -p secret --authenticationDatabase some-db
7499
> db.getName();
75100
some-db
76101
```
77102

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.
104+
105+
### `MONGO_INITDB_DATABASE`
106+
107+
This variable allows you to specify the name of a database to be used for creation scripts in `/docker-entrypoint-initdb.d/*.js` (see *Initializing a fresh instance* below). MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.
108+
109+
## Docker Secrets
110+
111+
As an alternative to passing sensitive information via environment variables, `_FILE` may be appended to the previously listed environment variables, causing the initialization script to load the values for those variables from files present in the container. In particular, this can be used to load passwords from Docker secrets stored in `/run/secrets/<secret_name>` files. For example:
112+
113+
```console
114+
$ docker run --name some-%%REPO%% -e MONGO_INITDB_ROOT_PASSWORD_FILE=/run/secrets/mongo-root -d %%IMAGE%%
115+
```
116+
117+
Currently, this is only supported for `MONGO_INITDB_ROOT_USERNAME` and `MONGO_INITDB_ROOT_PASSWORD`.
118+
119+
# Initializing a fresh instance
120+
121+
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.
122+
123+
# Caveats
124+
78125
## Where to Store Data
79126

80127
Important note: There are several ways to store data used by applications that run in Docker containers. We encourage users of the `%%REPO%%` images to familiarize themselves with the options available, including:
@@ -90,7 +137,7 @@ The Docker documentation is a good starting point for understanding the differen
90137
2. Start your `%%REPO%%` container like this:
91138

92139
```console
93-
$ docker run --name some-%%REPO%% -v /my/own/datadir:/data/db -d %%IMAGE%%:tag
140+
$ docker run --name some-%%REPO%% -v /my/own/datadir:/data/db -d %%IMAGE%%
94141
```
95142

96143
The `-v /my/own/datadir:/data/db` part of the command mounts the `/my/own/datadir` directory from the underlying host system as `/data/db` inside the container, where MongoDB by default will write its data files.
@@ -102,3 +149,11 @@ Note that users on host systems with SELinux enabled may see issues with this. T
102149
```console
103150
$ chcon -Rt svirt_sandbox_file_t /my/own/datadir
104151
```
152+
153+
## Creating database dumps
154+
155+
Most of the normal tools will work, although their usage might be a little convoluted in some cases to ensure they have access to the `mongod` server. A simple way to ensure this is to use `docker exec` and run the tool from the same container, similar to the following:
156+
157+
```console
158+
$ docker exec some-%%REPO%% sh -c 'exec mongodump -d <database_name> --archive' > /some/path/on/your/host/all-collections.archive
159+
```

mongo/stack.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use root/example as user/password credentials
2+
version: '3.1'
3+
4+
services:
5+
6+
mongo:
7+
image: mongo
8+
restart: always
9+
environment:
10+
MONGO_INITDB_ROOT_USERNAME: root
11+
MONGO_INITDB_ROOT_PASSWORD: example
12+
13+
mongo-express:
14+
image: mongo-express
15+
restart: always
16+
ports:
17+
- 8081:8081
18+
environment:
19+
ME_CONFIG_MONGODB_ADMINUSERNAME: root
20+
ME_CONFIG_MONGODB_ADMINPASSWORD: example

0 commit comments

Comments
 (0)