Problem scenario
dnf or yum commands always fail when you use the --installroot flag. The destination of the --installroot directory do not matter. You see error messages about epel, version, 404 and reaching external servers.
Solution
Root cause: There is an invocation of chroot behind the scenes (according to https://bugzilla.redhat.com/show_bug.cgi?id=850686).
Procedures
The --installroot=/foo/bar
flag should always be used in conjunction with --releasever=X
where "X" is the major version of your RedHat server. To find out what version you are using, run this: sudo cat /etc/*-release
For RedHat 9 here is an example of how to install "foobar" in /opt (instead of wherever foobar would have been installed):
sudo dnf -y install foobar --installroot=/opt --releasever=9