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

Conversation

gabezurita
Copy link
Collaborator

@gabezurita gabezurita commented Dec 19, 2024

Add Docker Support for Disability Max Ratings API

Closes: #3833

This PR adds Docker support for the disability-max-ratings-api service, providing optimized configurations for both production and development environments.

Changes

  • Dockerfile:

    • Multi-stage build for reduced production image size
    • Runs as non-root user for improved security
    • Minimal dependencies for production
  • docker-compose.yml:

    • Separate services for production (api) and development (api-dev)
    • Health monitoring via a dedicated /health endpoint
  • .dockerignore:

    • Improves build performance by excluding unnecessary files
  • pyproject.toml:

    • Updated Poetry configuration with comprehensive metadata
  • README.md:

    • Detailed instructions for building, running, and testing the service in both environments

Production Setup

  • Uses Python 3.12.3 slim image
  • Employs multi-stage builds for smaller images
  • Health checks enabled
  • Security features: non-root user, no-new-privileges

Development Setup

  • Includes all development tools and dependencies
  • Local directory mounting for live code changes
  • Supports running tests and dev tasks directly in the container

Testing

  1. Build & Start Services

    docker compose down
    docker compose build --no-cache
    docker compose up -d
    docker compose ps

    Expected: disability-max-ratings-api running on port 8130

  2. Check Endpoints

    # API docs
    curl http://localhost:8130/docs
    
    # Health endpoint
    curl http://localhost:8130/health
    
    # Main endpoint
    
     curl -X POST 'http://localhost:8130/disability-max-ratings' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"diagnostic_codes": [6260]}'
    

    Expected main endpoint response:

    {
        "ratings": [
            { "diagnostic_code": 6260, "max_rating": 10 }
        ]
    }
  3. Development Environment

     # Run tests inside container using Poetry
     docker compose run --rm api poetry run pytest
     
     # Check user
     docker compose run --rm api id

    Expected: All tests pass (>80% coverage), user should be non-root (uid=1000)

  4. Production Environment

    docker compose exec api id
    docker compose ps

    Expected: Non-root user in production container, container status "healthy"

  5. Local Development Checks

    poetry run pre-commit run --all-files
  6. Cleanup

    docker compose down

Notes

  • Pre-commit checks run locally (not in container)
  • The api-dev service is optional and only needed for development tasks
  • Changes in local files are instantly reflected inside the dev container

This PR adds Docker support to run the disability-max-ratings-api service in a containerized environment.

- Added `Dockerfile` with Python 3.12.3 and Poetry setup
- Added `docker-compose.yml` for local development
- Added healthcheck to monitor service availability
- Fixed Poetry configuration in `pyproject.toml`
  - Added required `authors` field
  - Removed unsupported `package-mode` field
- Updated the README.MD accordingly

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

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

3. 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
        }
    ]
}
```

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

- Uses Python slim base image to minimize container size
- Installs only production dependencies with `--only main`
- Configures Poetry to run without virtual environments in container
- Exposes port 8130 for API access
- Includes curl for healthcheck functionality
- Mounts local directory for development convenience

- Runs with `no-new-privileges` security option
- Cleans up apt cache to reduce image size
- Uses official Python base image
@gabezurita gabezurita marked this pull request as ready for review December 19, 2024 01:36
@gabezurita gabezurita requested review from a team as code owners December 19, 2024 01:36
@gabezurita gabezurita enabled auto-merge (squash) December 19, 2024 01:38
This commit adds Docker support to the disability-max-ratings-api with a focus on
security, maintainability, and developer experience.

Key Changes:
- Add multi-stage Dockerfile with non-root user for security
- Add docker-compose.yml with health monitoring
- Create .dockerignore for build optimization
- Update pyproject.toml with comprehensive metadata
- Update README with Docker setup and testing instructions

Technical Details:
- Uses Python 3.12.3 slim base image
- Implements multi-stage build to reduce final image size
- Runs container as non-root user 'appuser'
- Configures health monitoring with dedicated endpoint
- Mounts local directory for development convenience
- Adds security options (no-new-privileges)

Next Steps:
- Add health endpoint to src/python_src/api.py
- Update tests to include health endpoint coverage

Testing:
```bash
docker compose up --build

docker compose run --rm api poetry run pytest
```

Closes #ISSUE_NUMBER</parameter>
brostk
brostk previously approved these changes Dec 19, 2024
@gabezurita gabezurita requested a review from dfitchett December 19, 2024 17:41
.python-version Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
- Update Dockerfile for better dependency management:
  * Upgrade Poetry to 1.8.5 and add export plugin
  * Configure Poetry to avoid virtualenv creation
  * Copy all binaries and config from builder stage
  * Improve user permissions and security

- Enhance docker-compose.yml:
  * Add PYTHONPATH environment variable
  * Keep security options and healthcheck

- Update README with comprehensive Docker testing guide:
  * Add structured testing steps
  * Include expected outputs
  * Add development and production environment checks
  * Improve formatting and readability

- Update dependencies:
  * Upgrade ruff to 0.8.4
@gabezurita
Copy link
Collaborator Author

@dfitchett and @brostk, this is ready for review! Please run the testing steps on your machine and let me know if the Docker setup makes sense.

@gabezurita gabezurita removed the request for review from Ponnia-M December 19, 2024 21:47
@gabezurita gabezurita self-assigned this Dec 19, 2024
it adds no value
dfitchett
dfitchett previously approved these changes Dec 19, 2024
@gabezurita gabezurita disabled auto-merge December 19, 2024 23:18
@gabezurita gabezurita merged commit ec7dc1c into main Dec 19, 2024
3 checks passed
@gabezurita gabezurita deleted the add-docker-compose branch December 19, 2024 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants