In this article, I will take you through the steps to manage ports in Docker.
By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host.
Manage Ports in Docker
What is Port?
In computer networking, a port is a communication endpoint. Physical as well as wireless connections are terminated at ports of hardware devices. At the software level, within an operating system, a port is a logical construct that identifies a specific process or a type of network service.
How Port binding works?
Docker containers can connect to the outside world without further configuration, but the outside world cannot connect to Docker containers by default.A bridge network is created (with the name bridge) when you install Docker. Every outgoing connection appears to originate from the host’s IP space; Docker creates a custom iptables masquerading rule.
How Custom IP and Port forwarding works?
By default, Docker exposes container ports to the IP address 0.0.0.0 (this matches any IP on the system). If you prefer, you can tell Docker which IP to bind on. To bind on IP address 10.0.0.3, host port 80, and container port 8080:
docker run -p 10.0.0.3:80:8080 nginx
How to check port mapping in Docker
docker port command to inspect the mapping Docker creates.
[root@localhost ~]# docker port f3f6a5f08b57 443/tcp -> 0.0.0.0:32772 80/tcp -> 0.0.0.0:32773
Also Read: How to Install Docker In RedHat/CentOS 7
Reference: Docker Documentation