Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on swebtools.com

Search Suggest

Understanding Docker Port Mapping to Bind Container Ports

docker port mapping, docker port mapping examples, docker port mapping explained, docker networking port explained, docker networking, docker basics
This tutorial post will help you to understand about how docker port mapping or docker port forwarding that helps to bind the container ports with docker host in order to access the applications running on the containers through network.

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.

Understanding Docker Port Mapping to Bind Container Ports

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.

[root@docker-host ~]# docker run -d -it --name container1 docker.io/httpd
ec649e19885f2006cfe1d199d6c9f844b4fc1024dbe54c2d2dcd5dcaedbefc0f

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.

[root@docker-host ~]# curl 172.17.0.2:80
<html><body><h1>It works!</h1></body></html>

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.

[root@docker-host ~]# docker run -d -it --name container2 -p 8080:80 docker.io/httpd
7795205e821829732a780bf09b7ac03fb0cfaf9ab342ba68b4e3b752431e9303

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.

[root@docker-host ~]# curl 192.168.2.1:8080
<html><body><h1>It works!</h1></body></html>

To know about docker port mappings of the containers, use docker port command as below.

[root@docker-host ~]# docker port container1
[root@docker-host ~]# docker port container2
80/tcp -> 0.0.0.0:8080

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.

[root@docker-host ~]# docker run -d -it --name container3 -P docker.io/httpd
2634717516b75111a0b9f2336faa7a49cc2f13146c65a94a6e82442b6a2fad7c
[root@docker-host ~]# 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.

[root@docker-host ~]# curl 192.168.2.1:32769
<html><body><h1>It works!</h1></body></html>

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.

If you are interested in learning, Request you to go through the below recommended tutorial.

DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online


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.