Introduction to docker networking
docker
Three networks will be created by default during installation, bridge
(the created container is connected to this network by default), none
, host
.
host
Share the network directly with the host machine.bridge
docker0
Network isolation, communicating with the host through a virtual bridge (usually ).none
Disable network functionality.
docker network ls
You can view the network settings and the output is as follows:
NETWORK ID NAME DRIVER SCOPE
1363ad4ba2a5 bridge bridge local
715796504a13 host host local
723f2680cdad none null local
Network initialization when creating a container
Docker
When creating a container, the following operations will be performed:
- Create a pair of virtual interfaces and place them on the local host and the new container respectively;
- The local host is bridged to the default
docker0
or designated bridge and has a unique name, such asveth65f9
; - Place one end of the container into a new container and change the name
eth0
. This interface is only visible in the namespace of the container; - Obtain an idle address from the bridge's available address segment and assign it to the container's eth0, and configure the default route to the bridge network card
veth65f9
.
After completing this, the container can use eth0
the virtual network card to connect to other containers and other networks.
Use host IP
host mode
In this mode, the container can be used directly 127.0.0.1
to localhost
access the host.
docker
Use in to --network host
configure host
networking for the container:
docker run --network host host-main
docker-compose
Use network_mode: "host"
to configure:
version: "3"
services:
main:
build:
context: .
dockerfile: ./dockerfile
network_mode: "host"
bridge mode
Linux
You can use the host docker0
on IP
instead localhost
.
- Query host
IP
ip addr show docker0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:5a:02:53:f5 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:5aff:fe02:53f5/64 scope link
valid_lft forever preferred_lft forever
You can find the host machine's IP
Yes 172.17.0.1
, then http://localhost:4780
change the address to http://172.17.0.1:4780
and you can access it.
Windows
In the same way, Windows
we can also find the virtual network card and check IP
to replace it.
http://localhost:4780
=> http://192.168.236.129:4780
Windows
The host can also be resolved through host.docker.internal
this special name . Mapping , the file contents are as follows:DNS
IP
hosts
C:\Windows\System32\drivers\etc\hosts
http://localhost:4780
=>http://host.docker.internal:4780
reference
Official website
Docker network
Detailed explanation of Docker network - Principle
Docker container accesses the host network