At one of the busiest shipping terminals in the U.S., more than two dozen giant red robots wheeled cargo containers along the docks on a recent morning, handing the boxes off to another set of androids gliding along long rows of stacked containers before smoothly setting the boxes down in precise spots.

The tightly designed dance at TraPac LLC’s Los Angeles terminal offers a window on how global trade will move in the near future: using highly automated systems and machinery, with minimal human intervention, to handle the flood of goods that new free-trade agreements will push to the docks.

Many in the industry believe automation, which boosts terminal productivity and reliability while cutting labor costs, is critical to the ability of ports to cope with the surging trade volumes and the huge megaships that are beginning to arrive in the U.S. Analysts estimate the technology can reduce the amount of time ships spend in port and improve productivity by as much as 30%.

“We have to do it for productivity purposes, to stay relevant and to be able to service these large ships,” said Peter Stone, a member of TraPac’s board.

Yet the TraPac site is one of only four cargo terminals in the U.S. using the technology. That is fewer automated terminals than there are at the Port of Rotterdam in the Netherlands alone.

Logistics Daily Get the latest news and analysis on logistics and supply-chain issues via a daily newsletter, at WSJ.com/Logistics.

Supporters of robotic cargo handling are getting a new showcase this month with the phased-in opening of an automated terminal at the Port of Long Beach, next door to the Los Angeles port. At a cost of over $1 billion to complete and the capacity to handle 3.3 million 20-foot container units—nearly half of the entire port’s volume last year—the Orient Overseas (International) Ltd. site is a big bet on the future.

A successful operation in Long Beach could persuade other U.S. ports to follow, said Mark Sisson, a senior port planner with infrastructure-development group Aecom. “The industry at a global level is rushing hard into this technology,” he said. “That trend is only going to go in one direction. It’s just a question of timing.”

Experts in port-terminal infrastructure and operations say the U.S. has been slow to adopt the technology because of years of resistance by longshore labor unions. Some studies have shown robotic cargo handling can reduce the need for longshore labor by as much as 50%.

In 2002, the issue came to a head as West Coast port employers locked out workers during bitter contract talks, shutting down the Pacific ports for 11 days.

The West Coast’s International Longshore and Warehouse Union has since agreed to allow for automation technology in its contract, which the East Coast’s International Longshoremen’s Association contract also includes. But both labor unions still fight fiercely over the steps along the way to put the technology into use.

The president of the International Longshore and Warehouse Union’s Local 13 in Los Angeles, Bobby Olvera Jr., said the union has been working to obtain “minimum manning standards” and training on automated terminals, to “ensure there’s a future for workers.”

The unions’ efforts, to keep as many longshore jobs as possible on automated operations, can lead to lengthy negotiations over which jobs require humans at the helm. Adding jobs raises the final operating costs, making it tougher to get a return on the hundreds of millions of dollars typically required for automated machinery and technology.

Automated Cargo Handling Glides Into Ports Container terminals seek lower labor costs, productivity improvements of up to 30% Cargo containers are carried around the TraPac marine terminal at the Port of Los Angeles by automated machines guided by a magnetic grid embedded in the pavement. Patrick T. Fallon for The Wall Street Journal 1 of 9 • • • • • 1 of 9 Show Caption Cargo containers are carried around the TraPac marine terminal at the Port of Los Angeles by automated machines guided by a magnetic grid embedded in the pavement. Patrick T. Fallon for The Wall Street Journal

In the U.S., “You may not be able to achieve the cost savings as immediately as you do in other countries,” said John Martin of maritime consulting firm Martin Associates in Lancaster, Pa. “Hence, the decision to automate is much more stressful from the investors’ standpoint.”

Ports elsewhere have seen the investment pay off. APM Terminals, part of the A.P. Moeller-Maersk A/S group, said its automated terminal in Rotterdam uses about half the labor needed at its conventional terminal at the same port.

In the U.S., the history of automation is choppy. APM Terminals developed the first semi-automated terminal in North America at a cost of $450 million in Portsmouth, Va., and opened it in 2007. After poor returns following the 2008-2009 recession, APM leased the facility back to the port authority and eventually sold it in 2014 to a private infrastructure-investment group.

Related Logistics Accelerator Kicks Off

The TraPac terminal in Los Angeles faced long delays in environmental permitting, as well as a ballooning budget. TraPac ran into labor-related setbacks in 2014 when ILWU members walked off the job for more than a month after several machinery collisions occurred in the automated area of the terminal.

Overall, TraPac’s automation will cost roughly $1 billion in public and private funds once the entire terminal is automated, and executives say they aren’t sure when the investment will pay off. “It’s very much a moving goal post,” said board member Mr. Stone. “It takes a long time to realize the return.”

Still, some workers find benefits as the technology takes hold. On a recent afternoon, 57-year-old crane operator Jesse Martinez lowered shipping containers the last few feet of their journey on to truck trailers, using a computer from an air-conditioned office building at TraPac.

It was far different from his old work sitting in the crane for hours at a time, navigating the machinery with heavy gears. “The bouncing around and leaning over is the part I don’t miss,” he said.

Write to Erica E. Phillips at erica.phillips@wsj.com