From 7dc9cf9144a06fc8950ff6200021a922bd4ab726 Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Tue, 19 Nov 2024 15:26:56 +0700 Subject: [PATCH] add docker file --- Dockerfile | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 2 +- run.py | 3 ++- src/config.py | 1 + 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..429288e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,50 @@ +# Use the official Python 3.11 image from the Docker Hub +FROM python:3.11-slim as builder + +# Install Poetry +RUN pip install poetry + +# Set environment variables for Poetry +ENV POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_IN_PROJECT=1 \ + POETRY_VIRTUALENVS_CREATE=1 \ + POETRY_CACHE_DIR=/tmp/poetry_cache + +# Set the working directory +WORKDIR /app + +# Copy the Poetry configuration files +COPY pyproject.toml poetry.lock ./ + +# Install dependencies +RUN poetry install --no-dev --no-root + +# Use a new slim image for the runtime +FROM python:3.11-slim as runtime + +# Install necessary tools for running the app, including `make` +RUN apt-get update && apt-get install -y --no-install-recommends \ + make \ + && rm -rf /var/lib/apt/lists/* + +# Set environment variables for Poetry +ENV POETRY_VIRTUALENVS_IN_PROJECT=1 \ + PATH="/app/.venv/bin:$PATH" + +# Copy Poetry installation from builder +COPY --from=builder /app/.venv /app/.venv + +# Copy application files +COPY . /app/ + +# Delete Tests for production +RUN rm -rf /app/tests/ + +# Expose port for the application +EXPOSE 3005 + +# Set the working directory +WORKDIR /app + +# Run `make run` as the entry point +CMD ["make", "run"] diff --git a/Makefile b/Makefile index 8af83c5..429b7cd 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,4 @@ install: # Run the application run: - python bin/run.py \ No newline at end of file + python run.py \ No newline at end of file diff --git a/run.py b/run.py index df93966..09e853e 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,5 @@ import uvicorn +from src.config import PORT if __name__ == "__main__": - uvicorn.run("src.main:app", host="0.0.0.0", port=8081, reload=True) + uvicorn.run("src.main:app", host="0.0.0.0", port=int(PORT), reload=True) diff --git a/src/config.py b/src/config.py index 413652a..3311bd6 100644 --- a/src/config.py +++ b/src/config.py @@ -37,6 +37,7 @@ config = Config(".env") LOG_LEVEL = config("LOG_LEVEL", default=logging.WARNING) ENV = config("ENV", default="local") +PORT = config("PORT", cast=int, default=8000) # database