<p>I have an ASP.NET Core Multi-Container docker app which I am now trying to host to Kubernetes cluster on my local PC. But unfortunately one container is starting and other is giving error <strong>address already in use</strong>.</p>
<p>The Deployment file is given below:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: multi-container-dep
  labels:
    app: aspnet-core-multi-container-app
spec:
  replicas: 1
  selector: 
    matchLabels:
      component: multi-container
  template:
    metadata: 
      labels:
        component: multi-container
    spec:
      containers:
        - name: cmultiapp
          image: multiapp
          imagePullPolicy: Never
          ports:
            - containerPort: 80
        - name: cmultiapi
          image: multiapi
          imagePullPolicy: Never
          ports:
            - containerPort: 81
</code></pre>
<p>The full logs of the container which is failing is:</p>
<pre><code>Unable to start Kestrel.
  System.IO.IOException: Failed to bind to address http://[::]:80: address already in use.
   ---&gt; Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
   ---&gt; System.Net.Sockets.SocketException (98): Address already in use
     at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
     at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Bind(EndPoint localEP)
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.&lt;Bind&gt;g__BindSocket|13_0(&lt;&gt;c__DisplayClass13_0&amp; )
     --- End of inner exception stack trace ---
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.&lt;Bind&gt;g__BindSocket|13_0(&lt;&gt;c__DisplayClass13_0&amp; )
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
     at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig)
</code></pre>
<p>Note that I already tried putting another port to that container in the YAML file</p>
<pre><code>ports:
    - containerPort: 81
</code></pre>
<p>But it seems to not working. How to fix it?</p>

yogihosting
<p>To quote this answer: <a href="https://stackoverflow.com/a/62057548/12201084">https://stackoverflow.com/a/62057548/12201084</a></p>
<blockquote>
<p>containerPort as part of the pod definition is only informational purposes.</p>
</blockquote>
<p>This means that setting containerPort does not have any influence on what port application opens. You can even skip it and don't set it at all.</p>
<p>If you want your application to open a specific port you need to tell it to the applciation. It's usually done with flags, envs or configfiles. Setting a port in pod/container yaml definition won't change a thing.</p>
<p>You have to remember that <a href="https://kubernetes.io/docs/concepts/cluster-administration/networking/" rel="nofollow noreferrer">k8s network model</a> is different than docker and docker compose's model.</p>
<hr />
<p>So why does the containerPort field exist if is doesn't do a thing? - you may ask</p>
<p>Well. Actually is not completely true. It's main puspose is indeed for informational/documenting purposes but it may also be used with services. You can name a port in pod definition and then use this name to reference the port in service definition yaml (this only applies to targetPort field).</p>
