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

Optimize container build time #579

Merged
merged 1 commit into from
May 10, 2023
Merged

Conversation

aiven-anton
Copy link
Contributor

@aiven-anton aiven-anton commented Apr 9, 2023

About this change - What it does

  • Use cache mounts for pip and apt, heavily reducing network requests when cache is warm.
  • Remove git as a build-time dependency, other than switching to archive endpoints as was done in 39b4f08, this also requires either setting KARAPACE_VERSION as container build arg or building karapace/version.py independently.
  • Add hadolint pre-commit check for linting Dockerfile.
  • Move to using Python base images for builder and final stage. This allows omitting installation of some build tools. It also allows moving to a more recent Python version, no longer being bound by what's in distro repositories. Wheel availability of some of our Python dependencies prevents us from moving to 3.11 for now.
  • Change installation approach to construct a virtualenv in the builder step, and copying it unaltered to the final stage, with dependencies and Karapace itself installed in it. This allows having even fewer layers in the final stage, and is simpler.
  • Introduces a much stricter .dockerignore, ignoring files by default and explicitly including what's required. This makes sure changes in unrelated files does not evict layer cache. For example, a few files that previously erroneously evicted caches, because everything was included:
    • .git/*
    • .mypy_cache/*
    • container/Dockerfile itself
    • __pycache__/*
    • .idea/*

Image size remains the same as previously (the develop tag is the new version):

$ docker image ls
REPOSITORY                           TAG               IMAGE ID       CREATED          SIZE
ghcr.io/aiven/karapace               develop           30efca01fe54   6 seconds ago    203MB
ghcr.io/aiven/karapace               latest            ef8485cb1181   3 weeks ago      203MB

References #585.

Why this way

  • Removing git as a build-time dependency is a simplification.
  • Caching optimizes, makes building images faster.
  • Hadolint increases quality of Dockerfile, helps with maintainability.
  • Pinning apt requirements to adhere to a Hadolint rules. This ensures reproducible builds.

@aiven-anton aiven-anton marked this pull request as ready for review April 9, 2023 15:23
@aiven-anton aiven-anton requested review from a team as code owners April 9, 2023 15:23
@aiven-anton

This comment was marked as resolved.

@aiven-anton

This comment was marked as resolved.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented May 4, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 39ca83a
Status: ✅  Deploy successful!
Preview URL: https://e70aa090.karapace.pages.dev
Branch Preview URL: https://chore-optimize-container-bui.karapace.pages.dev

View logs

@aiven-anton aiven-anton force-pushed the chore/optimize-container-build branch from 493fbd0 to 0e0ce92 Compare May 4, 2023 09:20
@aiven-anton aiven-anton marked this pull request as ready for review May 4, 2023 09:58
@aiven-anton aiven-anton marked this pull request as draft May 4, 2023 16:28
@aiven-anton aiven-anton force-pushed the chore/optimize-container-build branch 2 times, most recently from 793e204 to 521bfca Compare May 4, 2023 16:33
@aiven-anton aiven-anton force-pushed the chore/optimize-container-build branch from 521bfca to 4956b2b Compare May 4, 2023 17:03
- Use cache mounts for pip and apt, heavily reducing network requests
  when cache is warm.
- Remove git as a build-time dependency, other than switching to archive
  endpoints as was done in 39b4f08, this also requires either setting
  `KARAPACE_VERSION` as container build arg or building
  karapace/version.py independently.
- Add hadolint pre-commit check for linting Dockerfile.
- Move to using Python base images for builder and final stage. This
  allows omitting installation of some build tools. It also allows
  moving to a more recent Python version, no longer being bound by
  what's in distro repositories. Wheel availability of some of our
  Python dependencies prevents us from moving to 3.11 for now.
- Change installation approach to construct a virtualenv in the builder
  step, and copying it unaltered to the final stage, with dependencies
  and Karapace itself installed in it. This allows having even fewer
  layers in the final stage, and is simpler.
- Introduces a _much_ stricter .dockerignore, ignoring files by default
  and explicitly including what's required. This makes sure changes in
  unrelated files does not evict layer cache. For example, a few files
  that previously erroneously evicted caches, because everything was
  included:
  - .git/*
  - .mypy_cache/*
  - container/Dockerfile itself
  - __pycache__/*
  - .idea/*
@aiven-anton aiven-anton force-pushed the chore/optimize-container-build branch from 4956b2b to 39ca83a Compare May 4, 2023 17:22
@aiven-anton aiven-anton marked this pull request as ready for review May 4, 2023 17:24
@tvainika tvainika merged commit 2083cf0 into main May 10, 2023
@tvainika tvainika deleted the chore/optimize-container-build branch May 10, 2023 09:11
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