<p>I'm new to docker and k8s. Topic with ports is hard to understand for me...
Basically, I know that we assign ports to containers for access to the container.</p>
<p>What is the difference between publishing port: <code>8080:80</code> and <code>127.0.0.1:8080:80</code>?</p>
<p>(<em>Because I'm new in docker and my question might be inexact I'll clarify</em> - I mean while using <code>Docker run</code> command I use <code>-p</code> option to set it).</p>
<p>What does <code>8080</code> and <code>80</code> ports mean? - Can we namely define those ports differently?</p>
<p>How publishing ports relates to k8s defining in pod manifest?
Also if I'd like to assign ports exactly the same in pod manifest like in docker, so how to relate let's say <code>127.0.0.1:8080:80</code> to k8s pod? Are those <code>containerPort</code> and <code>hostPort</code> properties?</p>

<blockquote>
<p>What is the difference between publishing port: 8080:80 and 127.0.0.1:8080:80?</p>
</blockquote>
<p>The difference is very well explained  <a href="https://www.howtogeek.com/225487/what-is-the-difference-between-127.0.0.1-and-0.0.0.0/#:%7E:text=0.0?-,127.0" rel="nofollow noreferrer">here</a>:</p>
<blockquote>
<ul>
<li>127.0.0.1 is the loopback address (also known as localhost).</li>
<li>0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown, or non-applicable target (a ‘no particular address’ place holder).
In the context of a route entry, it usually means the default route.
In the context of servers, 0.0.0.0 means  <em>all IPv4 addresses on the local machine</em>. If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.</li>
</ul>
</blockquote>
<p>If you run a Docker container using this command:</p>
<pre class="lang-sh prettyprint-override"><code>$ docker run -p 8080:80 --name web nginx
</code></pre>
<p>this will <strong>map</strong> a running  <em>container port</em> 80 to <em>host port</em> <code>0.0.0.0:8080</code>:</p>
<pre class="lang-sh prettyprint-override"><code>$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
08a5deaeeae8   nginx     &quot;/docker-entrypoint.…&quot;   26 seconds ago   Up 26 seconds   0.0.0.0:8080-&gt;80/tcp, :::8080-&gt;80/tcp   web
</code></pre>
<p>Then container port 80 will be reachable on the all host's IP addresses on port 8080.</p>
<p>And if you want to map <code>127.0.0.1:8080</code> you should use:</p>
<pre class="lang-sh prettyprint-override"><code>$ docker run -p 127.0.0.1:8080:80 --name web nginx
</code></pre>
<p>Then container port 80 will be reachable only on the host's loopback address.</p>
<p>You can read more information about ports exposing on official Docker documentation page <a href="https://docs.docker.com/config/containers/container-networking/" rel="nofollow noreferrer">here</a>.</p>
<blockquote>
<p>What mean 8080 and 80 ports? - Can we namely define those ports differently?</p>
</blockquote>
<p>You can choose any available port on your host and container. But, please, keep in mind that some apps inside a container are configured to use certain ports.</p>
<h2>k8s</h2>
<p>By default, ports in pod in Kubernetes are not published on nodes and host's IP addresses (pods have their own IP addresses).  It's something like using <em>docker run</em> without <em>-p</em> argument.</p>
<p>And a pod definition doesn't have an option to publish ports on the host IP address, you need to use</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl port-forward pod/mypod 8080:80
</code></pre>
<p>command to do it, which by default  uses 127.0.0.1, but you can specify 0.0.0.0 using <code>--address</code> flag:</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl port-forward --address 0.0.0.0 pod/mypod 8080:80
</code></pre>
<p>You can find additional information about port forwarding on the k8s official page <a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="nofollow noreferrer">here</a>.</p>
<p>And there is a much better option to use in Kubernetes - <strong>Service</strong> -
<strong><em>An abstract way to expose an application running on a set of Pods as a network service.</em></strong></p>
<p>You can check the official Kubernetes documentation about service <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">here</a>.</p>
