如何访问docker内php,docker中容器之间如何访问
我們都知道docker容器之間是互相隔離的,不能互相訪問,但如果有些依賴關系的服務要怎么辦呢。下面介紹三種方法解決容器互訪問題。
方式一、虛擬ip訪問
安裝docker時,docker會默認創建一個內部的橋接網絡docker0,每創建一個容器分配一個虛擬網卡,容器之間可以根據ip互相訪問。[root@33fcf82ab4dd /]# [root@CentOS ~]# ifconfig
......
docker0: flags=4163 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:35ff:feac:66d8 prefixlen 64 scopeid 0x20
ether 02:42:35:ac:66:d8 txqueuelen 0 (Ethernet)
RX packets 4018 bytes 266467 (260.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4226 bytes 33935667 (32.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......
方式二、link
運行容器的時候加上參數link
運行第一個容器docker run -it --name centos-1 docker.io/centos:latest
運行第二個容器[root@CentOS ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest
--link:參數中第一個centos-1是容器名,第二個centos-1是定義的容器別名(使用別名訪問容器),為了方便使用,一般別名默認容器名。
測試結果如下:[root@e0841aa13c5b /]# ping centos-1
PING centos-1 (172.17.0.7) 56(84) bytes of data.
bytes from centos-1 (172.17.0.7): icmp_seq=1 ttl=64 time=0.210 ms
bytes from centos-1 (172.17.0.7): icmp_seq=2 ttl=64 time=0.116 ms
bytes from centos-1 (172.17.0.7): icmp_seq=3 ttl=64 time=0.112 ms
bytes from centos-1 (172.17.0.7): icmp_seq=4 ttl=64 time=0.114 ms
方式三、創建bridge網絡
1.安裝好docker后,運行如下命令創建bridge網絡:docker network create testnet
查詢到新創建的bridge testnet。
2.運行容器連接到testnet網絡。
使用方法:docker run -it --name ---network --network-alias [root@CentOS ~]# docker run -it --name centos-1 --network testnet --network-alias centos-1 docker.io/centos:latest
[root@CentOS ~]# docker run -it --name centos-2 --network testnet --network-alias centos-2 docker.io/centos:latest
3.從一個容器ping另外一個容器,測試結果如下:[root@fafe2622f2af /]# ping centos-1
PING centos-1 (172.20.0.2) 56(84) bytes of data.
bytes from centos-1.testnet (172.20.0.2): icmp_seq=1 ttl=64 time=0.158 ms
bytes from centos-1.testnet (172.20.0.2): icmp_seq=2 ttl=64 time=0.108 ms
bytes from centos-1.testnet (172.20.0.2): icmp_seq=3 ttl=64 time=0.112 ms
bytes from centos-1.testnet (172.20.0.2): icmp_seq=4 ttl=64 time=0.113 ms
更多相關教程,請關注PHP中文網docker教程欄目。
總結
以上是生活随笔為你收集整理的如何访问docker内php,docker中容器之间如何访问的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 送子天王图图作者是谁啊?
- 下一篇: 少弱精子症的治疗