How to define publish parts of ports and what are substitutes in docker and k8s?

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.

What is the difference between publishing port: 8080:80 and 127.0.0.1:8080:80?

(Because I'm new in docker and my question might be inexact I'll clarify - I mean while using Docker run command I use -p option to set it).

What does 8080 and 80 ports mean? - Can we namely define those ports differently?

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 127.0.0.1:8080:80 to k8s pod? Are those containerPort and hostPort properties?


What is the difference between publishing port: 8080:80 and 127.0.0.1:8080:80?

The difference is very well explained here:

  • 127.0.0.1 is the loopback address (also known as localhost).
  • 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 all IPv4 addresses on the local machine. 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.

If you run a Docker container using this command:

$ docker run -p 8080:80 --name web nginx

this will map a running container port 80 to host port 0.0.0.0:8080:

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
08a5deaeeae8   nginx     "/docker-entrypoint.…"   26 seconds ago   Up 26 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   web

Then container port 80 will be reachable on the all host's IP addresses on port 8080.

And if you want to map 127.0.0.1:8080 you should use:

$ docker run -p 127.0.0.1:8080:80 --name web nginx

Then container port 80 will be reachable only on the host's loopback address.

You can read more information about ports exposing on official Docker documentation page here.

What mean 8080 and 80 ports? - Can we namely define those ports differently?

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.

k8s

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 docker run without -p argument.

And a pod definition doesn't have an option to publish ports on the host IP address, you need to use

$ kubectl port-forward pod/mypod 8080:80

command to do it, which by default uses 127.0.0.1, but you can specify 0.0.0.0 using --address flag:

$ kubectl port-forward --address 0.0.0.0 pod/mypod 8080:80

You can find additional information about port forwarding on the k8s official page here.

And there is a much better option to use in Kubernetes - Service - An abstract way to expose an application running on a set of Pods as a network service.

You can check the official Kubernetes documentation about service here.