Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How docker uses Bridges

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the bridge in docker". In the daily operation, I believe many people have doubts about how to use the bridge in docker. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the bridge in docker". Next, please follow the editor to study!

The docker engine creates a docker0 bridge by default, which connects to other physical or virtual network cards in the kernel layer, which places all containers and hosts on the same layer 2 network.

1. How to use the bridge for docker

1.1 Features of Linux virtual bridge

The bridge works at layer 2 (OSI stack) and is one of the general network devices that can set IP addresses. With the IP address, Linux can locate the bridge at the network layer through the routing table or IP table, which is equivalent to having a virtual network card, namely docker0. The default address division for docker0:

IP: 127.17.42.1/16

MAC: 65534 addresses from 02:42:ac:11:00:00 to 02:42:ac:11:ff:ff

1.2 docker0 bridge connection mode

The network configuration process is as follows:

a. Create a pair of virtual network card veth pair devices on the host. Veth devices always appear in pairs. they form a channel for data, and data enters from one device and comes out of another. Therefore, veth devices are often used to connect two network devices.

b. When the container starts, the Docker engine places one end of the veth pair device in the newly created container and names it eth0. The other end is placed in the host, named after a similar name like veth***, and the network device is added to the docker0 bridge, which can be viewed by the brctl show command.

c. Assign an IP from the docker0 subnet to the container and set the IP address of the docker0 as the default gateway of the container.

2. Implementation of docker0 bridge

2.1 View docker0 Brid

# yum install bridge-utils

/ / View host bridge information

# brctl show

/ / View the Nic information of the container

# docker run-d-name test locutus1/centos / bin/bash

CID#ifconfig

Ctrl+p; ctrl+q

/ / View in the host, docker0's open network interface veth

# brctl show

# ifconfig

2.2 modify docker0 network segment

# ifconfig docker0 192.168.1.10/24 up

/ / verify that the docker0 is changed to the target network segment

# ifconfig docker0

# service docker restart

/ / check the container network interface to verify whether it has been changed to the docker0 network segment

# docker run-d-name test locutus1/centos / bin/bash

CID#ifconfig

2.3 Custom linux virtual bridge br0

/ / add bridge br0

# brctl addbr br0

# ifconfig br0 172.17.0.0/24 up

/ / modify the docker startup configuration file (under centos, the path of the docker startup configuration file is / etc/sysconfig/docker), add the DOCKER_ opts value, and make docker enable the newly added bridge br0.

# vim / etc/sysconfig/docker

DOCKER_OPTS= "- b=br0"

/ / modify the docker daemon configuration file to add the EnvironmentFile absolute path and startup item $DOCKER_OPTS, where "-" represents ignore error

# vim / lib/systemd/system/docker.service

EnvironmentFile=-/etc/sysconfig/docker

ExecStart=/usr/bin/dockerd $DOCKER_OPTS

/ / restart all daemons. Reload the unit files for all daemons, and then regenerate the dependency tree

# systemctl daemon-reload

# service docker restart

/ / View the container IP and verify that it is changed to the br0 segment

# docker run-it-- name test2 centos / bin/bash

CID# yum install-y net-tools.x86_64

CID# ifconfig

Eth0: flags=4163 mtu 1500

Inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0

Inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20

Ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)

RX packets 1188 bytes 7830677 (7.4 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 1073 bytes 73229 (71.5 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

At this point, the study on "how to use the bridge in docker" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report