The Federal Maritime Commission, in a major setback for dockworkers, ruled yesterday that trucking companies, warehouses and shippers can use nonunion labor to pack and unpack shipping containers at Atlantic and Gulf coast ports.

The five-member commission ruled unanimously that labor contracts between the International Longshoremen's Association and ocean carriers restricting that work within 50 miles of a port to union stevedores was discriminatory under the 1916 Shipping Act.

In a 180-page decision climaxing a seven-year legal battle by trucking and warehouse operators, the commission gave ocean carriers 90 days to remove the restrictions from their tariffs with shippers.

"This is going to encourage more small companies to get into the export business," said Thomas Donohue, president of the American Trucking Associations. "You reduce the extra handling of a product and you reduce its costs."

Jim McNamara, a spokesman for the ILA, said the union's officers were scheduled to meet Wednesday on the union's next move.

The National Labor Relations Board had ruled in 1982 and 1983 that some of the restrictive provisions in the contracts between the ILA and ocean carriers were illegal under labor laws.

However, the Supreme Court reversed those decisions in 1985, ruling that the provisions were a lawful work-preservation labor practice.

The ILA first won the restrictions in 1959 when automation and container ships began replacing freighters and reducing the need for dockworkers.

Unlike bulk cargo, a container can be lifted from a ship onto the bed of a railroad car or truck trailer with cranes and vice versa without having to be unpacked and repacked.

Under the labor contracts between the ILA and the ocean carriers, no container could be packed or unpacked within 50 miles of a port unless the work was done by members of the union.

Despite the restrictions on nonunion labor, containerization still has greatly reduced the need for manual labor on the nation's docks, with ILU membership falling from 165,000 at its peak to less than 60,000 today.

Commission officials said the ruling yesterday addressed only Eastern and Gulf ports. Dockworkers on the West Coast are represented by a different labor group, the International Longshoremen's and Warehousemen's Union, which has about 50,000 U.S. members