The shipping industry, which has ailed for years, is finally showing signs of recovery.

The sector has struggled with overcapacity, price wars and freight rates far below break-even levels. Now, thanks to the improving global economy, industry executives and analysts say the worst may be over, at least for container and dry-bulk ship operators. Early hints of a recovery in the oil-tanker business are starting to emerge.

“Shipping is more than alive and kicking. There is a spring in the air and there is a spring in the world economy,” said Singapore Minister of Transport Khaw Boon Wan.

Container shipping, which moves 95% of all manufactured goods, was rattled last year by the bankruptcy of South Korea’s Hanjin Shipping Co., which stranded billions of dollars worth of cargo at sea. The top 20 operators by capacity posted combined net losses in 2016 of $5 billion. Since then, the big players have merged or formed alliances and most are expected to swing to a profit this year.

Among other signs of life: The cost to transport a container in the benchmark Asia-to-Europe route rose to $965 in May, up 55% from a year earlier. At the port of Singapore, which the industry uses to gauge trade flows, container volume rose 5% in the first quarter from a year earlier. And demand for container capacity cut the percentage of idle ships to 3.5% in the first three months of 2017, compared with 6.5% in the previous quarter.

Operators of the biggest dry-bulk cargo ships known as capesize vessels have been hit hard over the past few years by anemic demand in China for the coal, iron ore and other commodities those ships transport. Dozens have suspended operations or gone through painful restructurings. The Baltic Dry Index, which tracks the cost of moving such products, fell to a record low of 290 points in February 2016 from its peak of 11793 in May 2008. It now hovers around 850 points.

Industry experts expect the market to turn around this year. Demand is forecast to grow about 3%, compared with a 1% growth in capacity. This has been spurred by Chinese iron-ore imports, which rose 11% year-over-year in March to 95.5 million metric tons, the second-largest monthly amount on record. Imports of the thermal coal used in power plants also are up sharply, and so far this year, capesize daily rates have averaged around $11,000, triple the rate in early 2016.

Tankers, which move the world’s crude oil, got off to a rough start this year with too many vessels chasing too little cargo, while inland storage facilities were full. In late May daily charter rates for very large crude carriers, known as VLCCs, moving crude from the Middle East stood at an average $17,000, below the break-even rate of about $22,000.

Used tankers have been decreasing in value over the past 12 months, with the price of a five-year VLCC falling to $58 million in May from $68 million a year earlier.

However, a combination of low tanker prices and old ones being retired to scrapyards has prompted a buying frenzy, with 69 new and used VLCCs bought in the first five months of this year, compared with only eight last year, a sign that prospective owners expect a recovery.

Write to Costas Paris at costas.paris@wsj.com