||3 years ago|
|scripts||4 years ago|
|test||4 years ago|
|Dockerfile||4 years ago|
|LICENSE||4 years ago|
|Makefile||4 years ago|
|README.md||3 years ago|
|build_mongodb.sh||4 years ago|
|close_fw.sh||4 years ago|
|open_fw.sh||4 years ago|
|run_mongodb.sh||4 years ago|
|startup_script.sh||4 years ago|
This repo contains files for creating a MongoDB Docker container.
MongoDB Docker Image
This Docker container is based on the image from frodenas on github. The main change is that we are adding a build script and a run script, and a few tweaks to the Dockerfile.
To get the MongoDB authentication credentials for the docker container, check the logs using Docker - the credentials are printed to the logs.
$ docker logs inspiring_malachai
MongoDB Docker Image
Two scripts do the building and running of the Docker image. Don't call them directly - use the Makefile.
This should bind port 27017 from the host machine to port 27017 on the container.
Building the Docker Container
To build the Docker container, use the Makefile.
build_mongodb.sh script will do the following:
- Build the Docker container using the Dockerfile
Running the Docker Container
To run the Docker container, use the Makefile.
This script ask you to open the firewall of the machine running the Docker container to expose the container's service to the outside world.
run_mongodb.sh script will do the following:
- Mount /wifi in the host machine to /wifi in the container
- Map port X on the host machine to port X in the container (X is specified as the first command line argument)
The Dockerfile will do the following:
- Set up MongoDB aptitude repository for MongoDB 3.0
- Mount local
test/ directory contains two Python scripts to test out the MongoDB:
$ cd test/ $ python test_mongo_insert.py $ python test_mongo_retrieve.py
Run them in that order.
- Different versions of MongoDB are located at the github repo frodenas/docker-mongodb.
The first time you run your container, a new user
mongo with all privileges will be created with a random password.
To get the password, check the logs of the container by running:
docker logs <CONTAINER_ID>
You will see an output like the following:
======================================================================== MongoDB User: "mongo" MongoDB Password: "ZMUgiS3O1kJH1ec5" MongoDB Database: "admin" MongoDB Role: "dbAdminAnyDatabase" ========================================================================
If you want to preset credentials instead of a random generated ones, you can set the following environment variables:
MONGODB_USERNAMEto set a specific username
MONGODB_PASSWORDto set a specific password
On this example we will preset our custom username and password:
$ docker run -d \ --name mongodb \ -p 27017:27017 \ -e MONGODB_USERNAME=myusername \ -e MONGODB_PASSWORD=mypassword \ frodenas/mongodb
If you want to create a database at container's boot time, you can set the following environment variables:
MONGODB_DBNAMEto create a database
MONGODB_ROLEto grant the user a role to the database (by default
On this example we will preset our custom username and password and we will create a database with the default role:
$ docker run -d \ --name mongodb \ -p 27017:27017 \ -e MONGODB_USERNAME=myusername \ -e MONGODB_PASSWORD=mypassword \ -e MONGODB_DBNAME=mydb \ frodenas/mongodb
When you run the container, it will start the
MongoDB server without any arguments.
If you want to pass any arguments,
just add them to the
$ docker run \ -d --name mongodb \ -p 27017:27017 \ mongodb \ --smallfiles
MongoDB will store data in
/data to the host to keep data persistent.
$ mkdir -p /tmp/mongodb $ docker run -d \ --name mongodb \ -p 27017:27017 \ -v /tmp/mongodb:/data \ mongodb
Some Big Stupid Problems
A big huge waste of time working out some big huge stupid problems that were extremely difficult to track down. End lessons:
Lots of problems w/ things that would be set up the exact same way, then show different problems. That was a sign there might be issues with the Docker container being able to write to the files.
The data directory I was using (and have seen online in several places) is
/data/dbbut this was straight up wrong and caused me to lose all my data. It should have been
Checked and had to fix permissions of the directory being shared as the data directory. Mongo user.
The lock file issues were resolved by turning on journaling with the --jorurnal flag, but issues still persisted.
Running interactively kept showing connection problems with localhost port 27017, these actually turned out to be file repair issues. So I found the repair flag.
After adding the repair step, had to work out the order. The repair step has to be called first and separate from everything in the first run script.
Fix the mongodb scripts to use journaling and repair stuff and not NOT use journaling, etc.
Call the included startup script
/etc/rc.local as a regular
user as follows:
su charles -c '/path/to/script.sh