Creating a Docker network

Docker network introduction

By default, several network modes of bridge, host, none, overlay, maclan and Network plugins are provided. When running the container, you can set the specific mode to be used through the -network parameter

  • bridge: This is the default network driver of Docker. This mode will allocate Network Namespace and set IP for each container, and connect the container to a virtual bridge. If no network driver is specified, this will be used by default

  • host: This network driver directly uses the host's network

  • none: This driver does not construct a network environment. If the none network driver is used, then only the loopback network device can be used, and the container can only use the local network of 127.0.0.1

  • overlay: This network driver can connect multiple Docker daemons together and communicate between swarm services. You can also use the overlay network for communication between swarm services and containers, and between containers

  • macvlan: This network allows to specify a MAC address for the container, allowing the container to be a physical device in the network, so that the Docker daemon can access the route through the MAC address. For legacy applications that want to connect directly to the network, this network driver may sometimes be the best choice

  • Network plugins: You can install and use third-party network plugins. These plugins can be obtained from the Docker Store or third-party vendors

By default, Docker uses the bridge network mode. The schematic diagram of the bridge network driver is as follows. This article describes the Docker network in the bridge mode.

Docker Container Networking

  1. The construction process of the bridge network
    When installing Docker, create a virtual bridge named docke0. The virtual bridge uses "10.0.0.0 -10.255.255.255", "172.16.0.0-172.31.255.255" and "192.168.0.0——192.168.255.255". Private network address range
    You can view the information of the docker0 bridge through the ifconfig command:

bridge network

You can view the subnet network range and gateway of the bridge through docker network inspect bridge:

inspect bridge

  1. When running the container, create a virtual network card veth pair device on the host. The veth pair devices appear in pairs to form a data channel. When data enters from one device, it will come out from another device.
    Put one end of the veth pair device in the newly created container and name it eth0; put the other end in the docker0 of the host machine and name it with the prefix veth. View the veth pair device placed in docker0 through the brctl show command

veth pair

  1. External access
    The docker0 of the bridge is a virtual bridge, so it cannot be accessed by the external network. Therefore, it is necessary to map the port of the container to the port of the host through the -p and -P parameter pairs when running the container. In fact, Docker uses NAT to bind the service monitoring port inside the container to a certain port port of the host, so that the outside of the host can send network packets to the container

Create a Docker network

Bridge networks is the most common network type in docker,

  1. Create a bridge network

    $ docker network create --driver bridge szer1

    image.png

  2. View network details

    $ docker network inspect szer1

    image.png
    image.png

  3. Run the webserver container on the newly created network
    --network specifies the network

    docker run -it --rm -p 80:80 --network szer1 --mount source=szer1,destination=/usr/share/nginx/html --name web webserver
  4. View the containers running under the szer1 network

    $ docker network inspect szer1

image.png

Likes(0)

Comment list count 0 Comments

No Comments

WeChat Self-Service

WeChat Consult

TaoBao

support@elephdev.com

发表
评论
Go
Top