-
Notifications
You must be signed in to change notification settings - Fork 0
Docker deployment setup #21
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
Open
ramonechen
wants to merge
26
commits into
main-preview
Choose a base branch
from
deployment-setup
base: main-preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
229e1ca
Create basic Dockerfile
ramonechen d5b8582
Add DB root password to .env and move to project root
ramonechen dec915d
Add Docker and Docker Compose files
ramonechen 0e540fd
Remove redundant variable names in Docker Compose
ramonechen 8e8d827
Restore .env variable interpolation
ramonechen 9531ebe
Fix DB health check passing prematurely
ramonechen b658cfe
Second fix to DB health check
ramonechen 201957a
Remove hardcoded variable values in Docker Compose
ramonechen c47f74e
Modify Black check to call shared workflow
ramonechen 3831cba
Enhance Dockerfile readability
ramonechen 5d59083
Create .dockerignore
ramonechen fc5b97c
Include shared dev Compose and refactor API service
ramonechen 1144b0e
Add clarifying comment to Dockerfile
ramonechen bb55705
Create image-publish.yml
ramonechen f532cd8
Make environment variables explicit
ramonechen 702ee35
Add comment in Compose to clarify possible warning
ramonechen 5a83d14
Remove mention of shared Docker network in Compose
ramonechen 2557b9b
Clarify example.env
ramonechen 7ab0892
Fill in README
ramonechen a2a62bf
Add --service-ports to Compose command
ramonechen 1137df4
Update README setup command
ramonechen 420a5ba
Add helpful runtime tips to README
ramonechen f540bf0
Remove DB_ROOT_PASSWORD from Compose
ramonechen ee772e7
Reduce layering in Dockerfile
ramonechen f7f6ec6
Add dev and prod stages to Dockerfile
ramonechen 3ed1060
Remove Compose watch directive
ramonechen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Ignore all files | ||
|
|
||
| * | ||
|
|
||
| # Whitelist necessary build files | ||
| !requirements.txt | ||
|
|
||
| # Allow Docker to read app directory | ||
| !app/ | ||
|
|
||
| # Ignore everything inside app | ||
| app/* | ||
|
|
||
| # Only allow .py files within app and its subdirectories | ||
| !app/**/*.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,11 @@ | ||
| name: Black Check | ||
| name: Black Formatting Check | ||
|
|
||
| # Run on every push and pull request | ||
| on: [push, pull_request] | ||
|
|
||
| jobs: | ||
| format-check: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v2 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v2 | ||
| with: | ||
| python-version: '3.12.2' | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install black | ||
|
|
||
| - name: Run Black check | ||
| run: black --check . | ||
|
|
||
| call-format-check: | ||
| uses: project-carpi/.github/.github/workflows/black-check.yml@main | ||
| permissions: | ||
| contents: read | ||
| secrets: inherit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| name: Build and Push Docker Image to GHCR | ||
|
|
||
| # Run this workflow every time code is pushed or merged to the main branch | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| call-build-and-push: | ||
| uses: project-carpi/.github/.github/workflows/image-publish.yml@main | ||
|
ramonechen marked this conversation as resolved.
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| secrets: inherit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| FROM python:3.12-slim AS base | ||
|
|
||
| # Git is needed to install the git-based dependency in requirements.txt | ||
| RUN apt-get update && \ | ||
| apt-get install -y git && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # Store all application files in /api | ||
| WORKDIR /api | ||
|
|
||
| # Copy the requirements file and install dependencies | ||
| COPY requirements.txt . | ||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| # Copy the rest of the application code into the container | ||
| COPY . . | ||
|
|
||
| # Indicate that the container should listen on port 8000 | ||
| EXPOSE 8000 | ||
|
|
||
| # --- DEVELOPMENT STAGE --- | ||
| FROM base as dev | ||
|
|
||
| # Run Uvicorn with reload enabled for development | ||
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] | ||
|
|
||
| ### --- PRODUCTION STAGE --- | ||
| FROM base as prod | ||
|
|
||
| # Run Uvicorn for production | ||
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,37 @@ | ||
| # CARPI-Backend-FastAPI | ||
| # CARPI Course Planner - API | ||
|
|
||
| FastAPI re-write of the old Spring Boot API. | ||
| This is the API service for the CARPI Course Planner application. It is designed to work in tandem with the [frontend](https://github.com/Project-CARPI/site) and [SIS scraper](https://github.com/Project-CARPI/sis-scraper). | ||
|
|
||
| ## Running the API | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - **[Docker](https://www.docker.com/products/docker-desktop/)** (Required): Used for the streamlined development environment setup. | ||
| - **[Python >= 3.12](https://www.python.org/)** (Optional): Useful for code editor linting or running the API locally without Docker. | ||
|
|
||
| ### Setup Instructions | ||
|
|
||
| **1. Configure Environment Variables** | ||
|
|
||
| Create a copy of the `example.env` file in the project root and rename it to `.env`. | ||
|
|
||
| The default values provided in the example file will work out of the box, but they can be customized as needed. | ||
|
|
||
| **2. Start the Development Environment** | ||
|
|
||
| With Docker Engine running, execute the following command in your terminal: | ||
|
|
||
| ```bash | ||
| docker compose run --rm --service-ports api && docker compose down | ||
| ``` | ||
|
|
||
| While the container is running: | ||
|
|
||
| - **Live Reloading:** Local files are mounted to the running container, so the API service will automatically respond to live changes in your source code. | ||
|
ramonechen marked this conversation as resolved.
|
||
| - **API Documentation:** A list of endpoint definitions and interactive Swagger documentation can be found at http://localhost:8000/docs. | ||
|
|
||
| ### About the Database Environment | ||
|
|
||
| - **Shared Database:** The development environment pulls a shared MySQL database service definition from an external repository, creating a local database volume. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we discussed, you should update this section to include the caveat of only one of the scraper and API being able to use the database container at once. |
||
| - **Data Persistence:** The test database volume persists between Docker start and stop cycles, ensuring test data remains available across development sessions and for other services that depend on the database. | ||
| - **Resetting the Database:** To completely rebuild the database from scratch, you must manually remove the corresponding Docker volume prior to starting the environment. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Docker Compose configuration for the CARPI API environment. | ||
| # | ||
| # This Compose file includes a shared development environment configuration | ||
| # from the Project-CARPI/.github repository, which contains a database service. | ||
| # | ||
| # To start the development environment, run: | ||
| # docker compose run --rm --service-ports api && docker compose down | ||
| # | ||
| # This will start both the shared development environment and the carpi-api | ||
| # container. After the API container is stopped, the "docker compose down" will | ||
| # stop the shared development environment. | ||
| # | ||
| # A warning may appear about the test database volume already existing. This is | ||
| # expected and can be safely ignored. | ||
| # | ||
| # The test database volume persists between Docker start and stop cycles so | ||
| # that test data remains available across both development sessions and other | ||
| # services that depend on the database. The volume must be manually removed in | ||
| # order to build the database from scratch. | ||
|
|
||
| name: carpi-api-dev-environment | ||
|
|
||
| include: | ||
| - https://github.com/Project-CARPI/.github.git#main:shared-dev-env/docker-compose.yml | ||
|
ramonechen marked this conversation as resolved.
|
||
|
|
||
| services: | ||
| api: | ||
| build: | ||
| context: . | ||
| target: dev | ||
| image: carpi-api:latest | ||
| container_name: carpi-api | ||
| # Ensure image is built locally and is not pulled from a registry | ||
| pull_policy: build | ||
| restart: on-failure | ||
| ports: | ||
| - "8000:8000" | ||
| environment: | ||
| DB_DIALECT: ${DB_DIALECT} | ||
| DB_API: ${DB_API} | ||
| # Hardcode the DB_HOSTNAME environment variable | ||
| DB_HOSTNAME: db:3306 | ||
| DB_USERNAME: ${DB_USERNAME} | ||
| DB_PASSWORD: ${DB_PASSWORD} | ||
| DB_SCHEMA: ${DB_SCHEMA} | ||
| # Mount local code over the code built into the container for automatic | ||
| # reloading during development. | ||
| volumes: | ||
| - .:/api | ||
| depends_on: | ||
| db: | ||
| condition: service_healthy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Create a file named ".env" in this directory with the following variables. | ||
| # If using Docker Compose for development, these values can be used as-is. | ||
|
|
||
| # This only needs to be set if the database is different from the one used | ||
| # in the Docker Compose environment. | ||
| DB_HOSTNAME="hostname:port" | ||
|
|
||
| # This root password is only used for instantiating the MySQL container in | ||
| # Docker Compose. | ||
| DB_ROOT_PASSWORD="password" | ||
|
|
||
| DB_DIALECT="mysql" | ||
| DB_API="mysqlconnector" | ||
| DB_USERNAME="username" | ||
| DB_PASSWORD="password" | ||
| DB_SCHEMA="schema" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.