What is a Port Mapping (Forwarding) in Docker

In Docker, port mapping (also known as port forwarding) is the process of associating a port on the host system with a port on a Docker container, allowing network traffic to be directed to and from the container through the host system’s network interface.

Docker containers run in isolated environments with their own network stack, including their own IP address and network ports. By default, containers can communicate with each other and with the host system, but they are isolated from the external network. Port mapping allows you to expose specific container ports to the host system, making them accessible from the external network or other hosts on the host system’s network.

Port mapping is typically used to publish containerized applications or services to the outside world, allowing them to be accessed via standard network protocols such as HTTP, HTTPS, SSH, etc. It provides a way to expose container ports and make them accessible from the host system or external network, while maintaining the isolation of the container’s network stack.

Port mapping is done using the -p or --publish option when running Docker containers. The syntax is as follows:

docker run -p [host_port]:[container_port] my_container

For example, to map port 8080 on the host system to port 80 on a container, you would use the following command:

docker run -p 8080:80 my_container

This maps port 8080 on the host system to port 80 on the container, allowing incoming network traffic on port 8080 of the host system to be directed to port 80 of the container.

Port mapping provides flexibility in exposing containerized services to the external network, allowing you to define how the container’s ports are accessed and making Docker containers accessible over the network, just like regular services running on the host system.

Here’s another example of port mapping in Docker:

Let’s say you have a containerized database server running a MySQL container, and you want to expose it on a specific port on the host system so that other applications or clients can connect to it. The MySQL container listens on port 3306 by default, but you want to expose it on port 3308 on the host system.

You can use the -p or --publish option to map the desired host port to the container port when starting the MySQL container. For example:

docker run -p 3308:3306 -d --name my_mysql_container mysql

This command maps port 3308 on the host system to port 3306 on the MySQL container, and starts the container in detached mode (-d) with the name “my_mysql_container”. Now, the MySQL container is accessible on port 3308 of the host system.

Other applications or clients can connect to the MySQL container using the host system’s IP address and port 3308, as if the MySQL server were running directly on the host system. This allows you to expose the containerized MySQL service on a specific port on the host system, making it accessible to external applications or clients for database connectivity.

Port mapping is a powerful feature in Docker that enables you to define how containerized services are exposed and accessed over the network, providing flexibility and control over network communication between containers and the host system or external network.


So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.