Skip to content

Nickez/slim down docker image #1268

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 2 commits into
base: master
Choose a base branch
from

Conversation

NickeZ
Copy link
Collaborator

@NickeZ NickeZ commented Aug 13, 2024

  1. Use python3 interpreter instead of python which is ambiguous.

  2. All temporary files need to be deleted in every RUN command to avoid adding it in a layer. Removing them in a later RUN command will still make them bloat the final image.

    Environment variables that are intended only for when the container is built should be prepended on the lines where they are needed to avoid polluting the environment in the running container.

@NickeZ NickeZ force-pushed the nickez/slim-down-docker-image branch from 702d3d1 to c967beb Compare August 16, 2024 08:13
@NickeZ NickeZ force-pushed the nickez/slim-down-docker-image branch 15 times, most recently from c2c9e50 to a6dff27 Compare August 27, 2024 08:55
@NickeZ NickeZ marked this pull request as ready for review August 27, 2024 10:02
@NickeZ NickeZ requested a review from benma August 27, 2024 10:03
@NickeZ NickeZ self-assigned this Aug 27, 2024
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

utACK, but prefer to keep cargo install for the two rust tools.

If your goal is to reduce layers, you could also move all of it to a docker_install.sh script and call that in a single RUN. @cstenglein has done that in this open PR #992, but somehow that PR was never merged.

@NickeZ
Copy link
Collaborator Author

NickeZ commented Sep 3, 2024

utACK, but prefer to keep cargo install for the two rust tools.

If your goal is to reduce layers, you could also move all of it to a docker_install.sh script and call that in a single RUN. @cstenglein has done that in this open PR #992, but somehow that PR was never merged.

I have a few goals I guess:

  • Reducing number of layers (this PR cuts layers in half, which I think is a decent improvement)
  • Reducing size of individual layers (Remove all temporary files in the same layer they are created so that they don't end up in the final container, mount files that are only needed temporarily)
  • Working towards a functionally reproducible container.
    • What is left here is to pin the ubuntu base image and remove apt upgrade. And then probably move all apt installs to a new "base image" that we almost never rebuild.
  • Increasing speed of building image. This is good when you are building many images, like if you are testing all different GCC toolchains. 2 minutes saved per build, might make you save 20 minutes if you test 10 different versions.

I do think you can take the released versions of bindgen and cbindgen where available. We aren't building any other tools from source. Perhaps we could build our own linux/arm64 versions and publish somewhere in case you don't want to have two approaches in the dockerfile.

The reason a reproducible container is important is so that we can build older versions of the firmware at all. Today, since the image isn't reproducible we depend on docker hub having a copy of all the versions. You can imagine some future version of a debian or pip package after some "security update" isn't compatible anymore.

I think calling out to a single install.sh script that installs everything is a bit extreme. If you then for example want to try out different toolchains or something you have to rebuild the whole image and cannot use any caching at all. Defeats one of the purposes of docker. I guess it depends a bit on what you do in the install script.

@benma
Copy link
Collaborator

benma commented Sep 5, 2024

The reason a reproducible container is important is so that we can build older versions of the firmware at all.

That would be nice, but I would rank this as low priority for two reasons:

  1. we address this problem directly here and gather signatures for a historic record

What is the purpose of this?

The purpose is twofold:

  • [...]
    The firmware binaries are only practically reproducible for a limited amount of time after the release. In the future, the dependencies of our Dockerfile might have changed, or Docker itself might even become incompatible or obsolete. This historic record allows you to be reasonably sure that the released binary was created correctly, even if it becomes infeasible to re-build the binary.
  1. Failing to reproduce old firmwares has never come up as an issue so far. Users I guess are interested the reproducibility of the firmware they want to run, which is usually the latest version.

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

utACK

Python should be invoked as `python3` since we are writing python3 code
and not python2. See https://peps.python.org/pep-0394/#recommendation
Many (most?) distributions do not distribute a "python" executable any
more, forcing you to pick either 2 or 3.
@NickeZ NickeZ force-pushed the nickez/slim-down-docker-image branch from a6dff27 to f9154b1 Compare April 6, 2025 08:44
All temporary files need to be deleted in every `RUN` command to avoid adding it in a layer. Removing them in a later `RUN` command will still make them bloat the final image.

Instead of copying files temporarily they should be mounted in the `RUN`
command where they are needed. This avoids the need to delete them at
the end of the command.

Environment variables that are intended only for when the container is built should be prepended on the lines where they are needed to avoid polluting the environment in the running container.

We should strive to have as few layers as possible, but still enough to
simplify debugging the container image. When you only have 1 layer it is
not possible to create intermediate images and inspect the state of the
image.
@NickeZ NickeZ force-pushed the nickez/slim-down-docker-image branch from f9154b1 to d51692f Compare April 6, 2025 08:59
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.

3 participants