Skip to main content

How to Publish a Docker Container's Port Only on Your Computer

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A program in a Docker container listens on a port inside the container, which your computer can't reach until you publish it with -p. Written as -p 8081:80, Docker publishes it on every network interface, so other computers on your network can reach it too. Written as -p 127.0.0.1:8081:80, only your own computer can. This guide publishes nginx both ways and checks each with docker port, with real output from Docker Desktop 4.100.1.

Syntax​

Syntax
docker run -d --name NAME -p 127.0.0.1:HOST_PORT:CONTAINER_PORT IMAGE
PartWhat it means
127.0.0.1Publish on your computer's own address only
HOST_PORTThe port on your computer, such as 8081
CONTAINER_PORTThe port the program listens on inside the container, such as nginx's 80

The docker run reference: "If you don't specify an IP address (i.e., -p 80:80 instead of -p 127.0.0.1:80:80) when publishing a container's ports, Docker publishes the port on all interfaces (address 0.0.0.0) by default. These ports are externally accessible." The same commands work in PowerShell.

Prerequisites​

  • Docker: Docker Desktop on Mac or Windows, running.

Step 1: Publish on Your Computer Only​

nginx listens on port 80 inside its container. Publish it on port 8081 of 127.0.0.1:

Terminal
docker run -d --name web -p 127.0.0.1:8081:80 nginx:alpine

Ask it for its home page, and show only the page's title, with grep, or Select-String in PowerShell:

Terminal
curl -s http://127.0.0.1:8081 | grep '<title>'
Output
<title>Welcome to nginx!</title>

nginx answered from inside the container. docker port shows where the port is published:

Terminal
docker port web
Output
80/tcp -> 127.0.0.1:8081

curl's output in a terminal: the line <title>Welcome to nginx!</title>, then docker port's 80/tcp -> 127.0.0.1:8081.

The container's port 80 is published on 127.0.0.1:8081: your own computer, and nothing else.

Step 2: Compare Without the Address​

Stop and remove that container (docker stop web, then docker rm web), and start one without an address:

Terminal
docker run -d --name web-open -p 8082:80 nginx:alpine
docker port web-open
Output (after the container ID)
80/tcp -> 0.0.0.0:8082
80/tcp -> [::]:8082

docker port's output in a terminal: 80/tcp -> 0.0.0.0:8082 and 80/tcp -> [::]:8082.

0.0.0.0 and [::] mean every network interface, for IPv4 and IPv6: anyone who can reach your computer on the network can reach this nginx. For a server you're only trying out, or one without its own security, keep 127.0.0.1. Stop and remove web-open too.

Common Mistakes​

MistakeWhat happensDo this instead
-p 8082:80 for something only you should reachIt's published on every interface, 0.0.0.0 and [::]-p 127.0.0.1:8082:80
A program in the container that listens on the container's own 127.0.0.1The published port reaches the container, but nothing answers there: curl: (52) Empty reply from serverThe program must listen on every address inside the container, 0.0.0.0

Troubleshooting​

SymptomLikely causeFix
curl: (7) Failed to connectThe container isn't running, or the port isn't publisheddocker ps and docker port NAME
docker port prints nothingThe container was started without -pStart it again with -p

FAQ​

How do I see the ports of a running container?​

docker port NAME, as above. For a container started without -p, it prints nothing.

What's the difference between the two addresses in Step 2?​

0.0.0.0 is every IPv4 interface and [::] every IPv6 one. With 127.0.0.1, Docker publishes on one address, your computer's own.

Conclusion​

-p 127.0.0.1:8081:80 published nginx's port 80 on 127.0.0.1:8081 only, as docker port showed, while -p 8082:80 published it on 0.0.0.0:8082 and [::]:8082, every interface.

Additional Resources​