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 modify ip and Port Mapping of Container by docker

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how docker modifies container ip and port mapping". In daily operation, I believe many people have doubts about how docker modifies container ip and port mapping. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how docker modifies container ip and port mapping". Next, please follow the editor to study!

How does docker modify the ip and port mapping of a container? By default, the container can establish a link to the public network, but the external network cannot connect to the container. Docker allows network services to be provided through external access containers or interconnection of containers. External access containers can run network applications in containers. The port mapping can be specified through-p or-P parameters.

-P: default designated port-p custom designated port. The following operations are simulated by myfirstapp image

1. External access container

After the docker container starts, some network applications can be run in the container service, specifying the port mapping through the-p or-P parameters.

A. When marked with-P (uppercase), docker will randomly select a port to map to the open network port inside the container.

$docker run-d-P myfirstapp python app.py$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdbcf5d3290ba myfirstapp "python app.py" 4 seconds ago Up 4 seconds 0.0.0.0 python app.py 32770-> 80/tcp quizzical_engelbart

At this point, you can access the interface provided by the web application in the container by accessing port 32770 of this machine.

$curl http://192.168.1.160:32770/Hello Hellotine Hostname: dbcf5d3290baVisits: cannot connect to Redis, counter disabled

You can also use docker logs to view the information of the application

$docker logs dbcf5d3290ba* Running on http://0.0.0.0:80/ (Press CTRL+C to quit) 192.168.1.5-- [29/Mar/2018 06:17:34] "GET / HTTP/1.1" 200-192.168.1.5-- [29/Mar/2018 06:17:34] "GET / favicon.ico HTTP/1.1" 404-192.168.1.160-- [29/Mar/2018 06: 17:43] "GET / HTTP/1.1" 200-192.168.1.5-- [29/Mar/2018 06:18:59] "GET / HTTP/1.1" 200-

B. When using the-p (lowercase) flag, you can specify the port to be mapped, and only one container can be bound on a specified port. The supported formats are:

Ip:hostport:containerportip::containerporthostport:containerport$ docker run-d-p 4000 myfirstapp python app.py$ docker run-d-p 4001 myfirstapp python app.py$ docker run-d-p 4001 myfirstapp python app.py$ curl Helllogged Hostname: f43ed2810353Visits: cannot connect to Redis, counter disabled$ curl http://192.168.1.160:4001/Hello Hellotine Hostname: f43ed2810353Visits: cannot connect to Redis, counter disabled

You can see that local port 4000 has been mapped, and another port 4001 has also been mapped.

Docker maps all local addresses by default. Now let's try a designated port that maps to a specified address

$docker run-d-p 127.0.0.1 myfirstapp python app.py$ curl http://127.0.0.1:4000/ # # access via $curl http://localhost:4000/ # # access through $curl http://192.168.1.160:4000/ # # access deny curl: (7) Failed connect to 192.168.1.160 http://192.168.1.160:4000/ 4000; deny connection

Then the applications in the container can only be accessed through the ip 127.0.0.1.

The next step is to bind any port of the local machine to port 80 of the container, and randomly assign a port

$docker run-d-p 127.0.0.1 myfirstapp python app.py$ docker ps 80 myfirstapp python app.py$ docker ps # # 127.0.0.1 http://192.168.1.160:32770/ 32770-> 80 http://127.0.0.1:32770/ http://127.0.0.1:32770/ # # access via $curl http://192.168.1.160:32770/ # # access denied by $curl http://192.168.1.160:32770/ #

You can also specify a communication protocol

Docker run-d-p 5003:80/udp myfirstapp python app.py

two。 View the ports and Ip addresses bound and mapped by the container

$docker port 44de1b0b5312 (container ID) 80/tcp-> 127.0.0.1 80/tcp 32770 here, the study on "how to modify the container ip and port mapping by docker" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

*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