This question already has an answer here:
I use an external monitor with my Macbook Pro, often to run Windows VMs with a task bar at the bottom. While trying to use the task bar, I often accidentally summon the dock to the second monitor. How do I stop the dock from moving?
Note: I'm aware of the option to go back to the old one-space-for-all-displays behaviour. I don't want to go back to the old behaviour, I want to fix the new one.
Note, repeated: This isn't a duplicate of the question which asks why this happens. I know why it happens, but there's nothing there that would help me to prevent it from happening.