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

[ENH] Move to Docker multistage builds #450

Merged
merged 9 commits into from
Jan 29, 2025
Merged

[ENH] Move to Docker multistage builds #450

merged 9 commits into from
Jan 29, 2025

Conversation

rmanaem
Copy link
Contributor

@rmanaem rmanaem commented Jan 24, 2025

Changes proposed in this pull request:

  • Added ENV variables with placeholder values to the Dockerfile
    • these values are meant to be replaced every time a container starts up
  • Added entrypoint script that replaces placeholder values
    • entrypoint won't let container start unless required .env variables are provided

Other note: plan is to release from this PR

NOTE: If this pull request is to be released, the release label must be applied once the review process is done to avoid the local and remote from going out of sync as a consequence of the bump version workflow run

Checklist

This section is for the PR reviewer

  • PR has an interpretable title with a prefix ([ENH], [FIX], [REF], [TST], [CI], [MNT], [INF], [MODEL], [DOC]) (see our Contributing Guidelines for more info)
  • PR has a label for the release changelog or skip-release (to be applied by maintainers only)
  • PR links to GitHub issue with mention Closes #XXXX
  • Tests pass
  • Checks pass
  • If the PR changes the participant-level and/or the dataset-level result file, the query-tool-results files in the neurobagel_examples repo have been regenerated

For new features:

  • Tests have been added

For bug fixes:

  • There is at least one test that would fail under the original bug conditions.

Summary by Sourcery

Switch the base image to node:20-alpine and copy the built files to an nginx:alpine image. Inject a custom plausible link to the docker container at runtime using environment variables.

Build:

  • Use a multi-stage build to reduce the final image size.

Deployment:

  • Add an entrypoint script to inject a custom plausible link if the PLAUSIBLE_DOMAIN and PLAUSIBLE_SRC environment variables are set.

Summary by Sourcery

Update the Dockerfile to use a multi-stage build, switching to node:20-alpine for building and nginx:alpine for serving. Implement dynamic injection of environment variables at runtime via an entrypoint script, enabling customization of plausible link, authentication settings, and other configurations.

Build:

  • Use a multi-stage build to reduce the final image size.

Deployment:

  • Add an entrypoint script to inject environment variables at runtime.

Copy link

sourcery-ai bot commented Jan 24, 2025

Reviewer's Guide by Sourcery

This pull request modifies the Dockerfile to enable the injection of a custom plausible link at runtime using environment variables. It also introduces an entrypoint script to handle the replacement of placeholder values in the built application.

Sequence diagram for container startup and environment variable injection

sequenceDiagram
    participant User
    participant Container
    participant Entrypoint
    participant Files
    User->>Container: Start container with env vars
    Container->>Entrypoint: Execute entrypoint.sh
    Entrypoint->>Entrypoint: Check required env vars
    alt Missing NB_API_QUERY_URL
        Entrypoint-->>Container: Exit with error
    else All required vars present
        Entrypoint->>Files: Replace placeholders in JS/HTML
        Files-->>Entrypoint: Files updated
        Entrypoint->>Container: Start Nginx
    end
Loading

Flow diagram for environment variable processing

flowchart TD
    A[Start Container] --> B{Check NB_API_QUERY_URL}
    B -->|Not Set| C[Exit with Error]
    B -->|Set| D[Process Files]
    D --> E[Replace API URL]
    E --> F[Replace Auth Settings]
    F --> G[Replace Client ID]
    G --> H[Replace Chatbot Settings]
    H --> I[Replace Base Path]
    I --> J[Replace Header Script]
    J --> K[Start Nginx]
    K --> L[End]
Loading

File-Level Changes

Change Details Files
Modified the Dockerfile to use a multi-stage build process and include environment variables for runtime configuration.
  • Switched the base image from node:20 to node:20-alpine for the build stage.
  • Added a new stage using nginx:alpine as the base image.
  • Copied the built application from the build stage to the nginx stage.
  • Added ENV variables with placeholder values for runtime configuration.
  • Exposed port 80 instead of 5173.
Dockerfile
Added an entrypoint script to replace placeholder values in the built application with environment variables.
  • Created a new entrypoint.sh script.
  • The script checks if the required environment variables are set.
  • The script uses sed to replace placeholder values in .js and .html files with the values of the environment variables.
  • The script executes the command passed to it after the replacements are done.
entrypoint.sh
Added a placeholder for the plausible script in the index.html file.
  • Added a placeholder comment <!-- REPLACE_ME_NB_QUERY_HEADER_SCRIPT_REPLACE_ME --> in index.html to inject the plausible script.
index.html

Assessment against linked issues

Issue Objective Addressed Explanation
#433 Implement multi-stage Docker builds to reduce image size and container startup time
#433 Provide a way to configure .env variables at runtime that replace default values in the built app
#433 Create a process to search-replace specific place-holder strings after the app is built

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

netlify bot commented Jan 24, 2025

Deploy Preview for neurobagel-query ready!

Name Link
🔨 Latest commit 751d981
🔍 Latest deploy log https://app.netlify.com/sites/neurobagel-query/deploys/67998c0305d5dd0008ff0997
😎 Deploy Preview https://deploy-preview-450--neurobagel-query.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@surchs surchs marked this pull request as ready for review January 28, 2025 03:59
@surchs
Copy link
Contributor

surchs commented Jan 28, 2025

@sourcery-ai review

@surchs surchs changed the title [ENH] Enabled possiblity of injecting custom plausible link to the docker container at runtime [ENH] Move to Docker multistage builds Jan 28, 2025
@rmanaem rmanaem requested review from surchs and removed request for surchs January 28, 2025 21:24
@rmanaem rmanaem self-assigned this Jan 28, 2025
Copy link
Contributor Author

@rmanaem rmanaem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing this @surchs! 🧑‍🍳

@rmanaem
Copy link
Contributor Author

rmanaem commented Jan 28, 2025

Don't forget to add a PR label and a release label before merging.

dependabot bot and others added 2 commits January 28, 2025 17:44
Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.4.14 to 6.0.11.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v6.0.11/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Arman Jahanpour <[email protected]>
@surchs surchs added release Create a release when this PR is merged pr-internal Non-user-facing code improvement, will increment patch version when merged (0.0.+1) labels Jan 29, 2025
@surchs surchs enabled auto-merge (squash) January 29, 2025 03:14
@surchs surchs merged commit 4a7f8cc into main Jan 29, 2025
14 checks passed
@surchs surchs deleted the enh-433 branch January 29, 2025 03:15
@alyssadai
Copy link
Contributor

For some reason it looks like this PR didn't trigger a release as expected...not sure why ☹️

@alyssadai alyssadai added released This issue/pull request has been released. and removed release Create a release when this PR is merged labels Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag:check Issue needs attention before further action pr-internal Non-user-facing code improvement, will increment patch version when merged (0.0.+1) released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setup Docker multi-stage builds for env runtime changes
3 participants