I think it could be a turbulence inside the pipes that resonates with the pipes and causes the knocking. You could try to fix the pipe to some wall.
Home Improvement
Home Improvement
Sounds like “water hammer”. If you google that others can give a way better explanation of why it happens.
I fixed mine by closing the main at the street, and opening the faucet highest and furthest from the main. In my house that was an upstairs faucet at the opposite corner from the main. Released all the pressure and then turned back on the main. Noise left and hasn’t come back for 6~ months. There are knock arresters you can install or have installed that stop the problem before it happens.
Might be caused by sediment in your hot water heater (assuming you are using a tank water heater). If you're not familiar, there are tons of articles out there on how to clean it out.
A secondary concern is that you seem to have hot water moving through the system when you don't expect it. Do you have any water circulators on the house on faucets (so you get instant hot water at faucets?). That could explain it. Otherwise, you might have a leak somewhere.