Docker 容器互连 --link

一、启动两个容器测试

1.创建容器:

# 启动第一个容器
[root@yang ~]# docker run -it -d --name tomcat1 tomcat
# 启动第二个容器
[root@yang ~]# docker run -it -d --name tomcat2 tomcat
[root@yang ~]# docker ps -a
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                               NAMES
fcdd4c02da9c        tomcat                                "catalina.sh run"        2 hours ago         Up 2 hours          8080/tcp                            tomcat2
346962a1d73b        tomcat                                "catalina.sh run"        5 hours ago         Up 5 hours          8080/tcp                            tomcat1

2.测试两个容器间连通性

[root@yang ~]# docker exec -it tomcat1 ping tomcat2
ping: tomcat2: No address associated with hostname

以上显示两个容器之前ping不通!

思考:怎么做才能让两个容器之间ping通呢?

二、具体演示

1.创建容器:

# 启动第一个容器
[root@yang ~]# docker run -it -d --name tomcat3 --link tomcat2  tomcat

--link       # 在hosts配置中,指定某容器的映射

2.测试

[root@yang ~]# docker exec -it tomcat3 ping tomcat2
PING tomcat2 (172.17.0.9) 56(84) bytes of data.
64 bytes from tomcat2 (172.17.0.9): icmp_seq=1 ttl=64 time=0.247 ms
64 bytes from tomcat2 (172.17.0.9): icmp_seq=2 ttl=64 time=0.089 ms
64 bytes from tomcat2 (172.17.0.9): icmp_seq=3 ttl=64 time=0.105 ms

查看tomcat3中配置:

[root@yang ~]# docker inspect tomcat3
[
    {
        "Id": "ee6a1fd2ba6708fe79f3708489e44e8aa00f9404640c585ae1d2ea5bd2395cdf",
        "Created": "2021-01-29T09:06:20.640610921Z",
        "Path": "catalina.sh",
        "Args": [
            "run"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 7964,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-29T09:06:21.625013975Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:345867df0879186da623555494dcf5f4561e5bf0b8d2e4276a71947f30bdca0d",
        "ResolvConfPath": "/var/lib/docker/containers/ee6a1fd2ba6708fe79f3708489e44e8aa00f9404640c585ae1d2ea5bd2395cdf/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ee6a1fd2ba6708fe79f3708489e44e8aa00f9404640c585ae1d2ea5bd2395cdf/hostname",
        "HostsPath": "/var/lib/docker/containers/ee6a1fd2ba6708fe79f3708489e44e8aa00f9404640c585ae1d2ea5bd2395cdf/hosts",
        "LogPath": "/var/lib/docker/containers/ee6a1fd2ba6708fe79f3708489e44e8aa00f9404640c585ae1d2ea5bd2395cdf/ee6a1fd2ba6708fe79f3708489e44e8aa00f9404640c585ae1d2ea5bd2395cdf-json.log",
        "Name": "/tomcat3",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": [
                "/tomcat2:/tomcat3/tomcat2"
            ],

以上links位置显示,tomcat3绑定了tomcat2!

3.通过进入容器查看:

# 查看 hosts 配置,发现已经绑定!
[root@yang ~]# docker exec -it tomcat3 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 172.17.0.9 tomcat2 fcdd4c02da9c 172.17.0.10 ee6a1fd2ba67

其实tomcat3在本地就已经配置了tomcat2的配置了。

结论:

1.两个容器间互相通信使用--link,可以实现互通。

2.docker0  不支持容器名连接访问。

建议:

docker官方已不推荐使用docker run --link来链接2个容器互相通信,随后的版本中会删除--link,但了解其原理,对如何使2个容器之间互相通信还是有帮助。

 

posted @ 2021-01-29 17:57  西瓜君~  阅读(1067)  评论(0编辑  收藏  举报