Mongo Docker
December 22, 2019 ยท View on GitHub
Dockerfile and scripts to setup a production ready Mongo container with user authentication and SSL configuration
Example project https://github.com/duluca/lemon-mart-server
Key features:
- Uses the official
mongodocker image as the base - Enables configuring an application database and user/password
- Creates default admin user, if it doesn't already exist
- Scripts to deploy this image to your own AWS ECS cluster
Setup
- Define a
.envfile at the root of the project and set the MongoDB admin passowrd. Do NOT commit this file.
MONGO_INITDB_ROOT_USERNAME=admin_user
MONGO_INITDB_ROOT_PASSWORD=admin_password
MONGODB_APPLICATION_DATABASE=app_db_name
MONGODB_APPLICATION_USER=app_user
MONGODB_APPLICATION_PASS=app_password
Quick Start
npm installto get dependenciesnpm run docker:buildto build the Docker imagenpm run docker:debugto build and debug the Docker image
Note: To persist data you must link
/data/dbto a location where the data will persist if this image is stopped. See theymlfile below as an example.
- Use
npm run docker:runMountto enable data persistance with this image
Sample Usage with docker-compose
Full source code: https://github.com/duluca/lemon-mart-server
docker-compose.yml
version: '3'
services:
web-app:
build: web-app
ports:
- '8080:3000'
server:
build: server
environment:
- MONGO_URI=mongodb://database/minimal-mean
ports:
- '3000:3000'
depends_on:
- database
links:
- database
database:
image: excellalabs/mongo
env_file: .env
ports:
- '27017:27017'
volumes:
- '/my/own/datadir:/data/db'
Mounting to AWS EFS
If you're using AWS ECS as your container host, then your data needs to reside on AWS EFS. Setting this up correctly, so you can easily mount your '/data/db' volume to EFS can be complicated. Check out this step-by-step guide that will help you do just that, including how to create an AWS ECS Cluster from scratch.
TODO
- Enable
SSHcapability: https://docs.mongodb.com/manual/tutorial/configure-ssl/
Inspired by http://blog.bejanalex.com/2017/03/running-mongodb-in-a-docker-container-with-authentication/