It seems to me that racism (and/or) tribalism follows the same mechanism as gangs in prison and indeed finds its worst manifestation when people suffer a lack of freedom (whether social or economic).
In the 70’s as a young man living in Washington, DC, I saw a play by an Irish playwright entitled, “If We Didn’t Have Niggers We Would Have To Invent Them” which explored this dynamic from the the point of view of Irish immigrants to the USA facing discrimination and dealing with the shame that parroting the prevailing racism against blacks brought them while also helping them get a leg up economically.