In the previous posts, already we have explained the below topics. Refer these links to understand this topic from basics.
What is Docker – Get Started from Basics – Docker Tutorial
What is Container, What is Docker on Container – Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples – Docker Tutorial
Dockerfile Instructions Explained with Examples
How to Write Dockerfile To Build Your Own Docker Images
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Understanding Docker Port Mapping to Bind Container Ports
Let’s get started.
By default, When we create any containers that doesn’t publish or expose the application ports running on the containers. So, we can access these applications only within the docker host not through network systems.
[[email protected] ~]# docker run -d -it –name container1 docker.io/httpd
I have created one container for httpd apache web service with name container1.
This container has got IP Address 172.17.0.2 from bridged network “docker0” and running on port 80. But these httpd service port is not published, so we can access it only using the assigned IP address locally within the docker host.
[[email protected] ~]# curl 172.17.0.2:80
Its working now locally, In order to access these applications or any services through network, We must publish the ports while running the container itself using “p” or “-P” options. This will create a firewall rule to bind the container port to a docker host port.
[[email protected] ~]# docker run -d -it –name container2 -p 8080:80 docker.io/httpd
Now, I have created a container2 with the option “-p 8080:80” to expose the container port 80 with docker host port 8080. So Users must use the docker host IP Address (192.168.2.1) to access the containers through network with published port (8080).
This would reach the docker host first (192.168.2.1:8080) and forward the connection request to the respective containers (8080 -> 80), refer the above diagram for clear idea.
So, We have Container1 and Container2 which are running on same docker host. Each containers have got the local IP Address 172.17.0.2 and 172.17.0.3 respectively.
But, Container1 port is not published, but container2 port is published. So we can access these both applications locally using respective local IP address. Additionally, We can access container2 application externally through network 192.168.2.1:8080, because this port is published.
[[email protected] ~]# curl 192.168.2.1:8080
To know about docker port mappings of the containers, use docker port command as below.
Above output shows that, container1 doesnt have any port mapping rules, but container2 has port mapping rules.
Also, We can use “-P” option for automapping. This option would use any random port number to publish. So we dont need to provide the publish port with container port number.
For example, Let’s create a “container3” with “-P” option and list the docker port mapping of the container.
[[email protected] ~]# docker run -d -it –name container3 -P docker.io/httpd
[[email protected] ~]# docker port container3
80/tcp -> 0.0.0.0:32769
This time, it has published the application port with random port number 32769. So we can access the container3 application through network using 32769 port.
[[email protected] ~]# curl 192.168.2.1:32769
Some of the Useful options are,
-p 8080:80 : Map TCP port 80 in the container to port 8080 on the Docker host.
-p 192.168.1.100:8080:80 : Map 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/udp : Map UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udp : Map 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.
Hope you have got an idea about how to bind container ports using docker port mapping or docker port forwarding. Going forward, we will play more with docker tool.
Keep practicing and have fun. Leave your comments if any.
Support Us: Share with your friends and groups.
Stay connected with us on social networking sites, Thank you.