Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Docker support for disability-max-ratings-api #23

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.git
.github
.gitignore
.env
*.pyc
__pycache__/
tests/
docs/
.dockerignore
Dockerfile
docker-compose.yml
.pytest_cache/
.coverage
htmlcov/
build/
dist/
*.egg-info/
1 change: 1 addition & 0 deletions .python-version
dfitchett marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.3
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Stage 1: Builder
FROM python:3.12.3-slim AS builder

WORKDIR /app

# Install system dependencies required for building
RUN apt-get update && \
apt-get install -y curl && \
rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN pip install --no-cache-dir poetry==1.7.1
dfitchett marked this conversation as resolved.
Show resolved Hide resolved

# Copy only the poetry files to leverage caching
COPY pyproject.toml poetry.lock ./

# Configure Poetry (no virtualenvs and silence export warnings)
RUN poetry config virtualenvs.create false && \
poetry config warnings.export false && \
poetry install --only main --no-interaction --no-ansi

# Stage 2: Runner
FROM python:3.12.3-slim AS runner

WORKDIR /app

# Install curl for healthcheck
RUN apt-get update && \
apt-get install -y curl && \
rm -rf /var/lib/apt/lists/* && \
useradd -m appuser

# Copy installed site-packages and Poetry from the builder stage
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin/poetry /usr/local/bin/poetry

# Copy the application code
COPY . .

# Set ownership to appuser
RUN chown -R appuser:appuser /app

# Switch to non-root user
USER appuser

# Expose the application port
EXPOSE 8130

# Run the application using Poetry
CMD ["poetry", "run", "uvicorn", "src.python_src.api:app", "--host", "0.0.0.0", "--port", "8130"]
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,53 @@ Using Poetry, run the following command from the root of the repository:
poetry run uvicorn src.python_src.api:app --port 8130 --reload
```

## Run the server with Docker
## Run with Docker

TODO: update this to use the new disability-max-ratings-api Docker Compose file - <https://github.com/department-of-veterans-affairs/abd-vro/issues/3833>
You can also run the service using Docker:

1. Build and start the container:
```bash
docker compose up --build
```

2. Test the API health:
```bash
curl http://localhost:8130/health
```

3. View API documentation:
```bash
curl http://localhost:8130/docs
```

4. Test the API endpoint with a sample request:
```bash
curl -X POST http://localhost:8130/disability-max-ratings/ \
-H "Content-Type: application/json" \
-d '{"diagnostic_codes": [6260]}'
```

Expected response:
```json
{
"ratings": [
{
"diagnostic_code": 6260,
"max_rating": 10
}
]
}
```

5. Monitor container health:
```bash
docker compose ps
```

6. Run tests in Docker:
```bash
docker compose run --rm api poetry run pytest
```

## Testing it all together

Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
api:
build: .
container_name: disability-max-ratings-api
ports:
- "8130:8130"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8130/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
restart: unless-stopped
environment:
- ENVIRONMENT=local
volumes:
- .:/app
security_opt:
- no-new-privileges:true
210 changes: 105 additions & 105 deletions poetry.lock

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,20 @@ exclude = [
name = "disability-max-ratings-api"
version = "0.1.0"
description = "API to get the maximum rating for a collection of disabilities."
authors = ["Department of Veterans Affairs"]
readme = "README.md"
license = "LICENSE.md"
package-mode = false
homepage = "https://github.com/department-of-veterans-affairs/disability-max-ratings-api"
repository = "https://github.com/department-of-veterans-affairs/disability-max-ratings-api"
keywords = ["veterans", "disability", "ratings", "api", "fastapi"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.12",
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
]

[tool.poetry.dependencies]
python = "3.12.3"
Expand Down
Loading