赞
踩
自定义docker0的桥的网络属性信息需要修改/etc/docker/daemon.jsom配置文件
[root@cxr ~]# vi /etc/docker/daemon.json { "registry-mirrors": ["https://hzlytjqu.mirror.aliyuncs.com"], //这个是配置的加速器不需要写 "bip":"192.168.1.1/24" //想要修改的网关 } [root@cxr ~]# systemctl daemon-reload [root@cxr ~]# systemctl restart docker [root@cxr ~]# docker run -itd --name web busybox //创建一个容器 4b0a393beb69cfba0a07183d55879df05902c4c9fe5247572029370e3b7c23dc [root@cxr ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4b0a393beb69 busybox "sh" 14 seconds ago Up 13 seconds web [root@cxr ~]# docker inspect web | grep IPAddress //查看IP是否已经更改 "SecondaryIPAddresses": null, "IPAddress": "192.168.1.2", "IPAddress": "192.168.1.2",
[root@cxr ~]# docker network ls //原本只有三个网桥
NETWORK ID NAME DRIVER SCOPE
ecbfa2f87150 bridge bridge local
0482f6be6b37 host host local
63b6dd5ecb39 none null local
[root@cxr ~]# docker network create -d bridge --subnet "192.168.2.0/24" --gateway "192.168.2.1" cxr1 //自定义一个网桥
[root@cxr ~]# docker network ls //可以看到刚才创建的网桥
NETWORK ID NAME DRIVER SCOPE
ecbfa2f87150 bridge bridge local
515eda81c941 cxr1 bridge local
0482f6be6b37 host host local
63b6dd5ecb39 none null local
[root@cxr ~]# docker run -it --name cxr --network cxr1 busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:02:02
inet addr:192.168.2.2 Bcast:192.168.2.255 Mask:255.255.255.0 //这里的IP已经发生改变
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:36 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5124 (5.0 KiB) TX bytes:0 (0.0 B)
[root@cxr ~]# docker run -it --name cxr1 busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:01:03
inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2004 (1.9 KiB) TX bytes:0 (0.0 B)
[root@cxr ~]# docker network connect cxr1 c2 //c2这个容器连接到cxr1这个网络 / # ifconfig //查看c2的网卡状态发现多了一个192.168.2.3的ip eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02 inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:26 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2826 (2.7 KiB) TX bytes:532 (532.0 B) eth1 Link encap:Ethernet HWaddr 02:42:C0:A8:02:03 inet addr:192.168.2.3 Bcast:192.168.2.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:28 errors:0 dropped:0 overruns:0 frame:0 TX packets:5 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2942 (2.8 KiB) TX bytes:378 (378.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:6 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:504 (504.0 B) TX bytes:504 (504.0 B) / # ping 192.168.2.3 //进入到c2这个容器来ping c1 可以看到已经ping通了 PING 192.168.2.3 (192.168.2.3): 56 data bytes 64 bytes from 192.168.2.3: seq=0 ttl=64 time=0.209 ms 64 bytes from 192.168.2.3: seq=1 ttl=64 time=0.070 ms 64 bytes from 192.168.2.3: seq=2 ttl=64 time=0.075 ms //在进到c1里面去ping c2 / # ping 192.168.2.3 //也可以通信了 PING 192.168.2.3 (192.168.2.3): 56 data bytes 64 bytes from 192.168.2.3: seq=0 ttl=64 time=0.257 ms 64 bytes from 192.168.2.3: seq=1 ttl=64 time=0.135 ms 64 bytes from 192.168.2.3: seq=2 ttl=64 time=0.126 ms
[root@cxr ~]# docker network disconnect cxr1 c2 //进入到c2查看网卡状态 / # ifconfig //可以看到已经没有192.168.2.3这个IP了 eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02 inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:26 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2826 (2.7 KiB) TX bytes:532 (532.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:6 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:504 (504.0 B) TX bytes:504 (504.0 B)
存储卷的分类
Docker有两种类型的卷,每种类型都在容器中存在一个挂载点,但其在宿主机上的位置有所不同:
Bind mount volume
a volume that points to a user-specified location on the host file system
Docker-managed volume
the Docker daemon creates managed volumes in a portion of the host’s file system that’s owned bye Docker
容器中管理数据主要有两种方式:
数据卷(Data Volumes)
数据卷容器(Data Volumes Containers)
Docker-managed volume
[root@cxr ~]# docker run -it --name b1 -v /data busybox /bin/sh / # ls bin data dev etc home proc root sys tmp usr var / # [root@cxr ~]# docker inspect b1 //查看b1的数据存储的位置 "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/4b18b15683bd89181ca89e0cd4745c38cccda997020ceee02ec95f37af52fca5-init/diff:/var/lib/docker/overlay2/b65ed408da5ee16c9432d490934e947c18d98197b991db35b0d72901e4591e92/diff", "MergedDir": "/var/lib/docker/overlay2/4b18b15683bd89181ca89e0cd4745c38cccda997020ceee02ec95f37af52fca5/merged", "UpperDir": "/var/lib/docker/overlay2/4b18b15683bd89181ca89e0cd4745c38cccda997020ceee02ec95f37af52fca5/diff", "WorkDir": "/var/lib/docker/overlay2/4b18b15683bd89181ca89e0cd4745c38cccda997020ceee02ec95f37af52fca5/work" }, //在虚拟机上容器data存储的位置 [root@cxr diff]# cd /var/lib/docker/volumes/a7d16fe6c1cd102558b38945f895290ffe5c10c9c7eb99b0f71f3cce9cd91309/_data [root@cxr _data]# touch 123 //在data中创建一个文件123 [root@cxr _data]# ls 123 /data # ls /data/ //在容器中查看是否有123这个文件 123 [root@cxr _data]# rm -f 123 //在虚拟机中删除123文件 /data # ls /data/ //在容器中查看也被删除了
Bind mount volume
[root@cxr _data]# docker run -it --name b2 -v /web:/data busybox //创建一个容器并把data目录映射到虚拟机上的web
[root@cxr ~]# ls / //在虚拟机上也可以看到web这个目录
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr web
[root@cxr ~]# cd /web/
[root@cxr web]# echo "hello java" > index.html //在这个目录随便写个东西
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # cd /data/
/data # ls
index.html
/data # cat index.html //可以看到在容器中也是可以看到的
hello java
[root@cxr ~]# unzip 漂亮的个人主页HTML源码.zip [root@cxr ~]# mv 漂亮的个人主页HTML源码 gerenzhuye [root@cxr ~]# rm -rf 漂亮的个人主页HTML源码.zip [root@cxr ~]# cp -r gerenzhuye/ /web/ [root@cxr ~]# ls /web/ gerenzhuye index.html [root@cxr ~]# cd /web/ [root@cxr web]# ll 总用量 4 drwxr-xr-x. 6 root root 137 5月 2 03:17 gerenzhuye -rw-r--r--. 1 root root 11 5月 2 02:27 index.html [root@cxr web]# rm -f index.html [root@cxr web]# mv gerenzhuye/ index.html [root@cxr web]# ll 总用量 0 drwxr-xr-x. 6 root root 137 5月 2 03:17 index.html //然后做一个apache的容器 [root@cxr _data]# docker run -d --name web -v /web:/usr/local/apache2/htdocs -p 80:80 httpd //把web目录和容器中的/usr/local/apache2/htdocs做一个绑定,把容器里的80端口映射到虚拟机上 Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd 1fe172e4850f: Pull complete e2fa1fe9b1ec: Pull complete 60dd7398e74e: Pull complete ea2ca81c6d4c: Pull complete f646c69a26ec: Pull complete Digest: sha256:e02a2ef36151905c790efb0a8472f690010150f062639bd8c0760e7b1e884c07 Status: Downloaded newer image for httpd:latest c71462d8ed565f03315e7cb3c50adf1fb5de81bfec6c6bfc514f68916609cf36 [root@cxr _data]# cd [root@cxr ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c71462d8ed56 httpd "httpd-foreground" 17 seconds ago Up 15 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。