Docker container accesses host services

Introduction to docker networking

dockerThree networks will be created by default during installation, bridge(the created container is connected to this network by default), none, host.

  • hostShare the network directly with the host machine.
  • bridgedocker0Network isolation, communicating with the host through a virtual bridge (usually ).
  • noneDisable network functionality.

docker network lsYou 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

DockerWhen creating a container, the following operations will be performed:

  1. Create a pair of virtual interfaces and place them on the local host and the new container respectively;
  2. The local host is bridged to the default docker0or designated bridge and has a unique name, such as veth65f9;
  3. 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;
  4. 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 eth0the 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.1to localhostaccess the host.

dockerUse in to --network hostconfigure hostnetworking for the container:

docker run --network host host-main

docker-composeUse network_mode: "host"to configure:

version: "3"
    
services:
  main:
    build:
      context: .
      dockerfile: ./dockerfile
    network_mode: "host"

bridge mode

Linux

You can use the host docker0on IPinstead localhost.

  1. Query hostIP
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 IPYes 172.17.0.1, then http://localhost:4780change the address to http://172.17.0.1:4780and you can access it.

Windows

In the same way, Windowswe can also find the virtual network card and check IPto replace it.
Insert image description here

http://localhost:4780 => http://192.168.236.129:4780

WindowsThe host can also be resolved through host.docker.internalthis special name . Mapping , the file contents are as follows:DNSIPhostsC:\Windows\System32\drivers\etc\hosts
Insert image description here

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

Guess you like

Origin blog.csdn.net/DisMisPres/article/details/127279708