Skip to main content
Port mapping (also called port publishing) exposes a container’s internal port on the Docker host so clients outside the host can reach the service. This article revisits running a simple web app inside a Docker container and explains how to publish container ports to the host. When a containerized web server starts, the server listens on a container-internal port (for example, port 5000). To reach that app from outside the Docker host you must map a host port to the container port. Two IP addresses to consider:
  • The container IP: Every container receives an internal IP assigned by Docker (for example, 172.17.0.2). This IP is reachable only from the Docker host or other containers on the same Docker network. If you open a browser on the Docker host, you can use http://<container-ip>:5000 to reach the app.
  • The Docker host IP: To make the app accessible from other machines, publish a container port to a host port. That exposes the application on the host IP and chosen host port (for example, http://<docker-host-ip>:80).
To publish a container port, use the -p option with the format -p <hostPort>:<containerPort>:
This command maps host port 80 to container port 5000. Requests sent to http://<docker-host-ip>:80 will be forwarded to port 5000 inside the container. Common scenarios and examples:
Note: You cannot bind the same host port more than once. Attempting to map a host port already in use will cause Docker to fail to start the new container. Example error:
Table: Quick reference for -p usage
If a host port is already taken by another container or process, Docker will not start the new container. Choose a free host port or stop the conflicting service before mapping.
Port mapping gives you control over which container ports are exposed externally and how external traffic reaches your containerized services. For more advanced networking (bridge networks, host networking, overlay networks, and published ports with specific IP bindings), see the Docker networking documentation.
Remember: container IPs are internal to Docker networks. To make a container accessible externally, publish a container port to a free host port using -p hostPort:containerPort.
Links and references

Watch Video