A web server can be running inside a container while your browser still needs a way to reach it. A port is a number that helps a machine direct incoming network requests to the right service. Docker can publish a host port to pass those requests to a port inside the container.
Docker · Lesson 5
Port Mapping
Learn how to publish a container port and find the address a local browser should use.
Reading a Port Mapping
The Nginx server in this example accepts web requests on port 80 inside the container. The web container from the earlier example was created without a published port. Here, give a new container its own name and use -p to set the mapping when you create it.
$ docker run -d --name published-web -p 127.0.0.1:8080:80 nginx:alpine
Read 8080:80 as from host port 8080, where the browser connects, to container port 80, where the server receives the request. The address 127.0.0.1 binds the mapping to the host's loopback address, the address a machine uses to reach itself.
Use a private practice environment. In Docker Engine versions earlier than 28.0.0, other machines on the same local network segment may be able to reach ports published to localhost. Docker port publishing
In 127.0.0.1:9090:80, which port should a browser on the Docker host contact?
Testing from the Host
Open http://127.0.0.1:8080 in a browser on the machine where Docker published the port. If Docker runs on a remote machine, typing 127.0.0.1 on your laptop points back at your laptop. For a browser-based lab, use the access method supplied by the lab environment.
Docker published port 8080 on a separate lab machine. Where does 127.0.0.1 point when typed into your laptop's browser?
Checking What Docker Published
The PORTS column of docker ps can show the mapping. Here, the arrow points from the host address and port to the container port. The /tcp suffix names the network protocol used by this mapping.
$ docker ps
STATUS PORTS NAMES
Up 2 minutes 127.0.0.1:8080->80/tcp published-web
A row can show 80/tcp by itself. With no host address and arrow, that entry does not show a published host port.

This capture filters the list to published-web. The curl request runs on the Docker host and confirms that the published address returns a successful response.
A row shows 80/tcp with no arrow. What can you tell from that entry?
Choosing the Address to Test
If the page fails to open, compare the address you entered with the left side of the mapping. An Up status and an arrow tell you what Docker is running and forwarding. A browser request checks whether the server actually returns the page.
On the Docker host, you try port 8080 and get no page. The row shows 127.0.0.1:8081->80/tcp. What should you try next?
Lesson complete
You finished Port Mapping
You can now read a port mapping and use it to choose the correct browser address.
Distinguish the host port from the container port.
Explain where
127.0.0.1points.Recognize a published mapping in the container list.
Use the published host port to test the service.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account