Why is 'doing the right thing' followed by the preposition 'by' here?

No, you cannot use with here. That’s because to do right by means, per the OED:

to do right by: to treat fairly or honourably; to do one's duty by.

They provide various citations starting more than 200 years ago, but of which the two most recent are:

  • 1994 Denver Post 21 Aug. b9/2
    The team has chosen to do right by the city and its citizens.
  • 2004 S. Brown White Hot 21
    That gal won't see one red cent of my money. Not unless she does right by you and gives you a divorce.

So you cannot just swap in with for by here. This has a fixed meaning that would be destroyed if you were to do that, and people would not understand you.

It seems to me that I’ve seen this with an indirect object instead of a preposition, but I’ll have to get back to you on that.