How to map docker container port to the host port

By default, when the container is created, it doesn’t publish any port to Internet to within the network connected to the host. In order to acheive it, use --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host. Here are some examples.

Flag valueDescription
-p 8080:80Map TCP port 80 in the container to port 8080 on the Docker host.
-p 192.168.1.100:8080:80Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.
-p 8080:80/udpMap UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udpMap TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

There is the way from Docker, where we use “-P” (upper case “P”)to auto mapping to host from container. In that case, why can’t have option for auto EXPOSE.

And there is option to expose port while starting the container. also…

docker run -i --expose=22 <container ID e.g. b5593e60c33b> bash