docker 容器間的鏈接

  • 啓動一個tomcat容器,手動爲容器指定名字爲tomcat1

# docker run -d --name tomcat1 tomcatnode

7abc103ffd644a790f4445aa8b113eee372e09653b69dcaa14764b599febbab9

# docker psdocker

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7abc103ffd64        tomcat              "catalina.sh run"   4 seconds ago       Up 4 seconds        8080/tcp            tomcat1
  • 啓動一個容器去鏈接tomcat1

-i:使用交互模式centos

-t:分配一個僞終端tomcat

--link=tomcat1:server1:tomcat1爲鏈接到的容器名,server1爲該容器在hosts文件中的主機名,多個鏈接能夠指定多個--linkbash

# docker run -i -t --link=tomcat1:server1 centos /bin/bashcurl

[root@412394d09c73 /]# cat /etc/hosts
172.17.0.24     412394d09c73
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.23     server1 7abc103ffd64 tomcat1

[root@412394d09c73 /]# curl http://tomcat1:8080/ -o /dev/null -w %{http_code} 2>/dev/null
200
相關文章
相關標籤/搜索