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

The method of realizing Network access by Docker Port Mapping

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

After Docker runs the container, it is found that there is no IP or port, so how do you access the container?

Next, I will introduce docker to achieve network access through port mapping.

First, access container applications from the outside

When starting the container, if you do not specify the corresponding parameters, the network applications and services in the container cannot be accessed through the network outside the container.

When some network applications are running in the container, you can specify the port mapping through the-P or-p parameter for external access to these applications.

Let's talk about p and P first.

-p can specify the port to be mapped, and only one container can be bound on a specified port-P it will randomly map a port to the open network port inside the container (the range is unknown, it seems to be tens of thousands)

Just to be clear, my side

Client ip address is 192.168.0.225

Registry ip address is 192.168.0.216Vl5000

Docker run-d-it-- name nginx-P 192.168.0.216:5000/nginxdocker ps-a # check whether the container is running docker logs nginx # View the log of the nginx container

The formats supported by port mapping are:

Ip:hostport:containerport # specify ip, specify host port, specify container portip::containerport # specify ip, specify host port, specify container porthostport:container # not specify ip port, specify host port, specify container port

2. Map all interfaces ip

The following command views to 0.0.0.0 listening on port 5000

Docker run-d-it-p 5000 name registry 192.168.0.216:5000/registrydocker ps-a | grep 5000

Use the-p flag multiple times to bind multiple ports

Docker run-d-it-p 800 name tomcat 192.168.0.216:5000/tomcatdocker ps 80-p 8088 Swiss 8080-- name tomcat 192.168.0.216:5000/tomcatdocker ps-a | egrep "(80 | 8080)"

3. Map to the designated port of the specified address

Map 80 port of the container to 192.168.0.225 port 800

Docker run-d-it-p 192.168.0.225 name web_server 192.168.0.216:5000/nginxcurl 800lv 80-- 192.168.0.225 name web_server 192.168.0.216:5000/nginxcurl verify that you can access the default home page

4. Map to the udp port of the specified address

Not to mention whether it can be accessed or not, it's just a way of mapping. No offense, everyone.

Docker run-d-it-p 192.168.0.225:300:3000/udp-- name unknown 192.168.0.216:5000/nginxnetstat-anpu | grep udpudp 00 192.168.0.225 docker run 300 0.0.0.0 * 24588/docker-proxy

5. View the mapped port configuration

Through docker port container_ID, you can see which ports and protocols are mapped by the container.

[root@docker ~] # docker port tomcat8080/tcp-> 192.168.0.225 root@docker 32769 [root@docker] # docker port unknown3000/udp-> 192.168.0.225 root@docker 300

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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: 285

*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