一: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)