In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how the Docker container binds external IP and ports, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Docker allows network services to be provided through external access to containers or the interconnection between containers.
1. External access container
After the container starts, some network applications can be run in the container, 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 docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdbcf5d3290ba myfirstapp 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:containerport ip::containerport hostport:containerport$ docker run-d-p 4000 myfirstapp python app.py$ docker run-d-p 4001 myfirstapp python app.py$ docker run 80 myfirstapp python app.py$ curl http://192.168.1.160:4000/Hello Hellotine Hostname: f43ed2810353Visits:cannot connect to Redis, counter disabled$ curl http://192.168.1.160:4001/Hello HELLO! 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
3. Matters needing attention
A.docker inspect looks at the container's own internal network and ip address
$docker inspect 44de1b0b5312 (container ID)
b. Container starts binding multiple IP
$docker run-d-p 50050.0.0.0:500580/tcp 5000-p 5006ID:44e703c1279a$ docker port 44e703c1279a5000/tcp 80 myfirstapp python app.py # Container ID:44e703c1279a$ docker port 44e703c1279a5000/tcp-> 0.0.0.0:500580/tcp-> 0.0.0.0 ID:44e703c1279a$ docker port 44e703c1279a5000/tcp 5006 is all the content of this article "how to bind external IP and Port in Docker Container". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.