Docker custom network implementation

Docker custom network implementation

1. Customize the network to realize container interconnection

Four types of network modes, use docker network ls to view the docker network mode

Docker Network Mode Configuration illustrate
Host mode –net=host The container and the host share the Network namespace.
Container mode –net=container:NAME_or_ID The container shares the Network namespace with another container. A pod in kubernetes is multiple containers sharing a Network namespace.
None mode –net=none The container has an independent Network namespace, but no network settings are performed on it, such as allocating veth pairs and bridge connections, configuring IP, etc.
Bridge mode –net=bridge Bridge mode (this mode is the default)

–net=bridge This is the default value, connecting to the default bridge.

–net=host tells Docker not to put the container network into an isolated namespace, that is, not to containerize the network inside the container. At this point the container uses the local host's network and has full access to the local host interface. The container process can open low-range ports like other root processes on the host, access local network services such as D-bus, and allow the container to do things that affect the entire host system, such as restarting the host. Therefore use this option with caution. If --privileged=true is further used, the container will be allowed to directly configure the host's network stack.

–net=container:NAME_or_ID tells Docker to put the newly created container process into the network stack of an existing container. The new container process has its own file system, process list, and resource limits, but will share network resources such as IP addresses and ports with the existing container. The two processes can communicate directly through the lo loopback interface.

–net=none tells Docker to put the new container into an isolated network stack, but not to do any network configuration. Afterwards, users can configure it themselves.

Custom Network

# --driver bridge bridge mode# --subnet 192.168.0.0/16 subnet segment# --gateway 192.168.0.1 gateway# mynet network namedocker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet

insert image description here

View customized network information:

docker network inspect mynet 

insert image description here

Creating a container in a custom network

docker run -d -P --name tomcat01-net-01 --net mynet tomcat
docker run -d -P --name tomcat01-net-02 --net mynet tomcat

insert image description here

Check the custom network information again: the two newly created containers are found to be added to the network.

insert image description here

Ping test between containers using their names

tomcat01-net-01 pings tomcat01-net-02:

insert image description here

tomcat01-net-02 pings tomcat01-net-01:

insert image description here

Now containers can ping each other without using --link, and containers can communicate with each other.

2. Network connectivity

Demand analysis diagram

insert image description here

Create two containers, tomcat01 and tomcat02:

docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat02 tomcat

insert image description here

Obviously, it is impossible to ping directly. You need to connect the tomcat01 container to the mynet network first.

docker network connect

The docker network connect command is used to connect a docker container to a network. You can use the container name or container ID.

usage:

docker network connect [OPTIONS] NETWORK CONTAINER

Connect the tomcat01 container to the mynet network:

docker network connect mynet tomcat01

View the network information of mynet: docker inspect mynet

insert image description here

tomcat01 is already connected to the mynet network, as are tomcat01-net-tomcat01 and tomcat01-net-tomcat02.

At this point, tomcat01 has been connected to the two containers in the mynet network. Since the three containers are in the same network, they can of course communicate with each other:

insert image description here

One container, two networks:

View the metadata of tomcat01: docker inspect tomcat01

insert image description here

After tomcat01 is connected to the custom mynet network, there are two networks.

This is the end of this article about custom network implementation in Docker. For more relevant Docker network content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker network mode and configuration method
  • Docker compose custom network to achieve fixed container IP address
  • Docker Compose network settings explained

<<:  MySQL index principle and query optimization detailed explanation

>>:  CSS3 realizes the effect of triangle continuous enlargement

Recommend

Detailed description of component-based front-end development process

Background <br />Students who work on the fr...

Detailed code for adding electron to the vue project

1. Add in package.json "main": "el...

Toolkit: A more powerful front-end framework than Bootstrap

Note: Currently, the more popular front-end frame...

Detailed tutorial for installing MySQL on Linux

MySQL downloads for all platforms are available a...

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

JS realizes picture digital clock

This article example shares the specific code of ...

Use vue2+elementui for hover prompts

Vue2+elementui's hover prompts are divided in...

VUE + OPENLAYERS achieves real-time positioning function

Table of contents Preface 1. Define label style 2...

Detailed steps to modify MySQL stored procedures

Preface In actual development, business requireme...

CSS method of controlling element height from bottom to top and from top to bottom

Let’s start the discussion from a common question...

Implementation of Docker container connection and communication

Port mapping is not the only way to connect Docke...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

How to quickly create tens of millions of test data in MySQL

Remark: The amount of data in this article is 1 m...

Simple use of Vue bus

Simple use of Vue bus Scenario description: Compo...

Implementation example of Docker rocketmq deployment

Table of contents Preparation Deployment process ...