1 Docker容器使用LINK方式互通
在前面的网络实验中,当同一主机的两个Docker容器,使用Bridge网桥互通时,我们必须使用docker inspect Dockerid查找出Dockers容器的IP,然后才能通信,现在可以使用LINK这种方式,让两个容器进行通信
首先创建一个容器
[root@docker-server3 ~]# docker run -d openssh:v1.7
a8ada684e599da9266e91e5fb9ca771403e345be16c27ac1de238471e21fc8d4
[root@docker-server3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8ada684e599 openssh:v1.7 "/bin/sh -c '/usr/sb…" 3 seconds ago Up 2 seconds 22/tcp, 80/tcp confident_hypatia
只用LINK方式在创建一个容器
[root@docker-server3 ~]# docker run -d --link confident_hypatia:openssh nginx:v1.5
confident_hypatia:openssh这个意思时容器名:别名,方便识别
e4940929960c33d5fb093eef55253219c45895f0b77795e7cf22ba3f9b2edcba
[root@docker-server3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e4940929960c nginx:v1.5 "/build.sh nginx -g …" 12 seconds ago Up 11 seconds 80/tcp zealous_villani a8ada684e599 openssh:v1.7 "/bin/sh -c '/usr/sb…" 3 minutes ago Up 3 minutes 22/tcp, 80/tcp confident_hypatia
[root@docker-server3 ~]# docker exec -it e4940929960c /bin/bash
[root@e4940929960c html]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 192.168.0.2 openssh a8ada684e599 confident_hypatia 192.168.0.3 e4940929960c
可以看到,使用LINK的方式,就是把容器名,容器ID,别名写进hosts,这样就可以直接使用主机名或者dokerid进行通信
[root@e4940929960c html]# ping openssh
[root@e4940929960c html]# ping confident_hypatia
[root@e4940929960c html]# ping a8ada684e599
PING openssh (192.168.0.2) 56(84) bytes of data. 64 bytes from openssh (192.168.0.2): icmp_seq=1 ttl=64 time=0.135 ms 64 bytes from openssh (192.168.0.2): icmp_seq=2 ttl=64 time=0.124 ms
但是LINK不能跨主机
2 Dockers创建容器时,可以指定IP,但是必须要使用自定义的网络,才能指定IP
例如:
[root@docker-server3 ~]# docker run -d --net bridge --ip 192.168.0.20 nginx:v1.5
10694bcf9b8709a5161d71f0baef42ad5f555234b3eced853a2eae0167f4d633
docker: Error response from daemon: user specified IP address is supported on user defined networks only.
[root@docker-server3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10694bcf9b87 nginx:v1.5 "/build.sh nginx -g …" 8 seconds ago Created practical_ritchie
创建一个自定义网络
[root@docker-server3 ~]# docker network create --driver bridge --subnet 172.22.16.0/24 --gateway 172.22.16.1 my_net3
[root@docker-server3 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE 3b13bd244134 bridge bridge local e3d5265467e4 host host local 8f5945aac199 my_net3 bridge local eb34937c52b0 none null local
[root@docker-server3 ~]# docker run -d -it --net my_net3 --ip 172.22.16.10 nginx:v1.5
[root@docker-server3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f97a5669c5d6 nginx:v1.5 "/build.sh nginx -g …" 6 seconds ago Up 5 seconds 80/tcp beautiful_wilbur 10694bcf9b87 nginx:v1.5 "/build.sh nginx -g …" 2 minutes ago Created practical_ritchie
[root@docker-server3 ~]# docker inspect f97a5669c5d6|grep IP
"LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "IPAMConfig": { "IPv4Address": "172.22.16.10" "IPAddress": "172.22.16.10", "IPPrefixLen": 24, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0,
博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!