To address the issue of increased transportation costs and capacity resource waste caused by high empty-loading rates of container trucks in port collecting and dispatching, a cooperative allocation optimization method on task-and-path of container truck for heterogeneous carriers based on the task sharing and matching is proposed from the perspective of system optimization. Based on the concept of the sharing economy, the truck allocation problem is extended to a combined optimization and multi-path optimization problem that integrates inbound and outbound transportation task matching and multi-carrier collaborative transportation. Thus, a dual-flexibility sharing and cooperation mechanism consisting of capacity and information is designed, which integrates the capacity resources and transportation tasks of heterogeneous carriers to expand the revenue potential for all participants. Based on task sharing and matching, a cooperative allocation optimization model on task-and-path of container truck for heterogeneous carriers is formulated, to optimize capacity resources and transportation tasks in a comprehensive way and reduce system operating costs. The model implicitly contains the characteristics of element allocation in set, which can be transformed into a constrained set covering problem. A column generation algorithm embedded with a circle-increasing strategy is designed to solve the problem to improve computational efficiency. Through algorithm testing and case analysis, the effectiveness of the model and algorithm is validated. The optimization results show that the profits of participants increase by 24%-65% compared to independent operations. In addition?, the number of used trucks decreases by 28%, and overall carbon emissions are reduced by more than 30%.