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

refactor(docker): multi-arch docker 🐳 build overhaul πŸ—οΈ #258

Merged
merged 30 commits into from
Feb 10, 2025

Conversation

kaffolder7
Copy link
Collaborator

@kaffolder7 kaffolder7 commented Feb 8, 2025

Summary πŸš€

This PR includes multiple improvements and fixes related to Docker configuration, GitHub Actions workflows, and documentation. The key highlights are:

  • Fixed broken Docker build and improved security by running as a non-root user.
  • Refactored and optimized GitHub Actions workflows, adding better version tagging, security enhancements, and multi-platform support.
  • Updated Docker configurations to enhance stability, security, and maintainability, including version pinning, improved healthchecks, and reduced permissions.
  • Optimizes the Docker build to almost a 60-70% reduction in size!! πŸ’₯ Β Β (~1.5GB -> 315MB)
  • Expanded Docker documentation with improved README badges, detailed version tagging explanations, and an automated process to keep the README up-to-date.
  • General README improvements with proofreading, formatting fixes, and removal of broken icons.

Changes Made

Docker Improvements 🐳

  • Fixed build issues and enforced running as a non-root user for better security.
  • Version pinning for more stable and predictable builds.
  • Added container healthcheck to improve monitoring and reliability.
  • Refined permissions of key directories (/uploads and /database).
  • Introduced entrypoint.sh to handle startup behavior.
  • Properly mapped container timezone for consistency.
  • Removed unnecessary environment variables and improved ordering.
  • Added TODO comments for future improvements/considerations.

GitHub Actions Workflow Enhancements βš™οΈ

  • Refactored the publishImage.yml workflow:
    • Unified support for Alpine and Debian variants using a matrix strategy.
    • Improved tagging strategy for better semantic versioning and organization.
    • Implemented Cosign signing for security.
    • Optimized multi-platform builds and caching.
    • Allowed manual triggering for repo admin(s) to build images on demand.

Documentation & README Updates πŸ“–

  • Expanded Docker section in README:
    • Created a dynamic version table that updates automatically via a new GitHub job (update_readme).
    • Improved readability and structure of Docker-specific documentation.
  • General README refinements:
    • Fixed broken icons and misaligned table headers.
    • Proofread and polished content for clarity.

Impact 🌟

  • Security: Runs as a non-root user, implements Cosign signing, and restricts unnecessary permissions.
  • Stability: Version pinning ensures consistent builds, and healthchecks improve container reliability.
  • Maintainability: Simplified workflows, reduced complexity, and added automated updates to documentation.
  • Developer Experience: Improved documentation, tagging strategies, and the ability to manually trigger builds make the project easier to work with.

@rajnandan1 – I'm thinking this should probably be a new minor version bump (e.g. ver. 3.0.10 -> ver. 3.1.0)? Also, I did quite the overhaul on your existing GitHub Actions workflow. We'll probably wanna first test this as a β€˜dry-run’. πŸ˜…

kaffolder7 and others added 25 commits February 5, 2025 14:03
* Switch to multi-stage build pattern for smaller image size
* Add support for both Alpine and Debian variants via build args
* Change default image base to `node:23-slim` instead of using `node:23` (no need for full Debian base present in `node:23` since now prioritization is given to production-ready builds)
* Improve caching with --mount for npm dependencies
* Separate build and runtime dependencies
* Remove unnecessary Node.js packages in final stage
* Fix permissions on uploads/database directories
* Add proper scoping for build arguments
* Set NODE_ENV=production for better performance

This change reduces the final image size and improves build caching while adding flexibility to choose between Alpine and Debian base images.

Original: ~1.2GB
New Alpine: ~350MB
New Debian: ~450MB
Streamlined the GitHub `publishImage.yml` workflow with the following functionality:

* Handle both Alpine and Debian variants through matrix strategy
* Push to both Docker Hub and GitHub Container Registry
* Add comprehensive tagging strategy, handling both branches (aka release version, e.g. 1.0.0), semantic versions (major.minor and major), and latest versions (`latest` and `alpine`)
* Add security aspects (cosign signing, proper permissions)
* Add better caching and multi-platform build settings

With this revised workflow, the following Docker image variants will be built for every successful release. As an example, if the release version is β€œ3.0.9”, then the following Docker image variants will be built:

Debian variants (default):
- `kener:3.0.9` (Semver of current release)
- `kener:latest` (Latest Debian release, ’latest’ label points to 3.0.9)
- `kener:3.0` (major.minor version, major.minor β€˜3.0’ label points to 3.0.9)
- `kener:3` (major version, major β€˜3’ label points to 3.0.9)

Alpine variants (smallest filesize):
- `kener:3.0.9-alpine` (Semver of current release)
- `kener:alpine` (Latest Alpine release, β€˜alpine’ label points to 3.0.9)
- `kener:3.0-alpine` (major.minor version, major.minor β€˜3.0-alpine’ label points to 3.0.9)
- `kener:3-alpine` (major version, major β€˜3-alpine’ label points to 3.0.9)
* add: version pinning (better stability)
* remove: unnecessary KENER_BASE_PATH env. variable
* update: reduce permissions of /uploads and /database directories
* add: `entrypoint.sh` file
* add: properly map container timezone and localtime
* add: container healthcheck
* change: restrict to non-root β€œnode” user
* Expanded upon existing Docker README section.
* Created table which will contains version placeholder variables that will be replaced by new GitHub workflow job: β€œupdate_readme”. Job automatically runs after new images have been built & pushed to container registries.
add direct links to filtered image(s) on Docker Hub, based on whether Debian or Alpine Linux variant badges are clicked
@kaffolder7
Copy link
Collaborator Author

Alright @rajnandan1, I was able to update and quickly test including only the files you mentioned in discussion #247. Please review and let me know if you have any questions! I'm happy to coordinate a time to jump on Discord if that's easier. I know you had mentioned the Docker side of things isn't necessarily something you're most comfortable with, so I'm happy to try and continuing to support as I have the time, if you would like. ☺️

kaffolder7 and others added 2 commits February 9, 2025 01:04
Noticed when doing some cleanup, that you had two awesome tags, but they both point to different URLs/repos. I added back in the one I had inadvertently removed.
@kaffolder7 kaffolder7 changed the title refactor(docker): multi-arch docker build overhaul refactor(docker): multi-arch docker 🐳 build overhaul πŸ—οΈ Feb 10, 2025
@rajnandan1
Copy link
Owner

Will merge this tonight IST

@kaffolder7
Copy link
Collaborator Author

Thanks @rajnandan1! This is so very exciting!! Thank you for trusting me with your project. I am very happy to support it. You've got a very good thing started. 😊

I caught an issue where the README will only auto-update listed Docker versions the first time. Commenting out for now (in case this PR gets merged before I have time to fix this). Will revisit this and fix this week.
πŸ”„ Automate README Generation via Mustache Templating

- Use Mustache to dynamically generate `README.md` from `README.template.md`.
- Populate README with environment variables (e.g., `KENER_BUILD_FULL_VERSION`).
- Prevent direct edits to `README.md` by enforcing updates via the template.
- Enhance GitHub Actions workflow to auto-generate and commit the README.
- Add GitHub Action workflow (`protect-readme.yml`) to prevent others from direct updates to `README.md` via PR.
@rajnandan1 rajnandan1 merged commit 8bde322 into rajnandan1:main Feb 10, 2025
1 check passed
@rajnandan1
Copy link
Owner

rajnandan1 commented Feb 10, 2025

@kaffolder7 the build seems to be broken https://github.com/rajnandan1/kener/actions/runs/13245732187
can you get this fixed?

@kaffolder7
Copy link
Collaborator Author

@kaffolder7 the build seems to be broken https://github.com/rajnandan1/kener/actions/runs/13245732187 can you get this fixed?

Yes, let me take a look here.

@rajnandan1
Copy link
Owner

@kaffolder7 the build seems to be broken https://github.com/rajnandan1/kener/actions/runs/13245732187 can you get this fixed?

Yes, let me take a look here.

It has something to do with your latest commit where mustache is used to create readme

@kaffolder7
Copy link
Collaborator Author

@rajnandan1 – Maybe issue as simple as me forgetting to push an updated package-lock.json file? Could you try regenerating this and push it to this PR and then re-run the workflow? I'm currently AFK for a bit.

@rajnandan1
Copy link
Owner

@rajnandan1 – Maybe issue as simple as me forgetting to push an updated package-lock.json file? Could you try regenerating this and push it to this PR and then re-run the workflow? I'm currently AFK for a bit.

Did :(

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.

2 participants