The soldiers were searching for weapons near the Afghan city of Jalalabad when five gunmen fired on them with AK-47 machine guns.
The soldiers returned fire, but it is unknown whether any of the assailants were hit.
The soldiers searched one of three buildings in the compound and found equipment used to produce illicit narcotics.
Documents seized showed instructions on how to process and package the drugs. They also showed the lab was tied to Hezb-e-Islami, a group affiliated with the warlord Gulbuddin Hekmatyar, but would not give details.
Hekmatyar's Hezb-e-Islami force was one of the US-aided guerrilla armies that fought Soviet invaders in the 1980s. He served as Afghan prime minister then fled to Iran in 1996 after his group was defeated by the Taliban.
Recently, his forces have forged an alliance with Taliban fugitives and claim to be receiving arms and money from al-Qaida and Iran for planned suicide attacks on US forces in Afghanistan and destabilising the government of president Hamid Karzai. Hekmatyar has reportedly called for a holy war against the United States.