In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Docker to achieve a custom network, I believe that 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 go to understand it!
1. Custom network to realize container interconnection
Four types of network mode, use docker network ls to view docker network mode
Docker network mode configuration describes host mode-the net=host container and the host share the Network namespace. Container mode-the net=container:NAME_or_ID container shares Network namespace with another container. Pod in kubernetes means that multiple containers share a Network namespace. None mode-the net=none container has a separate Network namespace, but does not have any network settings for it, such as assigning veth pair and bridge connections, configuring IP, and so on. Bridge mode-net=bridge bridging mode (default is this mode)
-net=bridge this is the default value, which connects to the default bridge.
-net=host tells Docker not to put the container network in an isolated namespace, that is, not to containerize the network in the container. At this point, the container uses the network of the local host, which has full access to the local host interface. Like other root processes of the host, the container process can open low-range ports, access local network services such as D-bus, and allow the container to do some things that affect the entire host system, such as restarting the host. So be very careful when using this option. If you further use-- privileged=true, the container is allowed to configure the host's network stack directly.
-net=container:NAME_or_ID asks Docker to put the process of the new container on the network stack of an existing container. The new container process has its own file system, process list and resource restrictions, but shares network resources such as IP address and port with the existing container. The two processes can communicate directly through the lo loopback interface.
-net=none asks Docker to put the new container on an isolated network stack without network configuration. After that, the user can configure it himself.
Custom network
#-- driver bridge bridging mode #-- subnet 192.168.0.0 16 sub-network segment #-- gateway 192.168.0.1 gateway # mynet network name docker network create-- driver bridge-- subnet 192.168.0.0 16-- gateway 192.168.0.1 mynet
View custom network information:
Docker network inspect mynet
Create a container in a custom network
Docker run-d-P-name tomcat01-net-01-- net mynet tomcatdocker run-d-P-- name tomcat01-net-02-- net mynet tomcat
Check the custom network information again: find that the two newly created containers have been added to the network.
Ping test each other using container names
Tomcat01-net-01 ping tomcat01-net-02:
Tomcat01-net-02ping tomcat01-net-01:
Don't use it now-link containers can also ping each other, and containers can communicate with each other.
2. Network connectivity
Demand analysis diagram
Create two containers, tomcat01 and tomcat02:
Docker run-d-P-- name tomcat01 tomcatdocker run-d-P-- name tomcat02 tomcat
Obviously, direct ping communication is impossible, and you need to connect the tomcat01 container to the mynet network first.
Docker network connect
The docker network connect command is used to connect a docker container to a network, using either the container name or the container ID.
Usage:
Docker network connect [OPTIONS] NETWORK CONTAINER
Connect the tomcat01 container to the mynet network:
Docker network connect mynet tomcat01
View mynet's network information: docker inspect mynet
Tomcat01 is already connected to the mynet network, like tomcat01-net-tomcat01 and tomcat01-net-tomcat02.
At this point, tomcat01 is connected to two containers in the mynet network, because the three containers are on the same network and can of course communicate with each other:
One container, two networks:
View the metadata of tomcat01: docker inspect tomcat01
Once tomcat01 is connected to a custom mynet network, there are two networks.
The above is all the contents of the article "how to implement a custom network in Docker". 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: 227
*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.