> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Port Mapping

> Explains Docker port mapping and publishing host ports to expose container services to external clients, with examples, behaviors, and best practices.

In this lesson you’ll learn how Docker port mapping (also called port publishing) allows external clients to reach services running inside containers. This is essential for exposing web apps, databases, and other services from a Docker host to browsers, API clients, or other machines.

## How port mapping works

A containerized application listens on a port inside the container (the container port). To make that application reachable from outside the Docker host, Docker can publish a container port on a host port. When a host port is published, traffic arriving at the Docker host IP and that host port is forwarded into the container port.

Two IPs to understand:

* Container IP: Docker assigns an internal IP to each container. This IP is only reachable from the Docker host and other containers on the same Docker network. If you open a browser on the Docker host itself, you can use the container IP and container port to access the app.
* Docker host IP: When you publish a host port, clients outside the Docker host connect to the Docker host IP and the published host port; Docker forwards that traffic into the container.

## Example: publish a web app on port 80

Suppose the app inside the container listens on container port 5000. To make it available on port 80 of the Docker host, run:

```bash theme={null}
docker run -p 80:5000 kodekloud/webapp
```

Now users can browse to http\://\<docker\_host\_ip>/ and reach the app. Docker forwards host port 80 into container port 5000.

## Run multiple instances or different services

You can run multiple instances of the same image and publish each one to a different host port:

```bash theme={null}
docker run -p 8000:5000 kodekloud/webapp
docker run -p 8001:5000 kodekloud/webapp
```

Or publish a database container’s default port directly:

```bash theme={null}
docker run -p 3306:3306 mysql
```

## Examples at a glance

| Published host port | Container port | Command example | Reachable from |
| - | - | - | - |
| 80 | 5000 | `docker run -p 80:5000 kodekloud/webapp` | Any external client via `http://<docker_host_ip>:80` |
| 8000 | 5000 | `docker run -p 8000:5000 kodekloud/webapp` | External client via `http://<docker_host_ip>:8000` |
| 3306 | 3306 | `docker run -p 3306:3306 mysql` | External clients connecting to MySQL on `tcp://<docker_host_ip>:3306` |

## Important behavior and best practices

<Callout icon="lightbulb" color="#1CB2FE">
  By default, published host ports are bound to `0.0.0.0` on the Docker host, making them reachable on any network interface of the host. Use `-p <host_ip>:<host_port>:<container_port>` if you need to bind to a specific host IP.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  You cannot publish the same host port to multiple containers at the same time. Host ports must be unique. If a port is already in use on the host, Docker will fail to publish that port for a new container.
</Callout>

## Quick reference

* Syntax: `-p <host_port>:<container_port>`
* To bind to a specific host IP: `-p <host_ip>:<host_port>:<container_port>`
* Container IPs are internal; publishing on the host lets external clients reach the container.
* Multiple container ports can be published to distinct host ports, but a single host port cannot be reused.

## Links and references

* [Docker run reference — ports](https://docs.docker.com/engine/reference/run/#expose-incoming-ports)
* [Docker networking overview](https://docs.docker.com/network/)
* [Best practices for exposing services](https://docs.docker.com/engine/security/security/)

This article covered the essentials of Docker port mapping to help you expose containerized services safely and predictably.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/docker-training-course-for-the-absolute-beginner/module/7a88081c-a9d0-45cc-b75e-55c93b6886d6/lesson/92bed9c7-27f6-4d13-acbd-daed80c5414b" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.