Your driver's door hinge is worn, which has caused the pin switch to be maladjusted. The missing plastic sleeve for the door strike bolt allows the door to bounce & rattle, which is why the condition is intermittent.
There are hinge repair kits available at most parts stores, and many people on
FSB like them, but I prefer to replace the whole hinge. It's more work, but I like stock parts.
Aligning the door isn't easy, but I think it's worth it because it really "restores" the truck more than you might think.
Readjusting the pin switch is easy: just open the door, carefully grab the end of the pin with some pliers, & pull it straight out about 1/4". The next time you close the door, it'll reset itself.
These pics show how to reset them in a vise, but it's not necessary to remove them from the truck.