Preliminary practice on Docker Container Interconnection
- 2020-12-16 06:12:02
- OfStack
1. Interconnection between Docker containers
Docker is now a lightweight virtualization solution where all containers can be connected over a bridge under the same host. If you have previous experience with docker, you may already be used to using link to interconnect containers. As docker evolves, it is highly recommended that you use a bridge (bridge) to interconnect containers.
2. Practice process
1. Create a network my-ES16en:
[root@ChatDevOps ~]# docker network create my-net
71b42506de62797889372ea4a5270f905f79a19cf80e308119c02e529b89c94e
[root@ChatDevOps ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
3dec5cbb852e bridge bridge local
6dd6dcfc2f26 host host local
71b42506de62 my-net bridge local
4c142a02cd6b none null local
2. Specify the bridge network when you create the docker container:
[root@ChatDevOps docker]# docker create -it --name d1 --network my-net -p 8080:80 ubuntu:14.04
4776b65db566f370cad5da3a9354a12c7e4f9badab53647b7e30e1e8f343ae3d
[root@ChatDevOps docker]# docker start d1
d1
In this command, docker create can also be used as docker container create, with 2 equivalent. name specifies the name of the container, network specifies the network name of the container, the bridge form defaults to a bridge, and -ES31en or publish specifies the mapped port. If the network specified in this step is not created in advance, the container cannot be started properly. At this point, you can create a network for the container and start the container again.
3. You can also specify 1 network that has been created when running 1 docker container:
[root@ChatDevOps docker]# docker run -it --name d2 --network my-net --publish 8081:80 ubuntu:14.04 /bin/bash
root@07fd516911d0:/# ping d1
PING d1 (172.18.0.2) 56(84) bytes of data.
64 bytes from d1.my-net (172.18.0.2): icmp_seq=1 ttl=64 time=0.115 ms
root@4776b65db566:/# ping d2
PING d2 (172.18.0.3) 56(84) bytes of data.
64 bytes from d2.my-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.062 ms
ping is accessible by the container name on the same 1 bridge. You can also direct ping to its IP.
3. Summary
1. After the docker installation is completed, the docker container has three networks, as follows:
[root@ChatDevOps ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
3dec5cbb852e bridge bridge local
6dd6dcfc2f26 host host local
4c142a02cd6b none null local
2. All container networks in the same 1 network are interoperable.
3. The network configuration of the container of dns configurations can be on the host/etc/docker/daemon json file configured, follow the official format:
{
"bip": "192.168.1.5/24",
"fixed-cidr": "192.168.1.5/25",
"fixed-cidr-v6": "2001:db8::/64",
"mtu": 1500,
"default-gateway": "10.20.1.1",
"default-gateway-v6": "2001:db8:abcd::89",
"dns": ["10.20.1.2","10.20.1.3"]
}
It can be configured according to the actual situation.