Adding a local Mongodb database

Back To Blog

In this tutorial we will add a Mongodb database in the db/ folder working in an opeNode instance. It is worth noting that it will consume at least 100 MB extra memory, so if you use for instance the smallest instance plan, it is recommended to increase your plan. Also, we use use an alpine-based docker image. You can enable Mongodb by adding the following instructions in your Dockerfile:

RUN apk add mongodb
RUN echo 'mkdir -p db' >> /usr/bin/start.sh
RUN echo 'mongod --dbpath /opt/app/db/ &' >> /usr/bin/start.sh

Further, make sure to define a storage area so that it does not get deleted on updates (persistent):

openode add-storage-area db/

And also make sure to ignore that folder by creating an .openodeignore file with:

db/

The Complete Dockerfile we used:

FROM node:10-alpine

WORKDIR /opt/app

ENV PORT=80

RUN apk add mongodb
RUN echo 'mkdir -p db' >> /usr/bin/start.sh
RUN echo 'mongod --dbpath /opt/app/db/ &' >> /usr/bin/start.sh

RUN echo 'npm install --production' >> /usr/bin/start.sh

# npm start, make sure to have a start attribute in "scripts" in package.json
RUN echo 'sleep 3' >> /usr/bin/start.sh
RUN echo 'npm start' >> /usr/bin/start.sh

We tested it with a simple Node.js hello world, please see this repository.


Added on Tue Nov 27 2018 01:37:06 GMT+0000 (Coordinated Universal Time)