Docker realizes the connection with the same IP network segment

Docker realizes the connection with the same IP network segment

Recently, I solved the problem of Docker and the host communicating on the same network segment, and wrote this article to record the whole process.

For example

Host A and host B are connected through a network. Multiple containers are created on host A to form a cluster, but I hope to access the container of host A through host B. Of course, you may also say that port mapping is very convenient. If I need more ports, or if I need to add some ports temporarily, it may be troublesome to set up. Then, if we put the IP of the container in host A and the IP of the host in the same network, can't we directly interconnect them?

1. Install Docker (Linux server)

Install Docker

yum install docker

2. Use pipework to configure independent IP for docker container

After installing the pipework tool, you can use one command to change the IP address of the container, or more precisely, add a new network card to the container IP address.

wget https://github.com/jpetazzo/pipework/archive/master.zip
unzip master.zip 
cp pipework-master/pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework

3. Edit the IP configuration file, eh0

Edit the default IP configuration file, eth0 or ens33 (different operating systems have different names, for example, the name of the machine I operate is ifcfg-ens33)
vim /etc/sysconfig/network-scripts/ifcfg-ens33

Enter i to enter the edit mode and copy the following content into the file

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=36b40bc6-6775-4e02-8161-e245d0e3892f
DEVICE=ens33
#The following is the bridging part setting ONBOOT=yes
BRIDGE=br0
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

4. Create a custom bridge br0

vim ifcfg-br0

And copy the configuration content to the configuration file

 DEVICE=br0
 BOOTPROTO=static
 NM_CINTROLLER=no
 ONBOOT=yes
 TYPE=Bridge
 IPADDR=192.168.186.128
 NETMASK=255.255.255.0

Restart the virtual machine network service

systemctl restart network

5. Modify the docker configuration file and specify the bridge

Modify the docker configuration file /etc/sysconfig/

vim /etc/sysconfig/docker

The modifications are as follows

OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'

Modified to:

OPTIONS='--selinux-enabled -b=br0'

After modification:

# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
#OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
OPTIONS='--selinux-enabled -b=br0'
if [ -z "${DOCKER_CERT_PATH}" ]; then
  DOCKER_CERT_PATH=/etc/docker
fi

# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# instead. For more information reference the registries.conf(5) man page.

# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
#LOGROTATE=false

# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
other_args='-b br0'

5. Restart the Docker service

systemctl restart docker

6. Create a Docker container instance

docker run -itd --name test1 --net=none centos /bin/bash

--net=none means that the network cards of the container are all empty and need to be customized through pipework

7. Specify the network card

pipework br0 test1 192.168.186.111/[email protected]

8. Enter the container and try to ping the host machine and the IP address in the same network segment to see if it can be pinged successfully.

# Enter the container docker attach test1
# ping host ping 192.168.186.22

8.1 Modify the host IP in the same network segment

Modify the host IP and keep the network segment consistent with the host A bridge IP segment. After setting, hosts A and B can ping each other.

# ping the same network segment IP
ping 192.168.186.33

At this point, the communication between Docker networks is completed.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Correct modification steps for Docker's default network segment
  • How to modify the default network segment of Docker0 bridge in Docker
  • Detailed explanation of Docker container cross-host multi-network segment communication solution
  • Docker specifies an IP address that is in the same network segment as the host.
  • How does Docker allocate host network segment IP?
  • Docker container specifies a fixed IP/static IP address in a custom network segment
  • Analysis of the implementation method of modifying the default network segment of Docker

<<:  Sample code for generating QR code using js

>>:  Summary of coalesce() usage tips in MySQL

Recommend

Install MySQL 5.7.17 in win10 system

Operating system win10 MySQL is the 64-bit zip de...

How to clear default styles and set common styles in CSS

CSS Clear Default Styles The usual clear default ...

Solution to the welcome to emergency mode message when booting CentOS7.4

Today I used a virtual machine to do an experimen...

Problems and experiences encountered in web development

<br />The following are the problems I encou...

In-depth analysis of HTML semantics and its related front-end frameworks

About semantics Semantics is the study of the rel...

The whole process of implementing the summary pop-up window with Vue+Element UI

Scenario: An inspection document has n inspection...

The difference between datatime and timestamp in MySQL

There are three date types in MySQL: date(year-mo...

JavaScript and JQuery Framework Basics Tutorial

Table of contents 1. JS Object DOM –1, Function –...

Web Design Experience

<br />The author used to be a novice in web ...

Summary of MySQL's commonly used database and table sharding solutions

Table of contents 1. Database bottleneck 2. Sub-l...

CSS specification BEM CSS and OOCSS sample code detailed explanation

Preface During project development, due to differ...