Skip to content

chore: optimize dockerfile #61

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

gastonva
Copy link
Contributor

@gastonva gastonva commented Jul 8, 2025

Type of change

  • Fix
  • Story
  • Chore

Description of the change

In a recent project, we discovered that our Docker image was unnecessarily large and somewhat inefficient, so we implemented a few improvements.

The first change involved optimizing layer handling and cache usage. Every instruction in a Dockerfile creates a layer that Docker automatically caches. When we copied the entire project in one step, any change to any file would invalidate the cache and trigger a full rebuild. However, package installation does not need to be repeated if dependencies haven’t changed. To take advantage of layer caching, we split the copy step into two: first we copy only the pyproject.toml and uv.lock files and install dependencies, and then we copy the rest of the project. This allows Docker to reuse the cached dependency layer whenever those files remain unchanged.

We also reduced the APT footprint by skipping recommended packages and installing only the ones we actually need.

Finally, we could add even more caching by using BuildKit via docker buildx. Enabling BuildKit in a devcontainer requires installing or configuring docker buildx on the host, which may complicate the developer workflow, but we can discuss whether the benefits outweigh the extra setup.

Related PRs

N/A

@m-revetria
Copy link
Member

@gastonva could you share the a size difference between the previous docker image size and new one. Also, I'd like to see the output of docker scout

@gastonva
Copy link
Contributor Author

@mrevetria-sp

  • docker optimization.md
    • The deployment image size increased by 94MB with this optimization.
    • However, when using devcontainers during the development phase, the image is 253MB smaller.
    • This report also includes the docker scout recommendations for both the original and optimized Dockerfiles.
  • The Docker Scout CVE reports for both versions:

I suppose that for this particular template, the optimizations are not that significant. In the SCJ project, we managed to reduce the image size from around 2.8GB to 1.9GB.

I'm not an expert in Docker, but my assumption is that the larger the original image size, the more impactful these optimizations become, and possibly others as well.

Not sure if this PR makes sense anymore, you tell me.

@m-revetria
Copy link
Member

@mrevetria-sp

  • docker optimization.md

    • The deployment image size increased by 94MB with this optimization.
    • However, when using devcontainers during the development phase, the image is 253MB smaller.
    • This report also includes the docker scout recommendations for both the original and optimized Dockerfiles.
  • The Docker Scout CVE reports for both versions:

I suppose that for this particular template, the optimizations are not that significant. In the SCJ project, we managed to reduce the image size from around 2.8GB to 1.9GB.

I'm not an expert in Docker, but my assumption is that the larger the original image size, the more impactful these optimizations become, and possibly others as well.

Not sure if this PR makes sense anymore, you tell me.

Changes look good to me, I was just curious about the final results.

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.

4 participants