From 8e23f63d362599a736dff2d5c82b91b094ca81fe Mon Sep 17 00:00:00 2001 From: BluemediaGER Date: Sat, 20 Apr 2024 03:07:54 +0200 Subject: [PATCH] Add Dockerfile --- Dockerfile | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ poetry.lock | 34 +++++++++++++++++++++++++++- pyproject.toml | 1 + 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1510179 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,61 @@ +# `python-base` sets up all our shared environment variables +FROM python:3.11-slim as python-base + + # python +ENV PYTHONUNBUFFERED=1 \ + # prevents python creating .pyc files + PYTHONDONTWRITEBYTECODE=1 \ + \ + # pip + PIP_NO_CACHE_DIR=off \ + PIP_DISABLE_PIP_VERSION_CHECK=on \ + PIP_DEFAULT_TIMEOUT=100 \ + \ + # poetry + # https://python-poetry.org/docs/configuration/#using-environment-variables + # make poetry install to this location + POETRY_HOME="/opt/poetry" \ + # make poetry create the virtual environment in the project's root + # it gets named `.venv` + POETRY_VIRTUALENVS_IN_PROJECT=true \ + # do not ask any interactive question + POETRY_NO_INTERACTION=1 \ + \ + # paths + # this is where our requirements + virtual environment will live + PYSETUP_PATH="/opt/pysetup" \ + VENV_PATH="/opt/pysetup/.venv" + + +# prepend poetry and venv to path +ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" + + +# `builder-base` stage is used to build deps + create our virtual environment +FROM python-base as builder-base +RUN apt-get update \ + && apt-get install --no-install-recommends -y \ + # deps for installing poetry + curl \ + # deps for building python deps + build-essential + +# install poetry - respects $POETRY_VERSION & $POETRY_HOME +RUN curl -sSL https://install.python-poetry.org | python3 - + +# copy project requirement files here to ensure they will be cached. +WORKDIR $PYSETUP_PATH +COPY poetry.lock pyproject.toml ./ + +# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally +RUN poetry install --no-dev + +# `production` image used for runtime +FROM python-base as production +ENV FASTAPI_ENV=production +COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH +COPY alembic.ini /usr/src/ +COPY ./alembic /usr/src/alembic +COPY ./app /usr/src/app +WORKDIR /usr/src +CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "app.main:app"] \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 07231f9..820f6ae 100644 --- a/poetry.lock +++ b/poetry.lock @@ -184,6 +184,27 @@ files = [ docs = ["Sphinx", "furo"] test = ["objgraph", "psutil"] +[[package]] +name = "gunicorn" +version = "22.0.0" +description = "WSGI HTTP Server for UNIX" +optional = false +python-versions = ">=3.7" +files = [ + {file = "gunicorn-22.0.0-py3-none-any.whl", hash = "sha256:350679f91b24062c86e386e198a15438d53a7a8207235a78ba1b53df4c4378d9"}, + {file = "gunicorn-22.0.0.tar.gz", hash = "sha256:4a0b436239ff76fb33f11c07a16482c521a7e09c1ce3cc293c2330afe01bec63"}, +] + +[package.dependencies] +packaging = "*" + +[package.extras] +eventlet = ["eventlet (>=0.24.1,!=0.36.0)"] +gevent = ["gevent (>=1.4.0)"] +setproctitle = ["setproctitle"] +testing = ["coverage", "eventlet", "gevent", "pytest", "pytest-cov"] +tornado = ["tornado (>=0.2)"] + [[package]] name = "h11" version = "0.14.0" @@ -391,6 +412,17 @@ files = [ [package.dependencies] jsonschema = ">=4.4.0,<5.0.0" +[[package]] +name = "packaging" +version = "24.0" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "packaging-24.0-py3-none-any.whl", hash = "sha256:2ddfb553fdf02fb784c234c7ba6ccc288296ceabec964ad2eae3777778130bc5"}, + {file = "packaging-24.0.tar.gz", hash = "sha256:eb82c5e3e56209074766e6885bb04b8c38a0c015d0a30036ebe7ece34c9989e9"}, +] + [[package]] name = "pydantic" version = "2.6.4" @@ -1064,4 +1096,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "2b9d4a1a7ceaa46c78814156552aea406a0683f55a574d198d8a65917b476e40" +content-hash = "cb2045d464e0dbbb2edb47aa09012fe4c5cdd46f70c3c1aabee1b19e5a86e7dd" diff --git a/pyproject.toml b/pyproject.toml index c4ccb64..7ea56d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ uvicorn = {extras = ["standard"], version = "^0.28.0"} websockets = "^12.0" sqlalchemy = "^2.0.28" alembic = "^1.13.1" +gunicorn = "^22.0.0" [build-system] requires = ["poetry-core"]