Merge pull request #1 from bitnami/create-users-and-databases
creation of users and databases using env variables
This commit is contained in:
commit
104286a706
|
|
@ -13,9 +13,10 @@ ENV PATH=$BITNAMI_APP_DIR/bin:$BITNAMI_PREFIX/common/bin:$PATH
|
|||
|
||||
RUN sh $BITNAMI_PREFIX/install.sh --mongodb_password bitnami --disable-components common
|
||||
|
||||
COPY bitnami-utils-custom.sh /bitnami-utils-custom.sh
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
|
||||
EXPOSE 27017
|
||||
VOLUME ["$BITNAMI_APP_VOL_PREFIX/data", "$BITNAMI_APP_VOL_PREFIX/conf", "$BITNAMI_APP_VOL_PREFIX/logs"]
|
||||
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
CMD ["mongod"]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,429 @@
|
|||
[](http://bitnami-container-builds.bitnamiapp.com/jenkins/job/docker-mongodb/)
|
||||
|
||||
# What is MongoDB?
|
||||
|
||||
> [MongoDB](https://www.mongodb.org/) is a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas, making the integration of data in certain types of applications easier and faster.
|
||||
|
||||
# TLDR
|
||||
|
||||
```bash
|
||||
docker run --name mongodb bitnami/mongodb
|
||||
```
|
||||
|
||||
## Docker Compose
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
```
|
||||
|
||||
# Get this image
|
||||
|
||||
The recommended way to get the Bitnami MongoDB Docker Image is to pull the prebuilt image from the [Docker Hub Registry](https://hub.docker.com/u/bitnami/mongodb).
|
||||
|
||||
```bash
|
||||
docker pull bitnami/mongodb:latest
|
||||
```
|
||||
|
||||
To use a specific version, you can pull a versioned tag. You can view the [list of available versions](https://registry.hub.docker.com/u/bitnami/mongodb/tags/manage/) in the Docker Hub Registry.
|
||||
|
||||
```bash
|
||||
docker pull bitnami/mongodb:[TAG]
|
||||
```
|
||||
|
||||
If you wish, you can also build the image yourself.
|
||||
|
||||
```bash
|
||||
git clone https://github.com/bitnami/bitnami-docker-mongodb.git
|
||||
cd bitnami-docker-mongodb
|
||||
docker build -t bitnami/mongodb .
|
||||
```
|
||||
|
||||
# Persisting your database
|
||||
|
||||
If you remove the container all your data will be lost, and the next time you run the image the database will be reinitialized. To avoid this loss of data, you should mount a volume that will persist even after the container is removed.
|
||||
|
||||
**Note!**
|
||||
If you have already started using your database, follow the steps on [backing up](#backing-up-your-container) and [restoring](#restoring-a-backup) to pull the data from your running container down to your host.
|
||||
|
||||
The MongoDB image exposes a volume at `/bitnami/mongodb/data`, you can mount a directory from your host to serve as the data store. If the directory you mount is empty, the database will be initialized.
|
||||
|
||||
```bash
|
||||
docker run -v /path/to/data:/bitnami/mongodb/data bitnami/mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
volumes:
|
||||
- /path/to/data:/bitnami/mongodb/data
|
||||
```
|
||||
|
||||
# Linking
|
||||
|
||||
If you want to connect to your MongoDB server inside another container, you can use the linking system provided by Docker.
|
||||
|
||||
## Connecting a Mongo client container to the MongoDB server container
|
||||
|
||||
### Step 1: Run the MongoDB image with a specific name
|
||||
|
||||
The first step is to start our MongoDB server.
|
||||
|
||||
Docker's linking system uses container ids or names to reference containers. We can explicitly specify a name for our MongoDB server to make it easier to connect to other containers.
|
||||
|
||||
```bash
|
||||
docker run --name mongodb bitnami/mongodb
|
||||
```
|
||||
|
||||
### Step 2: Run MongoDB as a Mongo client and link to our server
|
||||
|
||||
Now that we have our MongoDB server running, we can create another container that links to it by giving Docker the `--link` option. This option takes the id or name of the container we want to link it to as well as a hostname to use inside the container, separated by a colon. For example, to have our MongoDB server accessible in another container with `server` as it's hostname we would pass `--link mongodb:server` to the Docker run command.
|
||||
|
||||
The Bitnami MongoDB Docker Image also ships with a Mongo client, but by default it will start a server. To start the client instead, we can override the default command Docker runs by stating a different command to run after the image name.
|
||||
|
||||
```bash
|
||||
docker run --rm -it --link mongodb:server bitnami/mongodb mongo --host server
|
||||
```
|
||||
|
||||
We started the Mongo client passing in the `--host` option that allows us to specify the hostname of the server, which we set to the hostname we created in the link.
|
||||
|
||||
**Note!**
|
||||
You can also run the Mongo client in the same container the server is running in using the Docker [exec](https://docs.docker.com/reference/commandline/cli/#exec) command.
|
||||
|
||||
```bash
|
||||
docker exec -it mongodb mongo
|
||||
```
|
||||
|
||||
## Linking with Docker Compose
|
||||
|
||||
### Step 1: Add a MongoDB entry in your `docker-compose.yml`
|
||||
|
||||
Copy the snippet below into your `docker-compose.yml` to add MongoDB to your application.
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
```
|
||||
|
||||
### Step 2: Link it to another container in your application
|
||||
|
||||
Update the definitions for containers you want to access your MongoDB server from to include a link to the `mongodb` entry you added in Step 1.
|
||||
|
||||
```
|
||||
myapp:
|
||||
image: myapp
|
||||
links:
|
||||
- mongodb:mongodb
|
||||
```
|
||||
|
||||
Inside `myapp`, use `mongodb` as the hostname for the MongoDB server.
|
||||
|
||||
# Configuration
|
||||
|
||||
## Setting the root password on first run
|
||||
|
||||
Passing the `MONGODB_PASSWORD` environment variable when running the image for the first time will set the password of the root user to the value of `MONGODB_PASSWORD`.
|
||||
|
||||
```bash
|
||||
docker run --name mongodb -e MONGODB_PASSWORD=password123 bitnami/mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
environment:
|
||||
- MONGODB_PASSWORD=password123
|
||||
```
|
||||
|
||||
The `root` user is configured to have full administrative access to the MongoDB server. When `MONGODB_PASSWORD` is not specified the server allows unauthenticated and unrestricted access.
|
||||
|
||||
**Note!**
|
||||
The `MONGODB_PASSWORD` enables authentication on the MongoDB server at runtime. Ensure that this parameter is **always** specified to ensure that authentication is enabled each time the container is started.
|
||||
|
||||
## Creating a user and database on first run
|
||||
|
||||
You can create a user with restricted access to a database while starting the container for the first time. To do this, provide the `MONGODB_USER`, `MONGO_PASSWORD` and `MONGODB_DATABASE` environment variables.
|
||||
|
||||
**Warning!** In this case, a root user will not be created, and your restricted user will not have permissions to create a new database.
|
||||
|
||||
```bash
|
||||
docker run --name mongodb -e MONGODB_USER=my_user -e MONGODB_PASSWORD=password123 -e MONGODB_DATABASE=my_database bitnami/mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
environment:
|
||||
- MONGODB_USER=my_user
|
||||
- MONGODB_PASSWORD=password123
|
||||
- MONGODB_DATABASE=my_database
|
||||
```
|
||||
|
||||
**Note!**
|
||||
When `MONGODB_PASSWORD` is specified along with `MONGODB_USER`, the value specified in `MONGODB_PASSWORD` is set as the password of the newly created user specified in `MONGODB_USER`.
|
||||
|
||||
## Command-line options
|
||||
|
||||
The simplest way to configure your MongoDB server is to pass custom command-line options when running the image.
|
||||
|
||||
```bash
|
||||
docker run -it --rm bitnami/mongodb --maxConns=1000
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
command: --maxConns=1000
|
||||
```
|
||||
|
||||
**Further Reading:**
|
||||
|
||||
- [MongoDB Server Command Options](http://docs.mongodb.org/manual/reference/program/mongod/)
|
||||
- [MongoDB Server Parameters](http://docs.mongodb.org/manual/reference/parameters/#mongodb-server-parameters)
|
||||
- [Caveats](#caveats)
|
||||
|
||||
## Configuration file
|
||||
|
||||
This image looks for the configuration in `/bitnami/mongodb/conf`. You can mount a volume there with your own configuration, or the default configuration will be copied to your volume if it is empty.
|
||||
|
||||
### Step 1: Run the MongoDB image
|
||||
|
||||
Run the MongoDB image, mounting a directory from your host.
|
||||
|
||||
```bash
|
||||
docker run --name mongodb -v /path/to/mongodb/conf:/bitnami/mongodb/conf bitnami/mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
volumes:
|
||||
- /path/to/mongodb/conf:/bitnami/mongodb/conf
|
||||
```
|
||||
|
||||
### Step 2: Edit the configuration
|
||||
|
||||
Edit the configuration on your host using your favorite editor.
|
||||
|
||||
```bash
|
||||
vi /path/to/mongodb/conf/mongodb.conf
|
||||
```
|
||||
|
||||
### Step 3: Restart MongoDB
|
||||
|
||||
After changing the configuration, restart your MongoDB container for changes to take effect.
|
||||
|
||||
```bash
|
||||
docker restart mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```bash
|
||||
docker-compose restart mongodb
|
||||
```
|
||||
|
||||
**Further Reading:**
|
||||
|
||||
- [Configuration File Options](http://docs.mongodb.org/v2.4/reference/configuration-options/)
|
||||
- [Caveats](#caveats)
|
||||
|
||||
## Caveats
|
||||
|
||||
The following options should not be modified, to ensure that the image runs correctly.
|
||||
|
||||
```bash
|
||||
--config /opt/bitnami/mongodb/conf/mongodb.conf
|
||||
--dbpath /opt/bitnami/mongodb/data
|
||||
```
|
||||
|
||||
# Logging
|
||||
|
||||
The Bitnami MongoDB Docker Image supports two different logging modes: logging to stdout, and logging to a file.
|
||||
|
||||
## Logging to stdout
|
||||
|
||||
The default behavior is to log to stdout, as Docker expects. These will be collected by Docker, converted to JSON and stored in the host, to be accessible via the `docker logs` command.
|
||||
|
||||
```bash
|
||||
docker logs mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```bash
|
||||
docker-compose logs mongodb
|
||||
```
|
||||
|
||||
This method of logging has the downside of not being easy to manage. Without an easy way to rotate logs, they could grow exponentially and take up large amounts of disk space on your host.
|
||||
|
||||
## Logging to file
|
||||
|
||||
To log to file, run the MongoDB image, mounting a directory from your host at `/bitnami/mongodb/logs`. This will instruct the container to send logs to a `mongodb.log` file in the mounted volume.
|
||||
|
||||
```bash
|
||||
docker run --name mongodb -v /path/to/mongodb/logs:/bitnami/mongodb/logs bitnami/mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
volumes:
|
||||
- /path/to/mongodb/logs:/bitnami/mongodb/logs
|
||||
```
|
||||
|
||||
To perform operations (e.g. logrotate) on the logs, mount the same directory in a container designed to operate on log files, such as logstash.
|
||||
|
||||
# Maintenance
|
||||
|
||||
## Backing up your container
|
||||
|
||||
To backup your data, configuration and logs, follow these simple steps:
|
||||
|
||||
### Step 1: Stop the currently running container
|
||||
|
||||
```bash
|
||||
docker stop mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```bash
|
||||
docker-compose stop mongodb
|
||||
```
|
||||
|
||||
### Step 2: Run the backup command
|
||||
|
||||
We need to mount two volumes in a container we will use to create the backup: a directory on your host to store the backup in, and the volumes from the container we just stopped so we can access the data.
|
||||
|
||||
```bash
|
||||
docker run --rm -v /path/to/backups:/backups --volumes-from mongodb busybox \
|
||||
cp -a /bitnami/mongodb /backups/latest
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```bash
|
||||
docker run --rm -v /path/to/backups:/backups --volumes-from `docker-compose ps -q mongodb` busybox \
|
||||
cp -a /bitnami/mongodb /backups/latest
|
||||
```
|
||||
|
||||
**Note!**
|
||||
If you only need to backup database data, or configuration, you can change the first argument to `cp` to `/bitnami/mongodb/data` or `/bitnami/mongodb/conf` respectively.
|
||||
|
||||
## Restoring a backup
|
||||
|
||||
Restoring a backup is as simple as mounting the backup as volumes in the container.
|
||||
|
||||
```bash
|
||||
docker run -v /path/to/backups/latest/data:/bitnami/mongodb/data \
|
||||
-v /path/to/backups/latest/conf:/bitnami/mongodb/conf \
|
||||
-v /path/to/backups/latest/logs:/bitnami/mongodb/logs \
|
||||
bitnami/mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```
|
||||
mongodb:
|
||||
image: bitnami/mongodb
|
||||
volumes:
|
||||
- /path/to/backups/latest/data:/bitnami/mongodb/data
|
||||
- /path/to/backups/latest/conf:/bitnami/mongodb/conf
|
||||
- /path/to/backups/latest/logs:/bitnami/mongodb/logs
|
||||
```
|
||||
|
||||
## Upgrade this image
|
||||
|
||||
Bitnami provides up-to-date versions of MongoDB, including security patches, soon after they are made upstream. We recommend that you follow these steps to upgrade your container.
|
||||
|
||||
### Step 1: Get the updated image
|
||||
|
||||
```bash
|
||||
docker pull bitnami/mongodb:latest
|
||||
```
|
||||
|
||||
or if you're using Docker Compose, update the value of the image property to `bitnami/mongodb:latest`.
|
||||
|
||||
### Step 2: Stop and backup the currently running container
|
||||
|
||||
Before continuing, you should backup your container's data, configuration and logs.
|
||||
|
||||
Follow the steps on [creating a backup](#backing-up-your-container).
|
||||
|
||||
### Step 3: Remove the currently running container
|
||||
|
||||
```bash
|
||||
docker rm -v mongodb
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```bash
|
||||
docker-compose rm -v mongodb
|
||||
```
|
||||
|
||||
### Step 4: Run the new image
|
||||
|
||||
Re-create your container from the new image, [restoring your backup](#restoring-a-backup) if necessary.
|
||||
|
||||
```bash
|
||||
docker run --name mongodb bitnami/mongodb:latest
|
||||
```
|
||||
|
||||
or using Docker Compose:
|
||||
|
||||
```bash
|
||||
docker-compose start mongodb
|
||||
```
|
||||
|
||||
# Testing
|
||||
|
||||
This image is tested for expected runtime behavior, using the [Bats](https://github.com/sstephenson/bats) testing framework. You can run the tests on your machine using the `bats` command.
|
||||
|
||||
```
|
||||
bats test.sh
|
||||
```
|
||||
|
||||
# Contributing
|
||||
|
||||
We'd love for you to contribute to this container. You can request new features by creating an [issue](https://github.com/bitnami/bitnami-docker-mongodb/issues), or submit a [pull request](https://github.com/bitnami/bitnami-docker-mongodb/pulls) with your contribution.
|
||||
|
||||
# Issues
|
||||
|
||||
If you encountered a problem running this container, you can file an [issue](https://github.com/bitnami/bitnami-docker-mongodb/issues). For us to provide better support, be sure to include the following information in your issue:
|
||||
|
||||
- Host OS and version
|
||||
- Docker version (`docker version`)
|
||||
- Output of `docker info`
|
||||
- Version of this container (`echo $BITNAMI_APP_VERSION` inside the container)
|
||||
- The command you used to run the container, and any relevant output you saw (masking any sensitive
|
||||
information)
|
||||
|
||||
# License
|
||||
|
||||
Copyright 2015 Bitnami
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
# MongoDB Utility functions
|
||||
PROGRAM_OPTIONS="--config $BITNAMI_APP_DIR/conf/mongodb.conf --dbpath $BITNAMI_APP_DIR/data"
|
||||
|
||||
create_mongodb_user() {
|
||||
if [ "$MONGODB_USER" ] && [ -z $MONGODB_PASSWORD ]; then
|
||||
echo ""
|
||||
echo "In order to create a MONGODB_USER you need to provide the MONGODB_PASSWORD as well"
|
||||
echo ""
|
||||
exit -1
|
||||
elif [ -z $MONGODB_PASSWORD ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
MONGODB_USER=${MONGODB_USER:-root}
|
||||
if [ "$MONGODB_USER" != "root" ] && [ ! $MONGODB_DATABASE ]; then
|
||||
echo ""
|
||||
echo "In order to use a custom MONGODB_USER you need to provide the MONGODB_DATABASE as well"
|
||||
echo ""
|
||||
exit -1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "==> Creating user $MONGODB_USER..."
|
||||
|
||||
# start mongodb server and wait for it to accept connections
|
||||
mongod $PROGRAM_OPTIONS --logpath /dev/null --bind_ip 127.0.0.1 --fork >/dev/null
|
||||
timeout=10
|
||||
while ! mongo --eval "db.adminCommand('listDatabases')" >/dev/null 2>&1
|
||||
do
|
||||
timeout=$(($timeout - 1))
|
||||
if [ $timeout -eq 0 ]; then
|
||||
echo "Could not connect to server. Aborting..."
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if [ "$MONGODB_USER" == "root" ]; then
|
||||
cat >> /tmp/initMongo.js <<EOF
|
||||
db = db.getSiblingDB('admin')
|
||||
db.createUser(
|
||||
{
|
||||
user: "$MONGODB_USER",
|
||||
pwd: "$MONGODB_PASSWORD",
|
||||
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
|
||||
}
|
||||
)
|
||||
EOF
|
||||
fi
|
||||
|
||||
if [ $MONGODB_DATABASE ]; then
|
||||
cat >> /tmp/initMongo.js <<EOF
|
||||
db = db.getSiblingDB('$MONGODB_DATABASE')
|
||||
db.createUser(
|
||||
{
|
||||
user: "$MONGODB_USER",
|
||||
pwd: "$MONGODB_PASSWORD",
|
||||
roles: [ { role: "readWrite", db: "$MONGODB_DATABASE" } ]
|
||||
}
|
||||
)
|
||||
EOF
|
||||
fi
|
||||
|
||||
mongo /tmp/initMongo.js >/dev/null
|
||||
mongod $PROGRAM_OPTIONS --shutdown >/dev/null
|
||||
|
||||
# enable authentication in mongo configuration
|
||||
echo "auth = true" >> $BITNAMI_APP_DIR/conf/mongodb.conf
|
||||
}
|
||||
|
||||
print_mongo_user() {
|
||||
if [ -z $MONGODB_USER ]; then
|
||||
echo "**none**"
|
||||
else
|
||||
echo $MONGODB_USER
|
||||
fi
|
||||
}
|
||||
|
||||
print_mongo_password() {
|
||||
if [ -z $MONGODB_PASSWORD ]; then
|
||||
echo "**none**"
|
||||
else
|
||||
echo $MONGODB_PASSWORD
|
||||
fi
|
||||
}
|
||||
|
||||
print_mongo_database() {
|
||||
if [ $MONGODB_DATABASE ]; then
|
||||
echo "Database: $MONGODB_DATABASE"
|
||||
fi
|
||||
}
|
||||
|
|
@ -12,24 +12,28 @@ fi
|
|||
|
||||
if [ ! "$(ls -A $BITNAMI_APP_VOL_PREFIX/conf)" ]; then
|
||||
generate_conf_files
|
||||
else
|
||||
print_container_already_initialized $BITNAMI_APP_NAME
|
||||
fi
|
||||
|
||||
rm -rf $BITNAMI_APP_VOL_PREFIX/data/mongod.lock
|
||||
chown -R $BITNAMI_APP_USER:$BITNAMI_APP_USER $BITNAMI_APP_VOL_PREFIX/data/ \
|
||||
$BITNAMI_APP_VOL_PREFIX/logs/ \
|
||||
$BITNAMI_APP_VOL_PREFIX/conf/ || true
|
||||
|
||||
# The user can run its own version or mongod and we still
|
||||
# will want to log it and run it using the BITNAMI_APP_USER
|
||||
if [[ "$1" = 'mongod' ]]; then
|
||||
if [ ! -f $BITNAMI_APP_VOL_PREFIX/data/storage.bson ]; then
|
||||
create_mongodb_user
|
||||
print_app_credentials $BITNAMI_APP_NAME `print_mongo_user` `print_mongo_password` `print_mongo_database`
|
||||
else
|
||||
print_container_already_initialized $BITNAMI_APP_NAME
|
||||
fi
|
||||
|
||||
rm -rf $BITNAMI_APP_VOL_PREFIX/data/mongod.lock
|
||||
chown -R $BITNAMI_APP_USER:$BITNAMI_APP_USER $BITNAMI_APP_VOL_PREFIX/data/ \
|
||||
$BITNAMI_APP_VOL_PREFIX/logs/ \
|
||||
$BITNAMI_APP_VOL_PREFIX/conf/ || true
|
||||
|
||||
wait_and_tail_logs &
|
||||
|
||||
# Add default configuration
|
||||
if [[ "$@" = 'mongod' ]]; then
|
||||
exec gosu $BITNAMI_APP_USER "$@" \
|
||||
--config $BITNAMI_APP_DIR/conf/mongodb.conf --dbpath $BITNAMI_APP_DIR/data $EXTRA_OPTIONS
|
||||
exec gosu $BITNAMI_APP_USER "$@" $PROGRAM_OPTIONS ${MONGODB_PASSWORD:+--auth} $EXTRA_OPTIONS
|
||||
else
|
||||
exec gosu $BITNAMI_APP_USER "$@"
|
||||
fi
|
||||
|
|
|
|||
|
|
@ -1,5 +1,10 @@
|
|||
$BITNAMI_APP_NAME cheatsheet:
|
||||
|
||||
ENVIRONMENT VARIABLES:
|
||||
MONGODB_USER: User to be created on first boot (default: root).
|
||||
MONGODB_PASSWORD: Password to be set for MONGODB_USER on first boot (default: none).
|
||||
MONGODB_DATABASE: Database to be created on first boot, accessible by MONGODB_USER (default: none).
|
||||
|
||||
VOLUMES:
|
||||
$BITNAMI_APP_VOL_PREFIX/data: Location of $BITNAMI_APP_NAME data files.
|
||||
$BITNAMI_APP_VOL_PREFIX/conf: Location of mongodb.conf ($BITNAMI_APP_NAME config file).
|
||||
|
|
@ -10,7 +15,8 @@
|
|||
|
||||
MISC:
|
||||
Options: You can add extra options during the docker run using the -- prefix.
|
||||
Note: The password is only set the first time you run the container.
|
||||
Note: MONGODB_USER works in conjunction with MONGODB_DATABASE environment variable.
|
||||
Tip: Back up the $BITNAMI_APP_VOL_PREFIX/data and $BITNAMI_APP_VOL_PREFIX/conf directories regularly.
|
||||
|
||||
COMMANDS:
|
||||
print-help: Print this page.
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
environment_variables:
|
||||
MONGODB_USER: "User to be created on first boot (default: root)."
|
||||
MONGODB_PASSWORD: "Password to be set for MONGODB_USER on first boot (default: none)."
|
||||
MONGODB_DATABASE: "Database to be created on first boot, accessible by MONGODB_USER (default: none)."
|
||||
volumes:
|
||||
$BITNAMI_APP_VOL_PREFIX/data: "Location of $BITNAMI_APP_NAME data files."
|
||||
$BITNAMI_APP_VOL_PREFIX/conf: "Location of mongodb.conf ($BITNAMI_APP_NAME config file)."
|
||||
|
|
@ -6,4 +10,6 @@ ports:
|
|||
27017: "Default $BITNAMI_APP_NAME port."
|
||||
misc:
|
||||
Options: "You can add extra options during the docker run using the -- prefix."
|
||||
Note: "The password is only set the first time you run the container."
|
||||
Note: "The user and database creation happens only the first time you run the container."
|
||||
Note: "MONGODB_USER works in conjunction with MONGODB_DATABASE environment variable."
|
||||
Tip: "Back up the $BITNAMI_APP_VOL_PREFIX/data and $BITNAMI_APP_VOL_PREFIX/conf directories regularly."
|
||||
|
|
|
|||
|
|
@ -0,0 +1,160 @@
|
|||
# #!/usr/bin/env bats
|
||||
CONTAINER_NAME=bitnami-mongodb-test
|
||||
IMAGE_NAME=${IMAGE_NAME:-bitnami/mongodb}
|
||||
SLEEP_TIME=5
|
||||
MONGODB_ROOT_USER=root
|
||||
MONGODB_DATABASE=test_database
|
||||
MONGODB_USER=test_user
|
||||
MONGODB_PASSWORD=test_password
|
||||
VOL_PREFIX=/bitnami/mongodb
|
||||
HOST_VOL_PREFIX=${HOST_VOL_PREFIX:-/tmp/bitnami/$CONTAINER_NAME}
|
||||
|
||||
cleanup_running_containers() {
|
||||
if [ "$(docker ps -a | grep $CONTAINER_NAME)" ]; then
|
||||
docker rm -fv $CONTAINER_NAME
|
||||
fi
|
||||
}
|
||||
|
||||
setup() {
|
||||
cleanup_running_containers
|
||||
mkdir -p $HOST_VOL_PREFIX
|
||||
}
|
||||
|
||||
teardown() {
|
||||
cleanup_running_containers
|
||||
}
|
||||
|
||||
cleanup_volumes_content() {
|
||||
docker run --rm\
|
||||
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
|
||||
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
|
||||
-v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs\
|
||||
$IMAGE_NAME rm -rf $VOL_PREFIX/data/ $VOL_PREFIX/logs/ $VOL_PREFIX/conf/
|
||||
}
|
||||
|
||||
create_container(){
|
||||
docker run --name $CONTAINER_NAME "$@" $IMAGE_NAME
|
||||
sleep $SLEEP_TIME
|
||||
}
|
||||
|
||||
# $1 is the command
|
||||
mongo_client(){
|
||||
docker run --rm --link $CONTAINER_NAME:$CONTAINER_NAME $IMAGE_NAME mongo --host $CONTAINER_NAME "$@"
|
||||
}
|
||||
|
||||
create_full_container(){
|
||||
docker run -d --name $CONTAINER_NAME\
|
||||
-e MONGODB_USER=$MONGODB_USER\
|
||||
-e MONGODB_DATABASE=$MONGODB_DATABASE\
|
||||
-e MONGODB_PASSWORD=$MONGODB_PASSWORD $IMAGE_NAME
|
||||
sleep $SLEEP_TIME
|
||||
}
|
||||
|
||||
create_full_container_mounted(){
|
||||
docker run -d --name $CONTAINER_NAME\
|
||||
-e MONGODB_USER=$MONGODB_USER\
|
||||
-e MONGODB_DATABASE=$MONGODB_DATABASE\
|
||||
-e MONGODB_PASSWORD=$MONGODB_PASSWORD\
|
||||
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
|
||||
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
|
||||
-v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs\
|
||||
$IMAGE_NAME
|
||||
sleep $SLEEP_TIME
|
||||
}
|
||||
|
||||
@test "Port 27017 exposed and accepting external connections" {
|
||||
create_container -d
|
||||
run mongo_client admin --eval "printjson(db.adminCommand('listDatabases'))"
|
||||
[[ "$output" =~ '"ok" : 1' ]]
|
||||
}
|
||||
|
||||
@test "Root user created with password" {
|
||||
create_container -d -e MONGODB_PASSWORD=$MONGODB_PASSWORD
|
||||
# Can not login as root
|
||||
run mongo_client -u $MONGODB_ROOT_USER admin --eval "printjson(db.adminCommand('listDatabases'))"
|
||||
[[ "$output" =~ "login failed" ]]
|
||||
run mongo_client -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
|
||||
[[ "$output" =~ '"ok" : 1' ]]
|
||||
}
|
||||
|
||||
@test "Root user has access to admin database" {
|
||||
create_container -d -e MONGODB_PASSWORD=$MONGODB_PASSWORD
|
||||
run mongo_client -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
|
||||
[[ "$output" =~ '"ok" : 1' ]]
|
||||
}
|
||||
|
||||
@test "Can't create root user without password" {
|
||||
run create_container -it -e MONGODB_USER=$MONGODB_ROOT_USER
|
||||
[[ "$output" =~ "you need to provide the MONGODB_PASSWORD" ]]
|
||||
}
|
||||
|
||||
@test "Can't create a custom user without password" {
|
||||
run create_container -it -e MONGODB_USER=$MONGODB_USER
|
||||
[[ "$output" =~ "you need to provide the MONGODB_PASSWORD" ]]
|
||||
}
|
||||
|
||||
@test "Can't create a custom user without database" {
|
||||
run create_container -it -e MONGODB_USER=$MONGODB_USER -e MONGODB_PASSWORD=$MONGODB_PASSWORD
|
||||
[[ "$output" =~ "you need to provide the MONGODB_DATABASE" ]]
|
||||
}
|
||||
|
||||
@test "Create custom user and database with password" {
|
||||
create_full_container
|
||||
# Cannot login without password
|
||||
run mongo_client -u $MONGODB_USER $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
|
||||
[[ "$output" =~ "login failed" ]]
|
||||
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
|
||||
[[ "$output" =~ '"ok" : 1' ]]
|
||||
}
|
||||
|
||||
@test "Custom user can't access admin database" {
|
||||
create_full_container
|
||||
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
|
||||
[[ "$output" =~ 'login failed' ]]
|
||||
}
|
||||
|
||||
@test "User and password settings are preserved after restart" {
|
||||
create_full_container
|
||||
|
||||
docker stop $CONTAINER_NAME
|
||||
docker start $CONTAINER_NAME
|
||||
sleep $SLEEP_TIME
|
||||
|
||||
run docker logs $CONTAINER_NAME
|
||||
[[ "$output" =~ "The credentials were set on first boot." ]]
|
||||
|
||||
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
|
||||
[[ "$output" =~ '"ok" : 1' ]]
|
||||
}
|
||||
|
||||
@test "If host mounted, password and settings are preserved after deletion" {
|
||||
cleanup_volumes_content
|
||||
create_full_container_mounted
|
||||
|
||||
docker rm -fv $CONTAINER_NAME
|
||||
|
||||
run docker run -d --name $CONTAINER_NAME\
|
||||
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
|
||||
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
|
||||
$IMAGE_NAME
|
||||
sleep $SLEEP_TIME
|
||||
|
||||
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
|
||||
[[ "$output" =~ '"ok" : 1' ]]
|
||||
cleanup_volumes_content
|
||||
}
|
||||
|
||||
@test "All the volumes exposed" {
|
||||
create_container -d
|
||||
run docker inspect $CONTAINER_NAME
|
||||
[[ "$output" =~ "$VOL_PREFIX/data" ]]
|
||||
[[ "$output" =~ "$VOL_PREFIX/conf" ]]
|
||||
[[ "$output" =~ "$VOL_PREFIX/logs" ]]
|
||||
}
|
||||
|
||||
@test "Data gets generated in conf and data if bind mounted in the host" {
|
||||
create_full_container_mounted
|
||||
run docker run -v $HOST_VOL_PREFIX:$HOST_VOL_PREFIX --rm $IMAGE_NAME ls -l $HOST_VOL_PREFIX/conf/mongodb.conf $HOST_VOL_PREFIX/logs/mongodb.log
|
||||
[ $status = 0 ]
|
||||
cleanup_volumes_content
|
||||
}
|
||||
Loading…
Reference in New Issue