In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Reference: http://www.runoob.com/docker/docker-container-connection.html
Earlier, we implemented access to services running in the docker container through network ports. Let's implement a port connection to a docker container
Network port mapping
We created a container for python applications.
[root@huixuan] # docker run-d-P training/webapp python app.py
Eda68d54e9976f0894461dc61b35fb1ebc607ae19954bb76fd68a316ece6027d
[root@huixuan ~] # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Docker.io/centos latest 3a42651974ec 7 minutes ago 199 MB
Tonykorn97/centos dev 3a42651974ec 7 minutes ago 199 MB
Tonykorn97/ubuntu v2 6bb0f7a6d1e6 34 minutes ago 137 MB
3248dea71f18 36 minutes ago 137 MB
Docker.io/ubuntu 14.04 8cef1fa16c77 3 days ago 223 MB
Docker.io/hello-world latest e38bc07ac18e 2 weeks ago 1.85 kB
Docker.io/centos e934aafc2206 3 weeks ago 199 MB
Docker.io/ubuntu 15.10 9b9cb95443b5 21 months ago 137 MB
Docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@huixuan ~] #
In addition, we can specify the network address of the container binding, such as binding 127.0.0.1.
We use the-P parameter to create a container and use docker ps to see that port 1026 binds the host port 5000.
[root@huixuan ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Eda68d54e997 training/webapp "python app.py" 3 minutes ago Up 2 minutes 0.0.0.0 minutes ago Up 1026-> 5000/tcp blissful_goldstine
0e044f323370 training/webapp "python app.py" About an hour ago Up About an hour 0.0.0.0About an hour ago Up About an hour 5000-> 5000/tcp admiring_goldwasser
[root@huixuan ~] #
We can also use the-p flag to specify that the container port is bound to the host port.
The difference between the two ways is:
-P: the internal port of the container is randomly mapped to the high port of the host.
-p: the internal port of the container is bound to the specified host port.
[root@huixuan] # docker run-d-p 5000 training/webapp python app.py
56af280bb2b399399a9a40a5fa52ac0d7e787cca7ac2446ccbbb3faecccde935
/ usr/bin/docker-current: Error response from daemon: driver failed programming external connectivity on endpoint laughing_heisenberg (b3ee694cd9c6bb3f7c8d7129aca3bf99b5eabc023daab0d08a5e78358d23b08e): Bind for 0.0.0.0 b3ee694cd9c6bb3f7c8d7129aca3bf99b5eabc023daab0d08a5e78358d23b08e 5000 failed: port is already allocated.
[root@huixuan ~] # docker stop 0e044f323370
0e044f323370
[root@huixuan ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Eda68d54e997 training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0 minutes ago Up 1026-> 5000/tcp blissful_goldstine
[root@huixuan] # docker run-d-p 5000 training/webapp python app.py
Fcced8f54a68432cb652a7b755047160869ff9110cd6ec2b850f14a666a72b28
[root@huixuan ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Fcced8f54a68 training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0 seconds ago Up 5000-> 5000/tcp kickass_pare
Eda68d54e997 training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0 minutes ago Up 1026-> 5000/tcp blissful_goldstine
[root@huixuan ~] #
In addition, we can specify the network address of the container binding, such as binding 127.0.0.1.
[root@huixuan ~] # docker run-d-p 127.0.0.1 training/webapp python app.py 5001 training/webapp python app.py
Eda9d7e0083933213dff616a38e39975c89783f018ae9554c87493cc2028e247
[root@huixuan ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Eda9d7e00839 training/webapp "python app.py" 3 seconds ago Up 2 seconds 127.0.0.1 seconds ago Up 5001-> 5000/tcp mystifying_haibt
Fcced8f54a68 training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0About a minute ago Up About a minute 5000-> 5000/tcp kickass_pare
Eda68d54e997 training/webapp "python app.py" 6 minutes ago Up 6 minutes 0.0.0.0 minutes ago Up 1026-> 5000/tcp blissful_goldstine
[root@huixuan ~] #
In this way, we can access port 5000 of the container by accessing 127.0.0.1 virtual 5001.
In the above example, the default is to bind the tcp port. If you want to bind the UDP port, you can add / udp after the port.
[root@huixuan] # docker run-d-p 127.0.0.1:5000:5000/udp training/webapp python app.py
2eaa281d655cf95a2df079b8488e0e5cc64850e610ad522ba14a0e2ba9ca2e02
[root@huixuan ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2eaa281d655c training/webapp "python app.py" 2 seconds ago Up 1 second 5000/tcp, 127.0.0.1 seconds ago Up 5000-> 5000/udp nifty_swirles
Eda9d7e00839 training/webapp "python app.py" About a minute ago Up About a minute 127.0.0.1 About a minute ago Up About a minute 5001-> 5000/tcp mystifying_haibt
Fcced8f54a68 training/webapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0 minutes 5000-> 5000/tcp kickass_pare
Eda68d54e997 training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0 minutes ago Up 1026-> 5000/tcp blissful_goldstine
[root@huixuan ~] #
The docker port command allows us to quickly view the binding of ports.
[root@huixuan ~] # docker port 2eaa281d655c
5000/udp-> 127.0.0.1 VR 5000
[root@huixuan ~] # docker port eda9d7e00839
5000/tcp-> 127.0.0.1purl 5001
[root@huixuan ~] #
Docker container connection
Port mapping is not the only way to connect docker to another container.
Docker has a connection system that allows multiple containers to be connected together to share connection information.
The docker connection creates a parent-child relationship where the parent container can see the information of the child container.
Container naming
When we create a container, docker automatically names it. Alternatively, we can use the-- name identity to name the container, for example:
Stop the previous one first:
[root@huixuan ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Fcced8f54a68 training/webapp "python app.py" 29 minutes ago Up 29 minutes 0.0.0.0 minutes 5000-> 5000/tcp kickass_pare
Eda68d54e997 training/webapp "python app.py" 35 minutes ago Up 35 minutes 0.0.0.0 minutes ago Up 1026-> 5000/tcp blissful_goldstine
[root@huixuan ~] # docker stop fcced8f54a68
Fcced8f54a68
[root@huixuan ~] # docker stop eda68d54e997
Eda68d54e997
[root@huixuan ~] #
[root@huixuan] # docker run-d-P-- name tonykorn97 training/webapp python app.py
C793b0a10ab17912d4b7138bd58f201dcc7479b27840426a79d3e3d921af7d6e
We can use the docker ps command to see the container name.
[root@huixuan ~] # docker ps-l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C793b0a10ab1 training/webapp "python app.py" 7 seconds ago Up 6 seconds 0.0.0.0 seconds ago Up 1027-> 5000/tcp tonykorn97
[root@huixuan ~] #
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.