Skip to content

Instantly share code, notes, and snippets.

@baruchiro
Last active February 19, 2021 09:34
Show Gist options
  • Save baruchiro/269f743ba972e4e819b19b8912a6bc2b to your computer and use it in GitHub Desktop.
Save baruchiro/269f743ba972e4e819b19b8912a6bc2b to your computer and use it in GitHub Desktop.
Dockerfile for auto update MongoDB from JSON file

JSON to MongoDB

For sure, this is not solution for production.

I have JSON files with all my bank/credit card transactions, and I found that to create a good dashboard, most of the solutions require a database.

I created a Dockerfile to update a sibling mongo container in docker-compose.yml, with the updated JSON files.

It use the HEALTHCHECK feature as a workaround to trigger the restore each time.

Any suggestions are welcome (include English corrections).

FROM mongo
ARG REPO_USER=baruchiro
ARG REPOSITORY=Accounts
ARG RESTORE_SCRIPT=mongo/load.sh
ENV REPOSITORY=${REPOSITORY}
ENV RESTORE_SCRIPT=${RESTORE_SCRIPT}
RUN apt-get update
RUN apt-get install -y git
RUN mkdir /root/.ssh/
ADD id_rsa /root/.ssh/id_rsa
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
RUN git clone [email protected]:${REPO_USER}/${REPOSITORY}.git
RUN chmod +x "/${REPOSITORY}/${RESTORE_SCRIPT}"
HEALTHCHECK --interval=10m --timeout=30s --start-period=3s --retries=3 CMD [ "/${REPOSITORY}/${RESTORE_SCRIPT}" ]
ENTRYPOINT [ "/bin/sh", "-c", "/${REPOSITORY}/${RESTORE_SCRIPT} && tail -f /dev/null" ]
#! /bin/bash
BASEDIR=$(dirname "$0")
cd $BASEDIR/..
git checkout -- .
git pull
cd data
for dateField in "date"; do
sed -i "s/\(\"$dateField\":\s*\)\(\"[-0-9T:.Z]*\"\)/\1ISODate(\2)/g" ./contributions.json
done
mongoimport --host mongodb --db transactions --collection maaser --type json --file ./contributions.json --jsonArray --mode=merge --upsertFields="date,sum,to"
for json in transactions-*.json ; do \
for dateField in "date" "processedDate"; do
sed -i "s/\(\"$dateField\":\s*\)\(\"[-0-9T:.Z]*\"\)/\1ISODate(\2)/g" "$json"
done
mongoimport --host mongodb --db transactions --collection transactions --type json \
--file $json --jsonArray --mode=merge --upsertFields=hash ;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment