Doolally comes from British army slang, originating when Deolali was a British army transit camp in India.
Doolally tap meant being mad, crazy or literally, suffering from camp fever. Tap in English is malarial fever, from the Hindi for fever.
The Madness at Deolali (Journal of the Royal Army Medical Corps) by Major NA Martin begins:
The slang word ‘doolally’ or ‘doolali’ is used
to describe someone who is ‘out of one’s
mind’ or ‘crazy’. It is a derivation of
‘doolali-tap’ and originates from the latter
part of the nineteenth century.The first part
of this phrase is derived from the name of a
small military town in the Indian state of
Maharashtra called Deolali. The second
part is a Hindustani word for fever, often
ascribed to malaria, although in Sanskrit,
‘tapa’ means simply heat or torment. Taken
literally, it is best translated as ‘camp fever’.
By the time of the Second World War, the
term had been shortened to ‘doolally’.
A mixture of boredom, syphilis, venereal diseases, heat, mosquitoes, fever, malaria, sand fleas contributed to misery at the camp.
He concludes:
Frank Richards wrote, `The well known saying among soldiers when speaking of a man who does queer things, "Oh, he's got the Doolally tap, " originated, I think, in the peculiar way men behaved owing to the boredom of that camp'(2). While this may have been true at times, the reason that Deolali became synonymous with mental illness has more to do with the limitations imposed on troop movements by the seasons, the debilitating effect of the summer climate, alcoholism, venereal diseases, malaria, and the difficulties of treating mental illness in the colonies.
The OED's first recorded use is in 1925 but I found some 1916 examples.
A booklet titled The Way They Have in the Army (1916) by Thomas O'Toole has a chapter on on Tommy's Private Language:
Doolally Tap. When a soldier becomes
mentally unbalanced, he is said to have
received the " Doolally Tap." " Doolally "
is a corruption of the name of an Indian
town, Deolali.
The booklet and quoted in Australian newspapers: the Brunswick and Coburg Leader of Friday 12 May 1916 and The Australasian of the next day.