From 7b0f104ca444f9be1f370ceae45f96a93d2cfbc9 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Wed, 26 Apr 2023 02:00:29 +0400 Subject: [PATCH 1/3] Fixed taskiq for docker-compose.dev.yaml. (#162) --- .../{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml index c6a2dfa..f793137 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml @@ -21,6 +21,6 @@ services: command: - taskiq - worker - - {{cookiecutter.project_name}}.taskiq:broker + - {{cookiecutter.project_name}}.tkq:broker - --reload {%- endif %} From 86fce026a85e9fec4cb9a1fe15ceb8fb4acb14ee Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Wed, 26 Apr 2023 02:03:31 +0400 Subject: [PATCH 2/3] Optimized image size (#163) --- .github/workflows/test.yml | 2 +- Dockerfile | 2 +- .../{{cookiecutter.project_name}}/.gitlab-ci.yml | 2 +- .../template/{{cookiecutter.project_name}}/README.md | 6 +++--- .../{{cookiecutter.project_name}}/deploy/Dockerfile | 12 ++++++++---- .../deploy/docker-compose.dev.yml | 2 ++ .../deploy/docker-compose.yml | 1 + fastapi_template/tests/utils.py | 10 ++++++++++ 8 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b95ed89..417bd85 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,7 +31,7 @@ jobs: - name: Install deps run: | pip install -U pip - pip install poetry==1.2.2 + pip install poetry==1.4.2 poetry install env: POETRY_VIRTUALENVS_CREATE: false diff --git a/Dockerfile b/Dockerfile index c6b8591..74d273d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,7 @@ WORKDIR /src ENV PATH ${PATH}:/home/fastapi_template/.local/bin -RUN pip install poetry==1.2.2 +RUN pip install poetry==1.4.2 COPY . /src/ RUN pip install . diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml b/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml index 40b17b8..9fc0c30 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml @@ -10,7 +10,7 @@ stages: except: - tags before_script: - - pip install poetry==1.2.2 + - pip install poetry==1.4.2 - poetry config virtualenvs.create false - poetry install diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/README.md b/fastapi_template/template/{{cookiecutter.project_name}}/README.md index 889eb9b..2c5eec5 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/README.md +++ b/fastapi_template/template/{{cookiecutter.project_name}}/README.md @@ -32,7 +32,7 @@ If you want to develop in docker with autoreload add `-f deploy/docker-compose.d Like this: ```bash -docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up +docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up --build ``` This command exposes the web application on port 8000, mounts current directory and enables autoreload. @@ -210,8 +210,8 @@ aerich migrate If you want to run it in docker, simply run: ```bash -docker-compose -f deploy/docker-compose.yml --project-directory . run --rm api pytest -vv . -docker-compose -f deploy/docker-compose.yml --project-directory . down +docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . run --build --rm api pytest -vv . +docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . down ``` For running tests on your local machine. diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile index aeea127..7ef4826 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile +++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9.6-slim-buster +FROM python:3.9.6-slim-buster as prod {%- if cookiecutter.db_info.name == "mysql" %} RUN apt-get update && apt-get install -y \ @@ -15,7 +15,7 @@ RUN apt-get update && apt-get install -y \ {%- endif %} -RUN pip install poetry==1.2.2 +RUN pip install poetry==1.4.2 # Configuring poetry RUN poetry config virtualenvs.create false @@ -25,7 +25,7 @@ COPY pyproject.toml poetry.lock /app/src/ WORKDIR /app/src # Installing requirements -RUN poetry install +RUN poetry install --only main {%- if cookiecutter.db_info.name == "mysql" or cookiecutter.db_info.name == "postgresql" %} # Removing gcc @@ -36,6 +36,10 @@ RUN apt-get purge -y \ # Copying actuall application COPY . /app/src/ -RUN poetry install +RUN poetry install --only main CMD ["/usr/local/bin/python", "-m", "{{cookiecutter.project_name}}"] + +FROM prod as dev + +RUN poetry install diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml index f793137..3a2c37c 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml @@ -5,6 +5,8 @@ services: ports: # Exposes application port. - "8000:8000" + build: + target: dev volumes: # Adds current directory as volume. - .:/app/src/ 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 534c4ce..36270cf 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml @@ -5,6 +5,7 @@ services: build: context: . dockerfile: ./deploy/Dockerfile + target: prod image: {{cookiecutter.project_name}}:{{"${" }}{{cookiecutter.project_name | upper }}_VERSION:-latest{{"}"}} restart: always env_file: diff --git a/fastapi_template/tests/utils.py b/fastapi_template/tests/utils.py index 4572342..bb09820 100644 --- a/fastapi_template/tests/utils.py +++ b/fastapi_template/tests/utils.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import shlex import subprocess from typing import Optional @@ -39,6 +40,15 @@ def run_docker_compose_command( def run_default_check(context: BuilderContext, without_pytest=False): generate_project_and_chdir(context) + compose = Path("./deploy/docker-compose.yml") + compose_contents = compose.read_text() + new_compose_lines = [] + for line in compose_contents.splitlines(): + if line.strip().replace(" ", "") == "target:prod": + continue + new_compose_lines.append(line) + compose.write_text("\n".join(new_compose_lines) + "\n") + assert run_pre_commit() == 0 if without_pytest: From 224f41bc68874ce3517ed94aeeccf9d08c4755fb Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Wed, 26 Apr 2023 02:26:30 +0400 Subject: [PATCH 3/3] Version bumped to 4.1.3. Signed-off-by: Pavel Kirilin --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ca87cdc..0510ff7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fastapi_template" -version = "4.1.2" +version = "4.1.3" description = "Feature-rich robust FastAPI template" authors = ["Pavel Kirilin "] packages = [{ include = "fastapi_template" }]