Docker custom network and docker custom
Docker automatically creates three types of networks: none, host, and bridge. You can also customize networks as needed, including bridge, overlay, and macvlan. overlay and macvlan are cross-host networks.
Common sub-commands for docker network are as follows:
docker network createdocker network connectdocker network lsdocker network rmdocker network disconnectdocker network inspect
The default network is as follows:
Network Structure
Create a bridge network and specify the ip network segment (which can be automatically allocated)
docker network create --driver bridge --subnet 172.14.14.0/24 --gateway 172.14.14.1 net1
You can also see the br-c201be4aa4a6 after executing the ifconfig command
Allocate the added bridge network to the container
docker run -d --name httpd1 --network=net1 --ip 172.14.14.12 -p 8081:80 dbfc2cbe2971
Curl 172.14.14.12 can be accessed normally
Looking at the network structure, we will find that the new network interface is mounted to net1. vethaef4a02 is the virtual network card of the container.
After entering the container, you can see that the NIC is different from the preceding virtual Nic. The two are a pair of veth pair, which can be equivalent to a virtual network cable connecting a pair of NICs, one is a container and the other is net1, which is equivalent to adding the container Nic to net1.
Create another container 2
docker run -d --name httpd2 --network=net1 -p 8082:80 dbfc2cbe2971
Enter the container and ping the httpd1 ip address.
In the same network, containers and gateways can communicate normally.
Create container 3, default gateway docker0
docker run -d --name httpd3 -p 8083:80 dbfc2cbe2971
Then ping the ip address of container 1 to display failure, indicating that the two networks are independent.
If you want to allow container 3 to ping the ip address of container 1, you need to add container 3 to the network where container 1 is located.
docker network connect net1 httpd3
View ifconfig. eth1 is added to httpd3.
Then ping again.
In addition, you can ping the container name directly. (Embedded DNS server) can only communicate between container names in a custom network. By default, docker0 cannot communicate with each other by container name (dns is unavailable ).