diff --git a/Dockerfile b/Dockerfile index 26c73d1d..b5b08f38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,12 +3,13 @@ MAINTAINER Tinpee ADD . /src WORKDIR /src -RUN pip install flask gunicorn \ - && cp cards-jwasham.db cards.db +RUN pip install flask gunicorn COPY entrypoint.sh / RUN chmod +x /entrypoint.sh +VOLUME /src/db + EXPOSE 8000 CMD ["/entrypoint.sh"] diff --git a/README.md b/README.md index 15c003a6..41cc4471 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,24 @@ __Make sure you already installed [docker](https://www.docker.com)__ 1. Build image: `docker build . -t cs-flash-cards` 1. Run container: `docker run -d -p 8000:8000 --name cs-flash-cards cs-flash-cards` 1. Go your browser and type `http://localhost:8000` -1. To back up your cards db, run `docker cp cs-flash-cards:/src/cards.db /path/to/save` + +__If you already had a backup file `cards.db`. Run following command:__ +*Note: We don't need to rebuild image, just delete old container if you already built.* +`docker run -d -p 8000:8000 --name cs-flash-cards -v ::/src/db cs-flash-cards` +``: is the full path contains `cards.db` +Example: `/home/tinpee/cs-flash-cards/db`, and `cards.db` is inside this folder. + +For convenient, if you don't have `cards.db`, this container will auto copy a new one from `cards-jwasham.db`. So you don't need to `initdb` + +__How to backup data ?__ +We just need store `cards.db` file, and don't need any sql command. +- If you run container with `-v :/src/db` just go to `folder_db` and store `cards.db` anywhere you want. +- Without `-v flag`. Type: `docker cp :/src/db/cards.db /path/to/save` + +__How to restore data ?__ +- Delete old container (not image): `docker rm cs-flash-cards` +- Build a new one with `-v flag`: +`docker run -d -p 8000:8000 --name cs-flash-cards -v ::/src/db cs-flash-cards` +- Voila :) *Happy learning!* diff --git a/entrypoint.sh b/entrypoint.sh index 05fa542a..6376c182 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,9 @@ #!/bin/bash +if [ ! -f /src/db/cards.db ]; then + mkdir -p /src/db + cp cards-jwasham.db /src/db/cards.db +fi + export CARDS_SETTINGS=/src/config.txt gunicorn --bind 0.0.0.0:8000 flash_cards:app \ No newline at end of file diff --git a/flash_cards.py b/flash_cards.py index 166c6796..12bf29c0 100644 --- a/flash_cards.py +++ b/flash_cards.py @@ -8,7 +8,7 @@ # Load default config and override config from an environment variable app.config.update(dict( - DATABASE=os.path.join(app.root_path, 'cards.db'), + DATABASE=os.path.join(app.root_path, 'db', 'cards.db'), SECRET_KEY='development key', USERNAME='admin', PASSWORD='default'