Docker提供三種不一樣方式將數據從宿主機掛載到容器中:volumes,bind mounts和tmpfsphp
volumes:Docker管理宿主機文件系統的一部分(/var/lib/docker/volumes)html
bind mounts:能夠存儲在宿主機系統的任意位置mysql
tmpfs:掛載存儲在宿主機系統的內存中,而不會寫入宿主機的文件系統nginx
#建立一個卷 docker volume create nginx-vol nginx-vol docker volume ls DRIVER VOLUME NAME local nginx-vol docker volume inspect nginx-vol [ { "CreatedAt": "2018-09-04T21:33:33-04:00", #建立時間 "Driver": "local", #驅動 "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", #掛載點 "Name": "nginx-vol", #卷名 "Options": {}, "Scope": "local" } ]
#下載一個nginx官方鏡像 docker pull nginx Using default tag: latest latest: Pulling from library/nginx be8881be8156: Pull complete 65206e5c5e2d: Pull complete 8e029c3e2376: Pull complete Digest: sha256:1b109555ad28bb5ec429422ee136c5f5ab5ee6faaeb518836a5c9a3b6436a1bd Status: Downloaded newer image for nginx:latest docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 71c43202b8ac 6 days ago 109MB centos 7 5182e96772bf 4 weeks ago 200MB docker run -dit --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx --name:容器的名字 --mount:掛載 src原卷的名字 dst 掛在到容器中的路徑 docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3d86928ac689 nginx "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 80/tcp nginx-test 02e7612375e7 518 "/bin/bash" 22 minutes ago Up 22 minutes distracted_vil
#向容器中的掛載目錄建立文件,查看是否掛載成功 docker exec nginx-test touch /usr/share/nginx/html/wk.html #向容器建立文件wk.html docker exec nginx-test ls /usr/share/nginx/html/ #查看文件是否建立成功 50x.html index.html wk.html ls /var/lib/docker/volumes/nginx-vol/_data/ #查看本地卷 50x.html index.html wk.html #若是把容器刪除,本地的volumes的數據仍然存在,不清空
docker run -dit --name nginx-test3 -p 90:80 --mount type=bind,src=/var/lib/docker/volumes/nginx-vol/_data,dst=/usr/share/nginx/html nginx
#指定類型,將本地本身建立的目錄掛載上去
首先下載一個wordpress博客git
wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
docker pull mysql:5.6 docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.6 1f47fade220d 2 hours ago 256MB nginx latest 71c43202b8ac 6 days ago 109MB centos 7 5182e96772bf 4 weeks ago 200MB #建立一個自定義網絡 docker network create lnmp b59ca0e58388b0857e79781de00dc8d81fba77d9d9969afd06496f087c86f7ea docker network ls NETWORK ID NAME DRIVER SCOPE f136adf5eb39 bridge bridge local b68e9da0b1e6 host host local b59ca0e58388 lnmp bridge local cf4b2d0b4394 none null local #啓動MySQL數據庫容器 docker run -dit --name lnmp_mysql --network lnmp -p 3306:3306 --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6 --character-set-server=utf8 71e98322f4c2c12772d36a841b5951e8d01c77205f6d4a13d510b3a1e24cc375 自定義網絡lnmp若是不提早建立的話,在啓動容器進程時會報錯 若是沒有提早pull好mysql:5.6那麼容器在啓動時會自動下載對應鏡像 若是沒有提早docker volume create mysql-vol,那麼容器啓動時會自動建立
#查看容器lnmp_mysql的日誌輸出
docker logs lnmp_mysql
#查看容器裏啓動的進程
docker top lnmp_mysql
docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database wp"' Warning: Using a password on the command line interface can be insecure.
#安裝mysql客戶端 yum -y install mysql #遠程方式鏈接docker宿主機的3306端口 mysql -h192.168.200.142 -P3306 -uroot -p123456
#建立一個網頁目錄 mkdir -p /www #下載richarvey/nginx-php-fpm鏡像 docker pull richarvey/nginx-php-fpm
docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.6 1f47fade220d 3 hours ago 256MB nginx latest 71c43202b8ac 6 days ago 109MB centos 7 5182e96772bf 4 weeks ago 200MB richarvey/nginx-php-fpm latest af61232ce682 5 weeks ago 297MB
#啓動richarvey/nginx-php-fpm鏡像的容器 docker run -dit --name lnmp_web --network lnmp -p 888:80 --mount type=bind,src=/www,dst=/var/www/html richarvey/nginx-php-fpm docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ba41b324a1cd richarvey/nginx-php-fpm "docker-php-entrypoi…" 13 seconds ago Up 13 seconds 443/tcp, 9000/tcp, 0.0.0.0:888->80/tcp lnmp_web 71e98322f4c2 mysql:5.6 "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 0.0.0.0:3306->3306/tcp lnmp_mysql 7c62aad40cab nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:88->80/tcp nginx-test 02e7612375e7 518 "/bin/bash" 2 hours ago Up 2 hours distracted_villani
tar xf wordpress-4.7.4-zh_CN.tar.gz -C /www/
192.168.50.169:888/wordpressgithub
若是打不開web
按順序執行如下命令一遍 systemctl stop firewalld systemctl stop iptables.service iptables -F iptables -P FORWARD ACCEPT iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT
Docker支持5種網絡模式sql
bridge ; 默認網絡,Docker啓動後默認建立一個docker0網橋,默認建立的容器也是添加到這個網橋中docker
host : 容器不會得到一個獨立的network namespace,而是與宿主機共用一個數據庫
none : 獲取獨立的network namespace,但不爲容器進行任何網絡配置
container :與指定的容器使用同一個network namespace,網卡配置也都是相同的
自定義 :自定義網橋,默認與bridge網絡同樣
#安裝bridge管理工具 yum -y install bridge-utils #查看網橋狀態 brctl show bridge name bridge id STP enabled interfaces br-b59ca0e58388 8000.02427e8ae48b no veth34fc3ac docker0 8000.0242e96ffd31 no vethbc1ab07 vethccc7741 #查看網絡類型 docker network ls NETWORK ID NAME DRIVER SCOPE 78188ea3507b bridge bridge local b68e9da0b1e6 host host local b59ca0e58388 lnmp bridge local cf4b2d0b4394 none null #查看容器進程 docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71e98322f4c2 mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up 2 minutes 0.0.0.0:3306->3306/tcp lnmp_mysql 7c62aad40cab nginx "nginx -g 'daemon of…" 4 hours ago Up 2 minutes 0.0.0.0:88->80/tcp nginx-test 02e7612375e7 518 "/bin/bash" 5 hours ago Up 2 minutes distracted_villani #查看容器lnmp_mysql的網絡信息 docker inspect lnmp_mysql | grep -A 15 "Networks" "Networks": { "lnmp": { #網絡類型lnmp "IPAMConfig": null, "Links": null, "Aliases": [ "71e98322f4c2" ], "NetworkID": "b59ca0e58388b0857e79781de00dc8d81fba77d9d9969afd06496f087c86f7ea", "EndpointID": "c1b8bf54ada9f7d9bb53943b2adcfdab81e33505adaff579f7357307a19dbf37", "Gateway": "172.18.0.1", #網關172.18.0.1,這就是網橋 br-b59ca0e58388 "IPAddress": "172.18.0.2", #容器IP172.18.0.2 "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:12:00:02", #查看容器lnmp_web的網絡信息 docker inspect lnmp_web | grep -A 15 "Networks" "Networks": { "lnmp": { #網絡類型lnmp "IPAMConfig": null, "Links": null, "Aliases": [ "ba41b324a1cd" ], "NetworkID": "b59ca0e58388b0857e79781de00dc8d81fba77d9d9969afd06496f087c86f7ea", "EndpointID": "8c050c4cb67b69179316210e19fb129ebb75ae2fe0ce40b929d6d70dd3d628b6", "Gateway": "172.18.0.1", #網關172.18.0.1,這就是網橋 br-b59ca0e58388 "IPAddress": "172.18.0.3", #容器IP172.18.0.3 "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:12:00:03", #查看容器nginx-test的網絡信息 docker inspect nginx-test | grep -A 15 "Networks" "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "78188ea3507be36ff03aedb3e271c3ca5739983ad12020109b7c739318fd736f", "EndpointID": "9d9b3e2f0dcb049cabd874a91743f16a86b9dcf4c1f41b4b6d849ca7a5dc18cb", "Gateway": "172.17.0.1", #網關172.17.0.1,這就是網橋docker0 "IPAddress": "172.17.0.2", #容器IP172.17.0.2 "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null }
#啓動一個網絡類型爲host的容器 docker run -dit --name hehe --network host centos:7 /bin/bash docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0b74acb0c57d centos:7 "/bin/bash" 3 seconds ago Up 3 seconds hehe ba41b324a1cd richarvey/nginx-php-fpm "docker-php-entrypoi…" 3 hours ago Up 9 minutes 443/tcp, 9000/tcp, 0.0.0.0:888->80/tcp lnmp_web 71e98322f4c2 mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up 14 minutes 0.0.0.0:3306->3306/tcp lnmp_mysql 7c62aad40cab nginx "nginx -g 'daemon of…" 4 hours ago Up 14 minutes 0.0.0.0:88->80/tcp nginx-test 02e7612375e7 518 "/bin/bash" 5 hours ago Up 14 minutes docker exec 0b hostname -I 192.168.50.169 172.18.0.1 172.17.0.1
#啓動一個網絡類型爲host的容器 docker run -dit --name hehe --network host centos:7 /bin/bash
運行一個容器 容器名字hehe 網絡類型 host 鏡像名 開起一個終端 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0b74acb0c57d centos:7 "/bin/bash" 3 seconds ago Up 3 seconds hehe ba41b324a1cd richarvey/nginx-php-fpm "docker-php-entrypoi…" 3 hours ago Up 9 minutes 443/tcp, 9000/tcp, 0.0.0.0:888->80/tcp lnmp_web 71e98322f4c2 mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up 14 minutes 0.0.0.0:3306->3306/tcp lnmp_mysql 7c62aad40cab nginx "nginx -g 'daemon of…" 4 hours ago Up 14 minutes 0.0.0.0:88->80/tcp nginx-test 02e7612375e7 518 "/bin/bash" 5 hours ago Up 14 minutes
docker exec 0b hostname -I #這就是網絡類型爲host的容器,ip地址和docker宿主機徹底同樣
192.168.50.169 172.18.0.1 172.17.0.1
docker exec 02 hostname -I #網橋類型容器
172.17.0.3#查看容器的ip
#啓動一個網絡類型爲none的容器 docker run -dit --name xixi --network none centos:7 d19a66e71c68dd54e889fde9d6efda6b1dfa69cb880a3a6fd9c24136b511856a #查看容器IP地址 docker exec xixi hostname -I 查詢後發現,什麼IP地址都沒有,none類型就是暫時不給容器指定網卡
#啓動一個容器,網絡類型container,使用distracted_villani容器的網卡 docker run -dit --name wawo --network container:distracted_villani centos:7 /bin/bash 52dc72b75e32f98c94ded4a9e491814a5e8de718ca7c5c55d4603236a91c61fb docker inspect 02 | grep -A 15 "Networks" "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "78188ea3507be36ff03aedb3e271c3ca5739983ad12020109b7c739318fd736f", "EndpointID": "90a80dfd64372423317d5d35c65b12f3e38a3751806578417190618bb173b5aa", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:03", "DriverOpts": null } docker inspect wawo | grep -A 15 "Networks" "Networks": {} #test4容器並無本身的網絡設置 } } ] docker exec wawo hostname -I #test4沒有網絡設置卻有IP地址和test容器徹底同樣 172.17.0.3
#構建一個永久生效的網橋br0 vim /etc/sysconfig/network-scripts/ifcfg-eth0 #在配置文件末尾加入BRIDGE=br0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=704ca798-b347-40df-a13d-c521eadd8929 DEVICE=eth0 ONBOOT=yes IPV6_PRIVACY=no BRIDGE=br0 vim /etc/sysconfig/network-scripts/ifcfg-br0 #添加br0配置文件 TYPE=Bridge BOOTPROTO=static DEVICE=br0 ONBOOT=yes IPADDR=192.168.50.169 NETMASK=255.255.255.0 GATEWAY=192.168.50.2 DNS1=192.168.50.2 service network restart #重啓網卡 #查看網卡IP ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #eth0網卡已經沒有IP地址了 inet6 fe80::20c:29ff:fe01:60fa prefixlen 64 scopeid 0x20<link> ether 00:0c:29:01:60:fa txqueuelen 1000 (Ethernet) RX packets 6143 bytes 818283 (799.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3527 bytes 520564 (508.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ifconfig br0 br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.50.169 netmask 255.255.255.0 broadcast 192.168.50.255 #網橋br0代替了eth0 inet6 fe80::20c:29ff:fe01:60fa prefixlen 64 scopeid 0x20<link> ether 00:0c:29:01:60:fa txqueuelen 1000 (Ethernet) RX packets 261 bytes 18191 (17.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 122 bytes 29032 (28.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 brctl show bridge name bridge id STP enabled interfaces br-b59ca0e58388 8000.02427e8ae48b no br0 8000.000c290160fa no eth0 #網橋br0,橋接在了真實的物理網卡eth0上
veth1pl3506 docker0 8000.0242e96ffd31 no vethccc7741
pipework工具下載地址:https://github.com/jpetazzo/pipework.git
#解壓安裝pipework工具 yum -y install unzip unzip pipework-master.zip Archive: pipework-master.zip ae42f1b5fef82b3bc23fe93c95c345e7af65fef3 creating: pipework-master/ extracting: pipework-master/.gitignore inflating: pipework-master/LICENSE inflating: pipework-master/README.md inflating: pipework-master/docker-compose.yml creating: pipework-master/doctoc/ inflating: pipework-master/doctoc/Dockerfile inflating: pipework-master/pipework inflating: pipework-master/pipework.spec mv pipework-master /usr/local/
ln -s /usr/local/pipework-master/pipework /usr/local/bin/
#創建網絡類型爲none的容器,並經過pipework配置固定ip地址 docker run -dit --name gaga --net none centos:5 /bin/bash pipework br0 gaga 192.168.50.28/24@192.168.50.2 #設置容器固定IP爲192.168.200.199網關192.168.200.2 docker exec gaga hostname -I #查看容器gaga的IP 192.168.50.28
[root@wk ~]# docker exec -it gaga /bin/bash #進入容器gaga
[root@d19a66e71c68 /]# ping www.baidu.com #在容器內ping外網能ping通
PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.
64 bytes from 127.0.0.1 (119.75.213.61): icmp_seq=1 ttl=128 time=2.94 ms
64 bytes from 127.0.0.1 (119.75.213.61): icmp_seq=2 ttl=128 time=4.95 ms
在windos上ping容器gaga