Docker study notes-Bridge Network

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.

Docker study notes-Bridge Network

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

Docker study notes-Bridge Network

Check out the specific content of this network

Docker study notes-Bridge 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

Docker study notes-Bridge Network

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

Docker study notes-Bridge Network

Check out the gateway

netstat -rn

Docker study notes-Bridge Network

Take a look at DNS

cat /etc/resolv.conf

Docker study notes-Bridge Network

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

Docker study notes-Bridge Network

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/>

Docker study notes-Bridge Network

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

Docker study notes-Bridge Network

But if you add him to our localhost network, you can communicate with each other

docker network connect localhost ping-mysql

Docker study notes-Bridge Network

If you check, you can see that he does belong to two networks
docker container inspect ping-mysql

Docker study notes-Bridge Network

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
Docker study notes-Bridge Network

But it can be accessed from the inside

Docker study notes-Bridge Network

Guess you like

Origin blog.51cto.com/beanxyz/2676182