<p>When I define e.g. a deployment in Kubernetes there is a section with a list of containers and each of them contains an array of ports, e.g.:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
</code></pre>
<p>Now the documentation <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#container-v1-core" rel="noreferrer">here</a> explicitly says it does not affect connectivity:</p>
<blockquote>
<p>List of ports to expose from the container. Exposing a port here gives
the system additional information about the network connections a
container uses, but is primarily informational. Not specifying a port
here DOES NOT prevent that port from being exposed. Any port which is
listening on the default &quot;0.0.0.0&quot; address inside a container will be
accessible from the network. Cannot be updated.</p>
</blockquote>
<p>Now it seems it does not really affect anything and is only informational, but what does that really mean, where is that used?</p>
<p>I have found one use of that is that if port defines a name, it can be referenced from a service by that name.</p>
<p>Is that it or there are some other uses for this specification?</p>

<p>As you quote the documentation, </p>

<blockquote>
  <p>List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.</p>
</blockquote>

<p>the purpose of defining the <code>containerPorts</code> is purely for documentation. It is only used by other developers to understand the port that the container listens to. Kubernetes borrows this idea from docker which does the same with <code>EXPOSE</code> command as mentioned <a href="https://docs.docker.com/engine/reference/builder/#expose" rel="noreferrer">here</a>.</p>
