If we go far back to Latin, the original meaning of "doctor" is, in fact, an education professional and has absolutely nothing to do with medicine. In classical Latin, "doctor" is a teacher or a trainer.
In Medieval Latin u
In (Middle) English, the earliest recorded use of "doctor" was for church fathers in 1303, but you'd find it widely used for teachers and any knowledgeable person - including medical doctors - as the 13th century goes on.
The codification of the Doctor of Medicine degree and license seems only started in the 18th century[1], but doctors of theology and law far predated that (somewhere in the 12th century I think). Even the PhD (i.e. doctor of philosophy) degree appears to predate the medical doctor degree by half a century.
Originally they were all academic titles: Doctor of Law, Theology, Medicine & Arts.
At some point an expectation developed that practitioners of medicine actually had some formal qualifications and eventually to most people Doctor became synonymous with Physician.