The port has an advantage because it's in a fixed location with favorable features like a good harbor and nearby rail lines. The union has an advantage because the port needs workers who live close. It's perfectly natural to form these types of agreements - the port is a business trying to captures the market for people shipping goods to the area, and the union is a business trying to provide the labor required to do so.
When you say that others should be able to work and compete with unions without joining them, that's a moral position, not a legal position. (And, to be clear, it's not a bad position at all!) The legal position is that the contract will be exclusive, and both parties are agreeing to it.
Labor unions exist because people don't get what they deserve. They get what they negotiate.