Key purpose of container terminals is to serve container vessels. Container vessels may be of different types such as large deep-sea vessels or feeders and barges. Container terminal operators have to deploy intelligent strategies for the allocation of their limited resources to the calling vessels of those different types. The presence of uncertainty in the real processing of the operational schedules and arrival of vessels adds to the complexity of the already multifaceted problem. An inefficient decision in the berth allocation phase affects all the other applications connected to this and may increase the service period and costs. In this study, we propose a framework based on stochastic dynamic programing approach to model the berth allocation problem and characterize optimal polices under stochastic arrival and handling times for different types of calling vessels. We find that the optimal control policy is of threshold type depending on the number of vessels in a certain berth group. The derived policies can be used at container terminals for the optimal use of their berthing facilities.