<p>As we expose the container port in dockerfile itself then what is the use of container port in kubernetes yaml. What does it actually do. Is it mandatory to mention the container port in yaml file or we need not to mention in when we expose it in docker file.
Anyways, we will be using target port the map the container port with pod</p>

<p>ports:
        - containerPort: 80</p>

<p><strong>ports :</strong> </p>

<blockquote>
  <p>containerPortList 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 <code>"0.0.0.0"</code> address inside
  a container will be accessible from the <strong>network</strong>. Cannot be
  updated.</p>
</blockquote>

<p><a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#container-v1-core" rel="nofollow noreferrer">container-core</a></p>

<p>So it is exactly same with docker EXPOSE instruction. Both are informational. If you don’t configure ports in Kubernetes deployment, you can still access to the ports using Pod IP inside the cluster. You can create a service to access the ports externally without configuring ports in the deployment. But it is good to configure. It will help you or others to understand the deployment configuration better.</p>

<blockquote>
  <p>The EXPOSE instruction does not actually publish the port. It
  functions as a type of documentation between the person who builds the
  image and the person who runs the container, about which ports are
  intended to be published.</p>
</blockquote>

<p><a href="https://docs.docker.com/engine/reference/builder/" rel="nofollow noreferrer">.docker-reference-builder</a></p>
