Skip to content

Commit

Permalink
Add a dockerfile and lines in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
metazool committed Oct 8, 2024
1 parent 7ef8af3 commit 659bf74
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
41 changes: 16 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
# Build virtualenv
FROM python:3.12-slim as build
WORKDIR /app
COPY pyproject.toml README.md /app/
COPY src /app/src
COPY .git /app/.git
RUN pip install --upgrade pip pdm
# Installs the codebase in editable mode into .venv
RUN pdm install

# Build production containerdocker
# Only the ./.venv ./src ./tests are present in the production image
FROM python:3.12-slim as prod
WORKDIR /app
RUN groupadd -g 999 python && \
useradd -m -r -u 999 -g python python
RUN chown python:python /app
COPY --chown=python:python --from=build /app/.venv /app/.venv
COPY --chown=python:python --from=build /app/src /app/src
COPY --chown=python:python tests/ /app/tests

USER python
ENV PATH="/app/.venv/bin:$PATH"
ENV VIRTUAL_ENV="/app/.venv"
CMD ["python", "-m", "os_api"]
FROM python:3.12


WORKDIR /code


COPY ./requirements.txt /code/requirements.txt


RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt


COPY ./src /code/app


CMD ["fastapi", "run", "app/os_api/api.py", "--port", "80"]
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,21 @@ AWS_URL_ENDPOINT="url goes here"

This will bring up the OpenAPI documentation on localhost:8080

### With uvicorn / nginx
### With `uvicorn` using the `fastapi` cli

TBD
`fastapi run --workers 4 src/os_api/api.py`

### In docker
### In a container

TBD if this proves useful - current deployment target is Posit Connect
#### docker

`docker build -t os_api .`
`docker run -f -p 80:80 os_api`

#### podman

`podman build -t os_api .`
`podman run --env-file=.env -p=8000 --expose=8000 os_api`

### Automatic Versioning

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies = [
"aioboto3",
"boto3",
"botocore",
"fastapi",
"fastapi[standard]",
"python-multipart",
"python-dotenv",
"uvicorn",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ click==8.1.7
# via uvicorn
dulwich==0.22.1
# via autosemver
fastapi==0.112.0
fastapi[standard]==0.112.0
# via os_api (pyproject.toml)
frozenlist==1.4.1
# via
Expand Down
4 changes: 2 additions & 2 deletions src/os_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Configure logging
logging.basicConfig(
filename="upload_logs.log", # Log file path on the server
level=logging.INFO, # Log level
level=logging.DEBUG, # Log level
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

Expand Down Expand Up @@ -168,7 +168,7 @@ async def upload(
tasks = [upload_file(bucket_name, key, file) for file in files]
await asyncio.gather(*tasks)
except Exception as e:
print("Error:", e)
logger.error("Error:", e)
return JSONResponse(status_code=500, content={str(e)})

end_time = perf_counter()
Expand Down

0 comments on commit 659bf74

Please sign in to comment.