For five weeks, almost everything seemed to go wrong for one commercial vessel waiting in the Danube River to load Ukrainian grain bound for Spain via the Black Sea.

First, Russian drones exploded mere miles away from where the vessel was anchored. Then, heavy congestion on the river led to weeks of delays, costing the vessel’s operator $8,000 a day in extra running costs. Finally, around midnight after its cargo of over 12,000 metric tons of grain had finally been loaded, Russian drones hit grain warehouses in an hourlong raid at the port the vessel had just left.

“My crew, they were scared,” said Captain Alan, who provided only his last name out of concern for his safety. “Some small mistake, they can hit us,” he said of the bombs.

For months, ships traversed the Black Sea and the Danube River without incident to load Ukrainian grain and deliver it around the world, even as Russia’s invasion of Ukraine intensified. Then in mid-July, when Russia withdrew from an agreement that allowed passage of those cargoes, everything changed.

The perilous journey of Captain Alan’s 560-foot dry-bulk carrier shows how a once low-risk shipping route now raises difficult calculations for longtime buyers of Ukrainian grain, prompting them to question how long they can keep making the trip.

“Everyone is praying to get out of there as quickly as possible,” said Firat Adriansen, whose company operates the Turkish-flagged vessel. “Another day, another risk.”

The vessel, which Mr. Adriansen asked not to be named out of concern for its safety, is now on its way to drop off the grain in Spain. He said he was still unsure how much profit he would make on this journey, if any.

Since Russia pulled out of the United Nations-backed agreement that had guaranteed the safe passage of grain shipments from Ukraine’s Black Sea ports, commercial vessels have increasingly relied on Ukraine’s much smaller ports on the Danube. They are thought to be a safer alternative because of their proximity to Romania, a NATO member.

In July, more than two million metric tons of Ukrainian agricultural products were shipped through these ports, up from a monthly average of 1.8 million during the first half of this year, said Alexis Ellender, a global analyst at Kpler, a commodities analytics firm.

At a meeting of European and American officials in the Romanian port of Galati this month, James C. O’Brien, the Biden administration’s sanctions coordinator, said the United States was committed to helping Ukraine expand the capacity of its Danube ports.

But in recent weeks, Russia’s relentless attacks on these ports — including one on Wednesday — have caused significant damage.

Beyond the threat of Russian drones, the port infrastructure on the Danube is aging, Mr. Ellender said. There is a shortage of pilots, critical for navigating ships through the river. There is significant risk of getting these massive vessels, more suited for larger ports, stuck or damaged in one of the narrow waterways, which would block traffic. A drought, like the one that hit Europe last summer, could cause rivers to become so low that ships have to reduce cargo volumes to allow passage.

And then, just beyond the Danube’s delta, there are also sea mines. Just as Captain Alan’s vessel was ready to leave the port of Reni, in Ukraine, and head to Turkey on a course that would hug Romania’s coast to reduce the chance of a run-in with a Russian warship, sea mines exploded in Romanian waters.

It forced him to map out a new route. “We needed to choose between Russian warships and sea mines,” he said.

When he and his crew of 20 finally reached the Bosporus in Turkey, it was a huge relief, he said. “I said, yes, it’s the end now.”

Given these risks and the threat of a Russian attack, shipowners may choose to buy grain and other agricultural products from countries like Brazil, Canada or Australia instead of Ukraine. That shift would undercut a cornerstone of Ukraine’s economy and deprive world markets of one of its largest agricultural producers, pushing prices higher.

“There is an increased hesitation to put both assets and people in risk,” said Peter Jameson, a managing director and partner at Boston Consulting Group in Copenhagen and a former officer in the British Royal Navy. “There’s a paralysis, particularly in the last couple of weeks, as we’ve seen physical attacks on ships.”

Increased insurance premiums have also added to the hesitation, he said.

Congestion in Danube ports has been a persistent issue, with many of the more than 100 vessels anchored outside Sulina, a Romanian port, waiting to enter Ukraine’s Danube ports, according to MarineTraffic data and a Ukrainian shipping agency.

Katerina Kononenko, an operations managers at Avolon Shipping based in Odesa, Ukraine, said vessels should be safe as long as they were in Romanian waters. “Drone attacks are aimed at destroying the infrastructure of ports and terminals, not at destroying foreign-flagged vessels,” she said.

Still, she recited the rising pressures on commercial shipping in the region: Insurers were calculating the changing risks to justify higher coverage costs, shipowners were raising freight rates to cover their added expenses, and buyers were pressuring producers to reduce grain prices. At the bottom of this chain, she said, Ukrainian farmers “are in the worst situation.”

Mr. Adriansen said that the next stop for the vessel would most likely be Brazil to pick up sugar, but that he would consider another trip to Ukraine depending on the circumstances.

Captain Alan, however, said he would not risk the same route again. “My company is not thinking about going back until the situation is settled,” he said.