What you have is called “water hammer” It’s usually caused by fast closing valves “solenoid” vales found in appliances like dishwashers, and washing machines. Or, on positive shut off faucets, such as are found on newer type faucets that are washerless.
I can think of two possible solutions:
1) When you hear the “knocking” sound, go into your basement, and look around at the water pipe that is hung from your floor joists sometimes, the force of a valve closing will cause the pipe to bang against the bottom of the joist. Wrap the pipe with some pipe insulation at the point where it’s banging against the joist, to give it some cushion.
2) Water hammer can also be caused by air trapped in the pipe. One solution could be to shut your water off at the meter, or well, and drain the system. This will recharge the air chambers in the water pipe system with air. (If your plumbing contracter supplied air chambers in the plumbing in the walls) Good luck