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..." >> /boot.sh RUN echo "mysqld_safe --datadir=$DB_DATA_PATH --skip-grant-tables &" >> /boot.sh RUN echo "echo 'create database $DB_NAME' | mysql -u root" >> /boot.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 /boot.sh # this is the script which will run on boot 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..." >> /boot.sh RUN echo "mysqld_safe --datadir=$DB_DATA_PATH --skip-grant-tables &" >> /boot.sh RUN echo "echo 'create database $DB_NAME' | mysql -u root" >> /boot.sh # if you need redis, uncomment the lines below # RUN apk --update add redis # RUN echo 'redis-server &' >> /boot.sh # daemon for cron jobs RUN echo 'echo will install crond...' >> /boot.sh RUN echo 'crond' >> /boot.sh RUN echo 'npm install --production' >> /boot.sh # npm start, make sure to have a start attribute in "scripts" in package.json CMD sh /boot.sh && npm start
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)