Background: In container terminals, optimizing the scheduling of external trucks and yard cranes is crucial as it directly impacts the truck turnaround time, which is one of the most critical performance measures. Furthermore, proper scheduling of external trucks contributes to reducing CO2 emissions. Methods: This paper proposes a new approach based on a mixed integer programming model to schedule external trucks and yard cranes with the objective of minimizing CO2 emissions and reducing truck turnaround time, the gap between trucking companies’ preferred arrival time and appointed time, and the energy consumption of yard cranes. The proposed approach combines data analysis and operations research techniques. Specifically, it employs a K-means clustering algorithm to reduce the number of necessary truck trips for container handling. Additionally, a two-stage mathematical model is applied. The first stage employs a bi-objective mathematical model to plan the arrival of external trucks at the terminal gates. The second stage involves a mathematical model that schedules yard cranes’ movements between different yard blocks. Results: The results show that implementing this methodology in a hypothetical case study may lead to a substantial daily reduction of approximately 31% in CO2 emissions. Additionally, the results provide valuable insights into the trade-off between satisfying the trucking companies’ preferred arrival time and the total turnaround time. Conclusions: The integration of data clustering with mathematical modeling demonstrates a notable reduction in emissions, underscoring the viability of this strategy in promoting sustainability in port-related activities.