From 031b0b92334f8aab985bb3bdb7f0471b977d83f0 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Wed, 29 Nov 2017 21:57:02 +0100 Subject: [PATCH] fix #41: Docker support --- .dockerignore | 7 +++++++ .gitignore | 1 + Dockerfile | 26 ++++++++++++++++++++++++++ docker-compose.yml | 14 ++++++++++++++ docker-tools/Dockerfile | 9 +++++++++ docker-tools/dump.sh | 4 ++++ docker-tools/restore.sh | 4 ++++ docker-tools/run.sh | 30 ++++++++++++++++++++++++++++++ docker-tools/shell.sh | 4 ++++ docs/DOCKER.md | 14 ++++++++++++++ monco.py | 2 +- 11 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 docker-tools/Dockerfile create mode 100755 docker-tools/dump.sh create mode 100755 docker-tools/restore.sh create mode 100755 docker-tools/run.sh create mode 100755 docker-tools/shell.sh create mode 100644 docs/DOCKER.md diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..30fa6b5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.DS_Store +node_modules/ +__pycache__ +npm-debug*.log +*.py[cod] +*.so +.editorconfig diff --git a/.gitignore b/.gitignore index dcda1df..8b6ea4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store node_modules/ +__pycache__ dist/ npm-debug*.log ssl/*.pem diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..36b115d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM node +LABEL \ + maintainer="Davide Alberani " \ + vendor="RaspiBO" + +EXPOSE 3000 + +RUN \ + apt-get update && \ + apt-get -y --no-install-recommends install \ + nodejs \ + npm \ + python3-pymongo \ + python3-tornado && \ + rm -rf /var/lib/apt/lists/* + +COPY . /ibt2 + +WORKDIR /ibt2/ + +RUN \ + npm install && \ + nodejs build/build.js && \ + rm -rf node_modules + +ENTRYPOINT ["./ibt2.py", "--mongo_url=mongodb://mongo", "--debug"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f5a3010 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3' +services: + ibt2: + build: . + ports: + - "3000:3000" + depends_on: + - mongo + mongo: + image: "mongo:latest" + volumes: + - data:/data/db +volumes: + data: diff --git a/docker-tools/Dockerfile b/docker-tools/Dockerfile new file mode 100644 index 0000000..b766332 --- /dev/null +++ b/docker-tools/Dockerfile @@ -0,0 +1,9 @@ +FROM mongo:latest +LABEL \ + maintainer="Davide Alberani " \ + vendor="RaspiBO" + +VOLUME ["/data"] + +COPY run.sh / +ENTRYPOINT ["/run.sh"] diff --git a/docker-tools/dump.sh b/docker-tools/dump.sh new file mode 100755 index 0000000..dc35a5b --- /dev/null +++ b/docker-tools/dump.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build -t ibt2-dump-and-restore . +docker run --name ibt2-dump --rm --network="ibt2_default" -v `pwd`:/data --link=ibt2_ibt2-mongo_1:ibt2-mongo ibt2-dump-and-restore --dump diff --git a/docker-tools/restore.sh b/docker-tools/restore.sh new file mode 100755 index 0000000..615a3d0 --- /dev/null +++ b/docker-tools/restore.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build -t ibt2-dump-and-restore . +docker run --name ibt2-restore --rm --network="ibt2_default" -v `pwd`:/data --link=ibt2_ibt2-mongo_1:ibt2-mongo ibt2-dump-and-restore --restore $1 diff --git a/docker-tools/run.sh b/docker-tools/run.sh new file mode 100755 index 0000000..9baa770 --- /dev/null +++ b/docker-tools/run.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +if [ $# -lt 1 ] ; then + exit 1 +fi + +cmd="$1" + +if [ "${cmd}" = "--shell" ] ; then + echo "INFO: opening shell..." + mongo --host mongo ibt2 +elif [ "${cmd}" = "--dump" ] ; then + echo "INFO: dumping..." + mongodump --host mongo --out /tmp/ --db ibt2 || (echo "ERROR: unable to dump the database" ; exit 10) + cd /tmp + tar cfz /data/ibt2-dump-`date +'%Y-%m-%dT%H:%M:%S'`.tgz ibt2 +elif [ "${cmd}" = "--restore" ] ; then + if [ -z "$2" ] ; then + echo "ERROR: missing argument to --restore" + exit 20 + fi + echo "INFO: restoring $2..." + tar xfz "/data/$2" -C /tmp || (echo "ERROR: error unpacking file" ; exit 21) + mongo --host mongo ibt2 --eval "db.dropDatabase()" || (echo "ERROR: error dropping the database" ; exit 22) + mongorestore --host mongo -d ibt2 /tmp/ibt2 +else + echo "ERROR: command not recognized: use --dump or --restore dumps/file.tgz" + exit 30 +fi + diff --git a/docker-tools/shell.sh b/docker-tools/shell.sh new file mode 100755 index 0000000..42eaa9d --- /dev/null +++ b/docker-tools/shell.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build -t ibt2-dump-and-restore . +docker run -it --name ibt2-shell --rm --network="ibt2_default" -v `pwd`:/data --link=ibt2_ibt2-mongo_1:ibt2-mongo ibt2-dump-and-restore --shell diff --git a/docs/DOCKER.md b/docs/DOCKER.md new file mode 100644 index 0000000..b5a5d11 --- /dev/null +++ b/docs/DOCKER.md @@ -0,0 +1,14 @@ +# Docker container + +ibt2 requires MongoDB to run. + +You can use docker-compose.yml to have a complete environment. Run: `docker-compose up --build` + +The data is stored in the *ibt2_data* volume: do not cancel it. + +In the *docker-tools* directory there is a set of tools to build and run another container to dump and restore the database; you need the docker-compose running, to execute them. From that directory you can: + +- **dump.sh**: dump the current database in a file like *ibt2-dump-2017-11-28T21:57:43.tgz* +- **restore.sh**: *ibt2-dump-2017-11-28T21:57:43.tgz*: restore the given dump. Notice that the current database is completely removed, so DO NOT restore a dump if you don't have a backup of the current data +- **shell.sh**: open a shell for the database + diff --git a/monco.py b/monco.py index 6aeece6..95f2b99 100644 --- a/monco.py +++ b/monco.py @@ -105,7 +105,7 @@ class Monco(object): """ self._url = url self._dbName = dbName - self.connect(url) + self.connect() def connect(self, dbName=None, url=None): """Connect to the database.