Adding a local MariaDB (MySQL) database

Back To Blog

In this tutorial we will add a MySQL database in the db/ folder. It is worth noting that it will consume at least 50-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 MySQL by adding the following instructions in your Dockerfile:

RUN apk add mariadb mariadb-client
ENV DB_DATA_PATH=/opt/app/db/
ENV DB_NAME=mydb # !!! change to your own DB name

RUN echo "installing db data to $DB_DATA_PATH"
RUN mysql_install_db --user=mysql --datadir=$DB_DATA_PATH

RUN echo "echo starting mysql with datadir $DB_DATA_PATH..." >> /usr/bin/start.sh
RUN echo "mysqld_safe --datadir=$DB_DATA_PATH --skip-grant-tables &" >> /usr/bin/start.sh
RUN echo "echo 'create database $DB_NAME' | mysql -u root" >> /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 touch /usr/bin/start.sh # this is the script which will run on start

RUN apk add mariadb mariadb-client
ENV DB_DATA_PATH=/opt/app/db/
ENV DB_NAME=mydb

RUN echo "installing db data to $DB_DATA_PATH"
RUN mysql_install_db --user=mysql --datadir=$DB_DATA_PATH

RUN echo "echo starting mysql with datadir $DB_DATA_PATH..." >> /usr/bin/start.sh
RUN echo "mysqld_safe --datadir=$DB_DATA_PATH --skip-grant-tables &" >> /usr/bin/start.sh
RUN echo "echo 'create database $DB_NAME' | mysql -u root" >> /usr/bin/start.sh

# if you need redis, uncomment the lines below
# RUN apk --update add redis
# RUN echo 'redis-server &' >> /usr/bin/start.sh

# daemon for cron jobs
RUN echo 'echo will install crond...' >> /usr/bin/start.sh
RUN echo 'crond' >> /usr/bin/start.sh

# Basic npm start verification
RUN echo 'nb=`cat package.json | grep start | wc -l` && if test "$nb" = "0" ; then echo "*** Boot issue: No start command found in your package.json in the scripts. See https://docs.npmjs.com/cli/start" ; exit 1 ; fi' >> /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 'npm start' >> /usr/bin/start.sh

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


Added on Sun Nov 25 2018 21:10:28 GMT+0000 (Coordinated Universal Time)