Rains was born in Craven County in North Carolina in 1817. He graduated from the United States Military Academy at West Point in 1842 as third out of a class of 56; being commissioned a 2nd Lieutenant in the Corps of Engineers. In the next year he transferred to the 4th U.S. Artillery Regiment and then became an assistant professor of chemistry, mineralogy, and geology back at West Point. He participated in the Mexican–American War, fighting at Vera Cruz, Cerro Gordo, Contreras, Churubusco, Molino del Rey and Chapultepec. For his services he was promoted to 1st Lieutenant and brevetted Captain. Over the next years he frequently changed stations and often served on recruiting duty. The full Captain's promotion came in February 1856 when he was stationed in New York but he resigned his commission half a year later.