One of my trains with 5 cars was getting stuck in between stations stating it was 'Docked' in the middle of nowhere. What was actually happening was the following:
The previous station had a slope on the tracks before station 4, this was causing the weight of the 5th car to push the train forward before it had completed the full docking procedure. So it would bug out before reaching the next station.
To resolve this, I removed the slope by adding 2 foundation blocks of straight even length. Train now docks fine and doesn't get stuck anymore.
TLDR; Make sure the train is fully stopped and not moving during docking, due to track slope