容器别名
root@virtual-machine:/etc/docker# docker run -it -d --name myos1 ubuntu:net /bin/bash
de03145b828e19e73a727f2321a40d09c1a42fcad07327ae989cbe443f96c47c
root@virtual-machine:/etc/docker#
root@virtual-machine:/etc/docker# docker run -it -d --name myos2 --link=myos1:os1 ubuntu:net /bin/bash
b1aef9bc5f7f809e88bb9541c16423555cec4a573e2e4471909c0c5b612f194d
root@virtual-machine:/etc/docker# docker exec -it myos2 /bin/bash
root@b1aef9bc5f7f:/# ping os1
PING os1 (172.17.0.2) 56(84) bytes of data.
64 bytes from os1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.171 ms
64 bytes from os1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.155 ms
64 bytes from os1 (172.17.0.2): icmp_seq=3 ttl=64 time=0.129 ms
^C
--- os1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2018ms
rtt min/avg/max/mdev = 0.129/0.151/0.171/0.022 ms
root@b1aef9bc5f7f:/# cat /etc/hosts
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.2 os1 de03145b828e myos1
172.17.0.3 b1aef9bc5f7
root@b1aef9bc5f7f:/# exit
exit
root@virtual-machine:/etc/docker#
阻断容器互连
root@virtual-machine:~# cat /etc/default/docker
DOCKER_OPTS="--icc=false"
root@virtual-machine:~# ps aux | grep docker
root 10159 2.4 8.2 550592 81384 ? Ssl 11:28 0:00 /usr/bin/dockerd -H fd:// --icc=false
root 10288 0.0 0.0 21572 920 pts/8 S+ 11:28 0:00 grep --color=auto docker
root@virtual-machine:~# docker rm `docker ps -aq`
9ea9dc72538a
7ba1a068cd9d
root@virtual-machine:~# docker run -it -d --name myos1 ubuntu:net /bin/bash
c2ef906327dcbb0e8ad4178d87d66383c81422823bd4ab61cc0e612cec687069
root@virtual-machine:~# docker run -it -d --name myos2 ubuntu:net /bin/bash
8ebe1df90b9e6f4181a08caeba8c8d0e14563faf0369711b88d82206551a7201
root@virtual-machine:~# docker exec -it myos1 /bin/bash
root@c2ef906327dc:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 23 bytes 2901 (2.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@c2ef906327dc:/# exit
exit
root@virtual-machine:~# docker exec -it myos2 /bin/bash
root@8ebe1df90b9e:/# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
^C
--- 172.17.0.2 ping statistics ---
7 packets transmitted, 0 received, 100% packet loss, time 6130ms
root@8ebe1df90b9e:/#
指定特定的容器互连
root@virtual-machine:~# ps aux | grep docker
root 11505 0.8 8.2 419264 81444 ? Ssl 11:46 0:00 /usr/bin/dockerd -H fd:// --icc=false --iptables=true
root 11659 0.0 0.0 21572 936 pts/8 S+ 11:47 0:00 grep --color=auto docker
root@virtual-machine:~# docker run -it -d --name myos1 ubuntu:nginx /bin/bash
cdce08ced79ad905afa41d36fe2b852002f652a24e1e7ea93ed3d5158a012464
root@virtual-machine:~# docker run -it -d --name myos2 ubuntu:nginx /bin/bash
c9fbaaba148c217c972bb53fcb43785b83596dd3d25da353d7fbf9bdac193d30
root@virtual-machine:~# docker exec -it myos1 /bin/bash
root@cdce08ced79a:/# nginx
root@cdce08ced79a:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 66 bytes 6055 (6.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22 bytes 4776 (4.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@cdce08ced79a:/# exit
exit
root@virtual-machine:~# docker exec -it myos2 /bin/bash
root@c9fbaaba148c:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 36 bytes 3776 (3.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5 bytes 274 (274.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@c9fbaaba148c:/# exit
root@virtual-machine:~# docker run -it -d --name myos4 --link=myos1:os1 ubuntu:nginx /bin/bash
1fe744247f97332dcc9d812345f04df32faaff48be2b6cce048092c05ab60a35
root@virtual-machine:~# docker exec -it myos4 /bin/bash
root@1fe744247f97:/# curl os1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
root@1fe744247f97:/# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@1fe744247f97:/# curl 172.17.0.3
^C
root@virtual-machine:~# iptables -S
-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
-N DOCKER
-N DOCKER-ISOLATION-STAGE-1
-N DOCKER-ISOLATION-STAGE-2
-N DOCKER-USER
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j DROP
-A DOCKER -s 172.17.0.5/32 -d 172.17.0.2/32 -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER -s 172.17.0.2/32 -d 172.17.0.5/32 -i docker0 -o docker0 -p tcp -m tcp --sport 80 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN
root@virtual-machine:~# cat /etc/docker/dockerfile
FROM ubuntu
MAINTAINER haojie
#RUN 执行以下命令
RUN apt-get update
RUN apt-get install iputils-ping -y
RUN apt-get install nginx -y
RUN apt-get install curl -y
RUN apt-get install net-tools -y
EXPOSE 80