docker不同容器如何访问


本文摘自PHP中文网,作者尚,侵删。

docker中不同容器间的访问方法:

虚拟ip访问

安装docker时,docker会默认创建一个内部的桥接网络docker0,每创建一个容器分配一个虚拟网卡,容器之间可以根据ip互相访问。

1

2

3

4

5

6

7

8

9

10

11

[root@33fcf82ab4dd /]# [root@CentOS ~]# ifconfig

......

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>

        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

......

运行一个centos镜像, 查看ip地址得到:172.17.0.7

1

2

3

4

5

6

7

8

9

10

[root@CentOS ~]# docker run -it --name centos-1 docker.io/centos:latest

[root@6d214ff8d70a /]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.17.0.7  netmask 255.255.0.0  broadcast 0.0.0.0

        inet6 fe80::42:acff:fe11:7  prefixlen 64  scopeid 0x20<link>

        ether 02:42:ac:11:00:07  txqueuelen 0  (Ethernet)

        RX packets 16  bytes 1296 (1.2 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 8  bytes 648 (648.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

以同样的命令再起一个容器,查看ip地址得到:172.17.0.8

1

2

3

4

5

6

7

8

9

10

[root@CentOS ~]# docker run -it --name centos-2 docker.io/centos:latest

[root@33fcf82ab4dd /]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.17.0.8  netmask 255.255.0.0  broadcast 0.0.0.0

        inet6 fe80::42:acff:fe11:8  prefixlen 64  scopeid 0x20<link>

        ether 02:42:ac:11:00:08  txqueuelen 0  (Ethernet)

        RX packets 8  bytes 648 (648.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 8  bytes 648 (648.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

容器内部ping测试结果如下:

1

2

3

4

5

6

[root@33fcf82ab4dd /]# ping 172.17.0.7

PING 172.17.0.7 (172.17.0.7) 56(84) bytes of data.

bytes from 172.17.0.7: icmp_seq=1 ttl=64 time=0.205 ms

bytes from 172.17.0.7: icmp_seq=2 ttl=64 time=0.119 ms

bytes from 172.17.0.7: icmp_seq=3 ttl=64 time=0.118 ms

bytes from 172.17.0.7: icmp_seq=4 ttl=64 time=0.101 ms

创建bridge网络

1、安装好docker后,运行如下命令创建bridge网络:docker network create testnet

查询到新创建的bridge testnet。

1.jpg

2、运行容器连接到testnet网络。

使用方法:docker run -it --name <容器名> ---network <bridge> --network-alias <网络别名> <镜像名>

1

2

[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另外一个容器,测试结果如下:

1

2

3

4

5

6

[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不同容器如何访问的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

Docker是什么?有什么作用?

Docker启动nginx后无法访问怎么办

如何把Docker镜像保存到本地压缩文件

tomcat和Docker之间有什么区别?

Docker怎么查看文件

Docker 可以用在哪些方面?

Docker 和 mysql 区别?

无法上网如何装Docker

怎么样在ubuntu中安装Docker

Docker容器日志在哪个目录

更多相关阅读请进入《Docker》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...