The story
Attestations exist from the 1820s and 1830s. Earlier ones may be discoverable with better corpus searches. The metaphor is self-evident and is typical of folk wisdom. To choose to pass by a sleeping dog without stirring it is comparable to letting a hornets' nest or beehive be, not poking a bear, and so on.
Etymology from Wiktionary, CC BY-SA 4.0.