From 3543db012c7d399272c44d211bb93adde43c75db Mon Sep 17 00:00:00 2001 From: Raniro Coelho Date: Thu, 7 Aug 2025 22:33:53 -0300 Subject: [PATCH] Add Docker env template and GitHub workflow --- .github/workflows/docker.yml | 25 ++++++++++++++++++++++++ README.md | 14 ++++++++++++++ api/.dockerignore | 5 +++++ api/.env.example | 5 +++++ api/Dockerfile | 37 +++++++++++------------------------- 5 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/docker.yml create mode 100644 api/.dockerignore create mode 100644 api/.env.example diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..579b597 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,25 @@ +name: Build and Run API Docker image + +on: + push: + paths: + - 'api/**' + - '.github/workflows/docker.yml' + pull_request: + paths: + - 'api/**' + - '.github/workflows/docker.yml' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build API image + run: docker build -t is-replicant-api -f api/Dockerfile api + - name: Run container + run: | + docker run -d --rm --name api -e DATABASE_URL=sqlite:///./test.db -e SECRET_KEY=test -p 8000:8000 is-replicant-api + sleep 10 + curl --fail http://localhost:8000/ || (docker logs api && exit 1) + docker stop api diff --git a/README.md b/README.md index 29117dd..57cbec2 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,20 @@ Swagger documentation: - ReDoc: - OpenAPI JSON: +## Production Deployment + +Build a standalone API image and run it with an environment file: + +```bash +cp api/.env.example api/.env # update values for your environment +docker build -t is-replicant-api -f api/Dockerfile api +docker run --env-file api/.env -p 8000:8000 is-replicant-api +``` + +The repository includes a sample GitHub Actions workflow in +`.github/workflows/docker.yml` that builds the container and starts it with +temporary environment variables. + ## Testing Install dependencies and run the test suite: diff --git a/api/.dockerignore b/api/.dockerignore new file mode 100644 index 0000000..9aea7f0 --- /dev/null +++ b/api/.dockerignore @@ -0,0 +1,5 @@ +__pycache__ +*.pyc +.env +.env.* +tests diff --git a/api/.env.example b/api/.env.example new file mode 100644 index 0000000..7fc749b --- /dev/null +++ b/api/.env.example @@ -0,0 +1,5 @@ +# Example environment variables for is-Replicant API +DATABASE_URL=postgresql://user:password@db:5432/is_replicant +SECRET_KEY=change-me +ALGORITHM=HS256 +ACCESS_TOKEN_EXPIRE_MINUTES=30 diff --git a/api/Dockerfile b/api/Dockerfile index 7c26d38..fe828c5 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -1,38 +1,23 @@ -# Use a slim variant of Python for smaller image size -FROM python:3.12-slim +# syntax=docker/dockerfile:1 +FROM python:3.12-slim AS base + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 -# Set the working directory WORKDIR /app -# Copy the requirements file first to leverage Docker cache COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt && \ + addgroup --system appgroup && \ + adduser --system --ingroup appgroup appuser && \ + mkdir -p /app/nltk_data && \ + python -m nltk.downloader -d /app/nltk_data punkt -# Install dependencies -RUN pip install --no-cache-dir -r requirements.txt - -# Create a non-root user and group -RUN addgroup --system appgroup && adduser --system appuser --ingroup appgroup - -# Create the NLTK data directory -RUN mkdir -p /app/nltk_data - -# Download NLTK data -RUN python -m nltk.downloader -d /app/nltk_data punkt - -# Set proper permissions -RUN chown -R appuser:appgroup /app - -# Copy the application code COPY ./app /app/app -# Set the non-root user to run the application USER appuser - -# Set environment variable for NLTK data directory ENV NLTK_DATA=/app/nltk_data -# Expose the port FastAPI will run on EXPOSE 8000 -# Default command to run the application -CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]