Docker's network can be divided into Bridge, Host, Overlay, None and Macvlan. Among them, the default type is the Bridge type. Let's take a look at a few common commands and configurations.
Similar to the virtual machine and the host's network relationship, the container's host can also have different networks. Containers on the same network can communicate with each other.
Example 1
Create a Bridge network, specify subnet and gateway
docker network create --subnet 10.1.0.0/24 --gateway 10.1.0.1 br02
Check it after creation and find that there is an additional network interface on our host, which happens to be our gateway address
Check out the specific content of this network
Example 2
First delete the previous network, and then create a new one. This time, ip-range and label are also specified on the basis of Example 1.
docker network prune
docker network create --subnet 10.1.0.0/16 --gateway 10.1.0.1 \
--ip-range=10.1.4.0/24 --driver=bridge --label=host4network br04
Create a new container on our network
docker container run --name network-test01 -it --network br04 centos /bin/bash
Then install network tools
yum update -y
yum install -y net-tools
Then check the address
Check out the gateway
netstat -rn
Take a look at DNS
cat /etc/resolv.conf
Example 3
Assign a specified IP address to the container
docker container run -d --name network-test02 --ip 10.1.4.102 --network br04 nginx
Example 4
Connect 2 containers in a network
First create a localhost network
docker network create -d bridge --internal localhost
Then create a new Mysql container on this network
<br/>docker container run -d --name test_mysql \<br/>-e MYSQL_ROOT_PASSWORD=P4sSw0rd0 \<br/>--network localhost mysql:5.7<br/>
Create a new container on the Bridge network
<br/>docker container run -it --name ping-mysql \<br/>--network bridge \<br/>centos<br/>
He can’t ping through at this time because he belongs to a different network
But if you add him to our localhost network, you can communicate with each other
docker network connect localhost ping-mysql
If you check, you can see that he does belong to two networksdocker container inspect ping-mysql
Example 5
We create a nginx container in our localhost network
docker container run -d --name private-nginx -p 8081:80 --network localhost nginx
Found that he is inaccessible from the outside
But it can be accessed from the inside