Charles Reid
cca5d741d8
|
6 years ago | |
---|---|---|
scripts | 7 years ago | |
test | 7 years ago | |
Dockerfile | 7 years ago | |
LICENSE | 7 years ago | |
Makefile | 7 years ago | |
README.md | 6 years ago | |
build_mongodb.sh | 7 years ago | |
close_fw.sh | 7 years ago | |
open_fw.sh | 7 years ago | |
run_mongodb.sh | 7 years ago | |
startup_script.sh | 7 years ago |
README.md
d-mongodb
This repo contains files for creating a MongoDB Docker container.
TL; DR
Run make
.
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.
$ make
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.
The 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.
The 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)
Container Actions
The Dockerfile will do the following:
- Set up MongoDB aptitude repository for MongoDB 3.0
- Mount local
/opt/mongodb
to container/data/db
Test MongoDB
The 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.
Notes
- Different versions of MongoDB are located at the github repo frodenas/docker-mongodb.
User Credentials
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_USERNAME
to set a specific usernameMONGODB_PASSWORD
to 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
Databases
If you want to create a database at container's boot time, you can set the following environment variables:
MONGODB_DBNAME
to create a databaseMONGODB_ROLE
to grant the user a role to the database (by defaultdbOwner
)
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
Extra arguments
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 run
command:
$ docker run \
-d --name mongodb \
-p 27017:27017 \
mongodb \
--smallfiles
Persistent data
MongoDB will store data in /data
.
Map /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/db
but this was straight up wrong and caused me to lose all my data. It should have been/data
. -
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.
Startup Script
Call the included startup script
from /etc/rc.local
as a regular
user as follows:
su charles -c '/path/to/script.sh