The phrase originally referred to actual cannons on a ship that could cause significant damage if they weren’t tied down during a storm. One of the first instances of this expression in print is in a novel by Victor Hugo and it refers to literal cannons.
It is unclear how the expression came to mean what it does today, but there is written evidence of the expression in its current context as far back as the late 1800s. Of course, the expression no longer refers to literal cannons because they are no longer used. Here is an example of how this phrase is currently used:
I can’t believe John got into another fight at school. That kid is a loose cannon.
Here is another example of how this expression is used:
This article talks about the different things that John Bolton, a government worker during the Bush administration, did and calls him a loose cannon.