Slimming down installation for Docker images

I want to use Linuxbrew to install fresh software when building a Docker image.

Size is crucial for Docker images, so I was wondering how can I slim down the Linuxbrew size after packages installation?

So far I’m doing

rm -rf "$(/home/linuxbrew/.linuxbrew/bin/brew --cache)"

However, that’s still leaving quite a significant amount of unnecessary data in the image, mainly homebrew/core tap. How can I safely delete this data? Unfortunately, untapping homebrew/core isn’t allowed.

I think homebrew might not be the best tool to minimize install size. A lot of formula installs contain things like examples. If you do need to use homebrew I think you’re better off copying over the installs to a new container after installing through homebrew.

Sure, it’s not the best tool to minimise install size, and I wouldn’t include Homebrew itself in production images, however, in my particular use-case, it makes sense, since I’m building developer containers for VS Code, and it’s convenient to install packages via Homebrew for devs (especially, since some tools outdated or outright not available in the Linux distribution packages).

So I’m wondering what can be safely deleted. I’ve just tried:

rm -rf /home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/*

and it seems to work great, Homebrew just redownloads taps on the next run.

Wouldn’t using the tool containers make more sense then? I’m still not sure why you’d want to break parts of Homebrew to save some space on a developer machine if you can just take the minimal tool installs instead. The base for the Homebrew container is an Ubuntu image so I would bet you can save more space in all of Ubuntu than in Homebrew.

Wouldn’t using the tool containers make more sense then?

Yes, but I’m building such a container for a project I’m working on. I can’t use Ubuntu Homebrew image, because it needs to be based on the same image, my production containers are based one.

What I’m doing is taking our production image and install a bunch of tools for the devs in it. I would like Homebrew to be one of such tools while minimizing its footprint inside the image.

If you don’t need to run brew again you can just rm -rf $(brew --repo) which saves about 500 mb.

If you do need to run brew again, you can remove and re-clone the git repositories as shallow clones, but this incurs a really hefty performance penalty on all brew update operations. At this point you might as well just include the full clone on the Docker image and save yourself the hassle.

2 Likes