From 0622597f493e315556f315d2e9bb0a91bb123ade Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Tue, 24 Aug 2021 09:41:04 +0400
Subject: [PATCH] Fixed docker issues. Description: * Added persistent volume
 to databases. * Fixed Dockerfile. * Fixed poetry requirement. * Fixed
 SQLAlchemy version.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 README.md                                     |  2 ++
 .../deploy/Dockerfile                         |  9 +++---
 .../deploy/docker-compose.yml                 | 28 ++++++++++++++-----
 .../pyproject.toml                            |  6 ++--
 .../web/lifetime.py                           |  2 +-
 pyproject.toml                                |  2 +-
 6 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md
index c4f8d76..3de5768 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,8 @@
 
 ⚠️ [Git](https://git-scm.com/downloads), [Python](https://www.python.org/) and [Poetry](https://python-poetry.org/) must be installed and accessible ⚠️
 
+Poetry version must be greater or equal than 1.1.8. Otherwise it won't be able to install SQLAlchemy.
+
 ```bash
 python3 -m pip install fastapi_template
 python3 -m fastapi_template
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile
index b40f5eb..7e052a8 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile
@@ -1,12 +1,16 @@
 FROM python:3.9.6-slim-buster
 
+RUN apt-get update && apt-get install -y \
+    wait-for-it \
+    && apt-get clean && rm -rf /var/lib/apt/lists/*
+
 RUN useradd -m {{cookiecutter.project_name}}
 
 USER {{cookiecutter.project_name}}
 
 ENV PATH="${PATH}:/home/{{cookiecutter.project_name}}/.poetry/bin:/home/{{cookiecutter.project_name}}/.local/bin"
 
-RUN pip install poetry==1.1.7
+RUN pip install poetry==1.1.8
 
 # Installing requirements
 RUN poetry config virtualenvs.create false
@@ -21,10 +25,7 @@ COPY . /home/{{cookiecutter.project_name}}/app/src/
 WORKDIR /home/{{cookiecutter.project_name}}/app/src/
 RUN pip install --use-feature=in-tree-build  .
 
-WORKDIR /home/{{cookiecutter.project_name}}/app
-
 USER root
-RUN rm -rf /home/{{cookiecutter.project_name}}/app/src
 RUN chown -R {{cookiecutter.project_name}} /home/{{cookiecutter.project_name}}
 RUN chmod -R 700 /home/{{cookiecutter.project_name}}
 USER {{cookiecutter.project_name}}
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml
index 3a0be7c..27a7ef0 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml
@@ -5,16 +5,11 @@ services:
     build:
       context: .
       dockerfile: ./deploy/Dockerfile
+    image: {{cookiecutter.project_name}}:latest
     env_file:
       - .env
     environment:
-      - {{cookiecutter.project_name | upper }}_VAR=test # stub
-      {%- if cookiecutter.db_info.name == "postgresql" %}
-      - {{cookiecutter.project_name | upper }}_DB_PORT=5432
-      {%- endif %}
-      {%- if cookiecutter.db_info.name == "mysql" %}
-      - {{cookiecutter.project_name | upper }}_DB_PORT=3306
-      {%- endif %}
+      - {{cookiecutter.project_name | upper }}_HOST=0.0.0.0
 
   {%- if cookiecutter.db_info.name == "postgresql" %}
   db:
@@ -24,6 +19,8 @@ services:
       - POSTGRES_PASSWORD={{cookiecutter.project_name}}
       - POSTGRES_USER={{cookiecutter.project_name}}
       - POSTGRES_DB={{cookiecutter.project_name}}
+    volumes:
+      - {{cookiecutter.project_name}}-db-data:/var/lib/postgresql/data
   {%- endif %}
 
   {%- if cookiecutter.db_info.name == "mysql" %}
@@ -34,6 +31,17 @@ services:
       - MYSQL_PASSWORD={{cookiecutter.project_name}}
       - MYSQL_USER={{cookiecutter.project_name}}
       - MYSQL_DATABASE={{cookiecutter.project_name}}
+    volumes:
+      - {{cookiecutter.project_name}}-db-data:/bitnami/mysql/data
+  {%- endif %}
+
+  {% if cookiecutter.db_info.name != 'none' %}
+  migrator:
+    build:
+      context: .
+      dockerfile: ./deploy/Dockerfile
+    image: {{cookiecutter.project_name}}:latest
+    command: wait-for-it {{cookiecutter.project_name}}-db:{{cookiecutter.db_info.port}} -- alembic upgrade head
   {%- endif %}
 
   {%- if cookiecutter.enable_redis == "True" %}
@@ -43,3 +51,9 @@ services:
     environment:
       - ALLOW_EMPTY_PASSWORD=yes
   {%- endif %}
+
+{% if cookiecutter.db_info.name != 'none' %}
+volumes:
+  {{cookiecutter.project_name}}-db-data:
+    name: {{cookiecutter.project_name}}-db-data
+{% endif %}
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml b/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml
index b93dc40..7df58b4 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml
@@ -17,8 +17,7 @@ uvicorn = "^0.15.0"
 pydantic = {version = "^1.8.2", extras = ["dotenv"]}
 yarl = "^1.6.3"
 {%- if cookiecutter.db_info.name != "none" %}
-SQLAlchemy = {version = ">=1.4,<1.4.23", extras = ["mypy"]}
-greenlet = "^1.1.1"
+SQLAlchemy = {version = "^1.4", extras = ["mypy", "asyncio"]}
 {%- endif %}
 {% if cookiecutter.enable_alembic == "True" %}
 alembic = "^1.6.5"
@@ -49,6 +48,9 @@ black = "^21.7b0"
 pytest-async-sqlalchemy = "^0.1.3"
 {%- endif %}
 autoflake = "^1.4"
+{%- if cookiecutter.db_info.name != "none" %}
+SQLAlchemy = {version = "^1.4", extras = ["mypy"]}
+{%- endif %}
 {%- if cookiecutter.enable_alembic %}
 pytest-alembic = "^0.3.3"
 {%- endif %}
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py
index 15a4ea8..56a383a 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py
@@ -10,7 +10,7 @@ import aioredis
 
 {%- if cookiecutter.db_info.name != "none" %}
 from asyncio import current_task
-from sqlalchemy.ext.asyncio import (  # type: ignore
+from sqlalchemy.ext.asyncio import (
     AsyncSession,
     async_scoped_session,
     create_async_engine,
diff --git a/pyproject.toml b/pyproject.toml
index 34ffd91..7904b9b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "fastapi_template"
-version = "2.1.1"
+version = "2.1.2"
 description = "Feature-rich robust FastAPI template"
 authors = ["Pavel Kirilin <win10@list.ru>"]
 packages = [
-- 
GitLab