实操:docker端口映射,容器之间link互联

一:docker端口映射

-P 随机指定端口映射

[root@gsy test1]# docker run -d -P 192.168.247.134:5000/nginx
4858b6709f9f40713e8dd6e3807faa3cdc78c301bb083b855fa0b708073c978a
[root@gsy test1]# docker ps -a | grep nginx
4858b6709f9f        192.168.247.134:5000/nginx   "nginx -g 'daemon of…"   14 seconds ago      Up 13 seconds               0.0.0.0:32768->80/tcp    infallible_chatelet

-p 指定端口映射

[root@gsy test1]# docker run -d -p 49280:80 192.168.247.134:5000/nginx
daac6e6993c46a11689499f1e355256bd61d9b50d1fc1cd846f386f64e9ec7ce
[root@gsy test1]# docker ps -a | grep nginx
daac6e6993c4        192.168.247.134:5000/nginx   "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds                0.0.0.0:49280->80/tcp    objective_chaum
4858b6709f9f        192.168.247.134:5000/nginx   "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes                0.0.0.0:32768->80/tcp    infallible_chatelet
[root@gsy test1]# 

二:容器互联(使用centos镜像)

创建并运行容器,命名为web1,端口号自动映射

[root@gsy test1]# docker run -itd -P --name web1 centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
45b54dc1634d4dd02bff5a908d55cf54295442057c91dd39772411236814e93c
[root@gsy test1]# docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                         PORTS                    NAMES
45b54dc1634d        centos                       "/bin/bash"              10 seconds ago      Up 8 seconds                                            web1
daac6e6993c4        192.168.247.134:5000/nginx   "nginx -g 'daemon of…"   35 minutes ago      Up 35 minutes                  0.0.0.0:49280->80/tcp    objective_chaum
4858b6709f9f        192.168.247.134:5000/nginx   "nginx -g 'daemon of…"   43 minutes ago      Up 43 minutes                  0.0.0.0:32768->80/tcp    infallible_chatelet
fe4968194091        centos:7                     "/bin/bash"              59 minutes ago      Exited (0) 57 minutes ago                               db1
8dcf772df2ac        centos:7                     "/bin/bash"              About an hour ago   Up 53 minutes                                           test2
4e5fdf54c872        centos:7                     "/bin/bash"              About an hour ago   Exited (0) About an hour ago                            test1
72c7470e210c        registry                     "/entrypoint.sh /etc…"   2 hours ago         Up 2 hours      

新创建并运行容器web2,链接到web1和其通信

–link 添加链接到另一个容器

[root@gsy test1]# docker run -itd -P --name web2 --link web1:web1 centos /bin/bash
d1b8b567a609186a3d124defa493c0aa9959e1983166fc81f7b71614002cf24f
[root@gsy test1]# docker ps -a | grep web2
d1b8b567a609        centos                       "/bin/bash"              14 seconds ago      Up 13 seconds                                           web2

进入web2容器测试验证,首先先进入web1查看自己的IP地址

[root@gsy ~]# docker exec -it web1 /bin/bash
[root@45b54dc1634d /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
57: eth0@if58: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.6/16 brd 172.17.255.255 scope global eth0

进入web2

[root@gsy test1]# docker exec -it web2 /bin/bash
[root@d1b8b567a609 /]# ping 172.17.0.6
PING 172.17.0.6 (172.17.0.6) 56(84) bytes of data.
64 bytes from 172.17.0.6: icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from 172.17.0.6: icmp_seq=2 ttl=64 time=0.120 ms
64 bytes from 172.17.0.6: icmp_seq=3 ttl=64 time=0.121 ms
64 bytes from 172.17.0.6: icmp_seq=4 ttl=64 time=0.109 ms
^C
--- 172.17.0.6 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.091/0.110/0.121/0.014 ms
[root@d1b8b567a609 /]# ping web1
PING web1 (172.17.0.6) 56(84) bytes of data.
64 bytes from web1 (172.17.0.6): icmp_seq=1 ttl=64 time=0.138 ms
64 bytes from web1 (172.17.0.6): icmp_seq=2 ttl=64 time=0.130 ms
64 bytes from web1 (172.17.0.6): icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from web1 (172.17.0.6): icmp_seq=4 ttl=64 time=0.098 ms
^C
--- web1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 5ms
rtt min/avg/max/mdev = 0.052/0.104/0.138/0.035 ms

三 :全部stop容器

docker stop $(docker ps -aq)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值