Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to bind a port in Docker

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How to bind a port in Docker? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

External access container

Some network applications can be run in the container, and to make them accessible externally, you can specify the port mapping with the-P or-p parameter.

When-P marking

Docker randomly maps a port of 49000 to 49900 to a network port open in the internal container.

When marked with-p

You can specify the port to map, and only one container can be bound on a specified port. Supported formats are

Ip:hostPort:containerPort

Ip::containerPort

HostPort:containerPort

Map all interface addresses

Use the local port 5000 in hostPort:containerPort format to map to port 5000 of the container

$sudo docker run-d-p 5000UR 5000 training/webapp python app.py

All addresses on all local interfaces are bound by default.

Map to the specified port of the specified address

You can use the ip:hostPort:containerPort format to specify that the mapping uses a specific address, such as the localhost address 127.0.0.1

$sudo docker run-d-p 127.0.0.1 purl 5000 training/webapp python app.py

Map to any port at the specified address

Use ip::containerPort to bind any port of localhost to port 5000 of the container, and the local host will automatically assign a port.

$sudo docker run-d-p 127.0.0.1 udp training/webapp python app.py# can also use the udp tag to specify the udp port $sudo docker run-d-p 127.0.0.1:5000:5000/udp training/webapp python app.py

View mapped port configuration

Use docker port to view the port configuration of the current mapping, and you can also see the bound address

$docker port

Note:

The container has its own internal network and ip address (all variables can be obtained using docker inspect, and Docker can have a variable network configuration. )

The-p flag can be used multiple times to bind multiple ports, such as

After reading the above, do you know how to bind ports in Docker? $sudo docker run-d-p 5000-p 3000 Docker 80 training/webapp python app.py. If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 259

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report