From a50fdcebb3a5e3064ffc1ccf09d4e15064de2dd0 Mon Sep 17 00:00:00 2001 From: Ryan Hefner Date: Thu, 4 May 2017 16:23:24 -0400 Subject: [PATCH] Create automatic importer and persist data --- .gitignore | 1 + README.md | 14 ++++++-------- docker-compose.yml | 17 +++++++++++++++++ importer/Dockerfile | 11 +++++++++++ importer/files_to_import/.gitkeep | 0 importer/import.sh | 8 ++++++++ 6 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 importer/Dockerfile create mode 100644 importer/files_to_import/.gitkeep create mode 100755 importer/import.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data/ diff --git a/README.md b/README.md index 7d5b281..e6944d0 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # docker-metabase w/MongoDB backend To get started, do the following: -- Start metabase`docker-compose up` -- In another terminal, start another Mongo container: `docker run -it mongo:3.2 bash` -- In another terminal, find the container ID of the container just started: `docker ps` -- Copy file into container: `docker cp ./file.csv CONTAINER_ID:/` -- Connect container to Mongo network: `docker network connect metabase_stack CONTAINER_ID` -- Inspect network and get IP of Mongo container: `docker network Inspect metabase_stack` -- Run the import tool, replacing IP and filename: `mongoimport --host=172.19.0.2 -d groupme -c groupme --type csv --file ./file.csv --headerline` -- Open http://localhost:3000 to get to the Metabase setup wizard. When setting up the database, use the IP address from the previous step. No authentication is set up. +- Drag CSV files to import into MongoDB into `importer/files_to_import/`. If you have different kinds of files, you may need to make modifications to the import script. +- Look at the environment variables for the importer in `docker-compose.yml`. Change the database and collection names if desired. +- For the first run only, change IMPORT_RUN to "true". Then change it back to "false" so you don't have new documents being imported every time you restart the containers. +- Start metabase: `docker-compose up` +- Open http://localhost:3000 to get to the Metabase setup wizard. When setting up the database, use "mongodb" as the IP address for MongoDB and the data from the environment variables. No authentication is set up. +- diff --git a/docker-compose.yml b/docker-compose.yml index 6a8f0a2..9b34a97 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,8 @@ services: build: mongodb/ ports: - "27017:27017" + volumes: + - ./data/mongodb:/data/db networks: - stack @@ -12,6 +14,21 @@ services: build: metabase/ ports: - "3000:3000" + environment: + - MB_DB_FILE=/metabase-data/metabase.db + volumes: + - ./data/metabase:/metabase-data + networks: + - stack + depends_on: + - mongodb + + importer: + build: importer/ + environment: + - IMPORT_RUN=false + - MONGO_DATABASE=db + - MONGO_COLLECTION=collection networks: - stack depends_on: diff --git a/importer/Dockerfile b/importer/Dockerfile new file mode 100644 index 0000000..9d0c806 --- /dev/null +++ b/importer/Dockerfile @@ -0,0 +1,11 @@ +FROM mongo:3.2 + +# Create data directory and switch to it +RUN mkdir /import-data +WORKDIR /import-data + +# Copy files to import into the container +COPY ./import.sh /import-data +COPY files_to_import /import-data + +CMD ["/bin/bash", "./import.sh"] diff --git a/importer/files_to_import/.gitkeep b/importer/files_to_import/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/importer/import.sh b/importer/import.sh new file mode 100755 index 0000000..2c69b95 --- /dev/null +++ b/importer/import.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +if [ "$IMPORT_RUN" = true ] ; then + for filename in *.csv; + do + mongoimport --host=mongodb -d $MONGO_DATABASE -c $MONGO_COLLECTION --type csv --file $filename --headerline; + done +fi