From e7024deff7f3fa402af197ee1cea8326c83a658c Mon Sep 17 00:00:00 2001 From: Antoine Bertin Date: Mon, 20 Sep 2021 08:50:48 +0200 Subject: [PATCH] Add docker-compose with postgres --- .dockerignore | 12 ++++++++++++ README.md | 26 ++++++++++++++++++++------ docker-compose.yaml | 19 +++++++++++++++++++ spkrepo/config.py | 6 ++++-- 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 docker-compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3550856 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +.git/ +.direnv/ +.github/ +.pytest_cache/ +__pycache__ +data/ +docs/ +.envrc +.flake8 +.gitignore +.pre-commit-config.yaml +README.md diff --git a/README.md b/README.md index 08f1ecd..fb1b928 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,11 @@ spkrepo ======= Synology Package Repository - ![Build](https://github.com/SynoCommunity/spkrepo/workflows/Build/badge.svg) ## Development ### Installation - 1. Install dependencies with `poetry install` 2. Run the next commands in the virtual environment `poetry shell` 3. Create the tables with `python manage.py db create` @@ -28,10 +26,19 @@ To reset the environment, clean up with `python manage.py clean`. 5. API is available at http://localhost:5000/api 6. Run the test suite with `poetry run pytest -v` +## Docker Compose Run +It is also possible to start a development environment with postgres database +using docker compose: +1. Build and run `docker-compose up --build` +2. On first run you can apply database migrations with `docker exec spkrepo_spkrepo_1 python manage.py db upgrade`. + Also run any other command that you need (populate the databse, create user) as mentioned above but by prefixing + with `docker exec {container_id} [...]`. +3. Browse to http://localhost:5000 +4. To tear down the environment, run `docker-compose down --remove` + ## Deployment ### Configuration - Create a config file `./config.py` to disable debug logs, connect to a database, set a secure key and optionally set a cache: Use `LC_CTYPE=C tr -cd '[:print:]' < /dev/urandom | head -c 64` or `base64 < /dev/urandom | head -c 64` to get a random string @@ -50,15 +57,22 @@ GNUPG_PATH= "/usr/local/bin/gpg" ### Docker - Example usage: ```bash -docker run -it --rm --name spkrepo -v $(pwd)/data:/data -v $(pwd)/docker-config.py:/usr/src/app/spkrepo/config.py -p 8000:8000 synocommunity/spkrepo +docker run -it --rm --name spkrepo -v $(pwd)/data:/data -p 8000:8000 synocommunity/spkrepo ``` -### Serve app via [a WSGI server](https://flask.palletsprojects.com/en/1.1.x/deploying/). +Additional configuration can be mounted in the container and loaded by putting +the path into `SPKREPO_CONFIG` environment variable. +e.g. +```bash +docker run -it --rm --name spkrepo -v $(pwd)/data:/data -v $(pwd)/docker-config.py:/docker-config.py -e SPKREPO_CONFIG=/docker-config.py -p 8000:8000 synocommunity/spkrepo +``` + + +### Serve app via [a WSGI server](https://flask.palletsprojects.com/en/1.1.x/deploying/). Example: ```bash diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..5c9b0cc --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,19 @@ +version: "3.9" +services: + db: + image: postgres:13 + environment: + POSTGRES_DB: spkrepo + POSTGRES_USER: spkrepo + POSTGRES_PASSWORD: spkrepo + spkrepo: + build: . + command: python manage.py runserver -h 0.0.0.0 + ports: + - "5000:5000" + environment: + SPKREPO_SQLALCHEMY_DATABASE_URI: postgresql://spkrepo:spkrepo@db/spkrepo + volumes: + - .:/usr/src/app/ + depends_on: + - db diff --git a/spkrepo/config.py b/spkrepo/config.py index 8456d18..4e371b2 100644 --- a/spkrepo/config.py +++ b/spkrepo/config.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -import os.path +import os DEBUG = True TESTING = False @@ -23,7 +23,9 @@ # SQLAlchemy SQLALCHEMY_ECHO = True -SQLALCHEMY_DATABASE_URI = "sqlite:///%s/spkrepo.db" % DATA_PATH +SQLALCHEMY_DATABASE_URI = os.environ.get( + "SPKREPO_SQLALCHEMY_DATABASE_URI", "sqlite:///%s/spkrepo.db" % DATA_PATH +) SQLALCHEMY_TRACK_MODIFICATIONS = False # Restful