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

Teach you how to get the pointer position in javascript

The method of obtaining the position of the point...

Some pitfalls of JavaScript deep copy

Preface When I went to an interview at a company ...

MySQL recursion problem

MySQL itself does not support recursive syntax, b...

A brief discussion on JS packaging objects

Table of contents Overview definition Instance Me...

Discussion on the problem of garbled characters in iframe page parameters

I encountered a very unusual parameter garbled pro...

How to draw the timeline with vue+canvas

This article example shares the specific code of ...

JS array loop method and efficiency analysis comparison

Array Methods JavaScript has provided many array ...

Linux checkup, understand your Linux status (network IO, disk, CPU, memory)

Table of contents 1. Core commands 2. Common comm...

Introduction to possible problems after installing Tomcat

1. Tomcat service is not open Enter localhost:808...

Common pitfalls of using React Hooks

React Hooks is a new feature introduced in React ...

vue3.0+echarts realizes three-dimensional column chart

Preface: Vue3.0 implements echarts three-dimensio...

Detailed explanation of the sticky position attribute in CSS

When developing mobile apps, you often encounter ...

Change the MySQL database engine to InnoDB

PS: I use PHPStudy2016 here 1. Stop MySQL during ...

Based on JavaScript ES new features let and const keywords

Table of contents 1. let keyword 1.1 Basic Usage ...

How to align text boxes in multiple forms in HTML

The form code is as shown in the figure. The styl...