By default 'brew install' first runs an implicit 'brew update' when the last update was more than a few minutes ago, and it runs 'brew cleanup' automatically roughly every 30 days. Both are convenience features and both are hostile to reproducible builds and to CI images, because the same Dockerfile or setup script installs different versions on different days and can spend minutes updating before doing anything.
Set HOMEBREW_NO_AUTO_UPDATE=1 to suppress the implicit update and HOMEBREW_NO_INSTALL_CLEANUP=1 to stop the periodic cleanup. In CI also consider HOMEBREW_NO_ANALYTICS=1 and HOMEBREW_NO_ENV_HINTS=1 to quiet the output. These are environment variables, not config file settings, though Homebrew also reads them from a .env file in its config directory.
The automatic cleanup deletes older versions from the Cellar, which silently breaks anything that linked against a specific versioned path. If a build records an absolute path under Cellar rather than the opt symlink, that path disappears after the next cleanup and the binary fails to load its library.