Germany declared war on Russia which was allied with France. And Germany did not declare war on any of those countries out of will, but due to an alliance with the Austro-Hungarian empire.
As parent said, Germany didn’t declare war willingly, they had no choice but supporting the alliance. You cannot just change sides in war as it suits you.
For starters, Germany declared war on Russia five days before Austria-Hungary did. But even if it were otherwise, they still had a choice whether to honor their obligations or not.
Furthermore, there's substantial evidence that Germany - or rather, some German factions - was very specifically pressuring Austria into a war, precisely so that it could then be "joined":