Why do static shocks happen?
The short answer
Rubbing can create a charge imbalance. When the voltage difference between your body and metal becomes large, charge suddenly moves in a discharge.
How it works
Dry air lets charge remain on surfaces, and clothing and shoes affect how it builds up. Even a small spark requires care near flammable gas.
A familiar example
Touching a wall first can let some charge leave gradually and reduce the shock at a doorknob.