When the hideous sport of cockfighting was legal, the birds were taken to a pit in the ground where they fought to the death.
These fights were quick and bloody, and for this reason, the “cockpit” became the designated name of the room on a warship were surgeons attended the wounded and dying.
During the First World War, pilots, like the roosters, were inserted into a confined space to do battle, and so they named that space the cockpit.