[toc]html
Docker官網linux
githubgit
開源的容器引擎,可讓開發者打包應用以及依賴的庫,而後發佈到任何流行的linux發行版上,移植很方便github
由go語言編寫,基於apache2.0協議發佈docker
基於linux kernel,要想在win下運行須要藉助一個vm(虛擬機)來實現 apache
自2013年開始,近些年發展迅猛
docker從1.13x開始,版本分爲社區版ce和企業版ee,而且基於年月的時間線形式,當前最新穩定版爲17.09json
版本信息參考http://blog.csdn.net/chenhaifeng2016/article/details/68062414ubuntu
大白話Docker入門一centos
容器虛擬化:
傳統虛擬化:
[root@xaviyunserver ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--: 0 0 0 0 0 0 0 0 --:--:-- --:--:100 2424 100 2424 0 0 2009 0 0:00:01 0:00:01 --:--:-- 2011 [root@xaviyunserver ~]# yum install -y docker-ce
[root@xaviyunserver ~]# systemctl start docker [root@xaviyunserver ~]# ps aux | grep docker root 1624 0.7 3.2 452080 61592 ? Ssl 11:10 0:00 /usr/bin/dockerd root 1628 0.1 1.0 289788 20100 ? Ssl 11:10 0:00 docker-containerd --config /var/run/docker/containerd/containerd.toml root 1771 0.0 0.0 112660 968 pts/0 R+ 11:11 0:00 grep --color=auto docke [root@xaviyunserver ~]# docker version Client: Version: 18.03.1-ce API version: 1.37 Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:20:16 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.03.1-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:23:58 2018 OS/Arch: linux/amd64 Experimental: false
[root@xaviyunserver ~]# iptables -nvL Chain INPUT (policy ACCEPT 110 packets, 7188 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DOCKER-USER all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 DOCKER-ISOLATION-STAGE-1 all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 80 packets, 15362 bytes) pkts bytes target prot opt in out source destination Chain DOCKER (1 references) pkts bytes target prot opt in out source destination Chain DOCKER-ISOLATION-STAGE-1 (1 references) pkts bytes target prot opt in out source destination 0 0 DOCKER-ISOLATION-STAGE-2 all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 Chain DOCKER-ISOLATION-STAGE-2 (1 references) pkts bytes target prot opt in out source destination 0 0 DROP all -- * docker0 0.0.0.0/0 0.0.0.0/0 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 Chain DOCKER-USER (1 references) pkts bytes target prot opt in out source destination 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
[root@xaviyunserver ~]# cat //這裏我實驗用的是雲服務器,以前沒有作個iptable練習 /etc/sysconfig/iptables cat: /etc/sysconfig/iptables: No such file or directory [root@xaviyunserver ~]# cat /etc/sysconfig/iptables-config
[root@xaviyunserver ~]# iptables -t nat -F [root@xaviyunserver ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain DOCKER (0 references) pkts bytes target prot opt in out source destination
重啓以後ipatble規則又出現了
vi /etc/docker/daemon.json //加入以下內容 { "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"] }
說明:這個url爲加速器地址,須要同窗自行到阿里雲申請。
[root@xaviyunserver ~]# vim /etc/docker/daemon.json [root@xaviyunserver ~]# systemctl restart docker [root@xaviyunserver ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos 7dc0dca2b151: Pull complete Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322 Status: Downloaded newer image for centos:latest
[root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 49f7960eb7e4 4 weeks ago 200MB
從docker倉庫搜索centos鏡像
[root@xaviyunserver ~]# docker search centos
[root@xaviyunserver ~]# docker pull ubuntu //相似於wget [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 113a43faa138 4 weeks ago 81.2MB centos latest 49f7960eb7e4 4 weeks ago 200MB
[root@xaviyunserver ~]# docker tag centos xavilinux_centos
[root@xaviyunserver ~]# docker tag centos test11:xavier [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 113a43faa138 4 weeks ago 81.2MB centos latest 49f7960eb7e4 4 weeks ago 200MB test11 xavier 49f7960eb7e4 4 weeks ago 200MB xavilinux_centos latest 49f7960eb7e4 4 weeks ago 200MB
[root@xaviyunserver ~]# docker run -itd centos [root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83a4c32c03ca centos "/bin/bash" 42 seconds ago Up 41 seconds nostalgic_lamport
[root@xaviyunserver ~]# docker rmi test11 Error: No such image: test11 [root@xaviyunserver ~]# docker rmi test11:xavier Untagged: test11:xavier [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 113a43faa138 4 weeks ago 81.2MB centos latest 49f7960eb7e4 4 weeks ago 200MB xavilinux_centos latest 49f7960eb7e4 4 weeks ago 200MB
這裏只是刪除了tag,至關於刪除了一個硬連接而已
剛剛我只是說到了如何刪除修改一些基礎的操做,一旦鏡像搭建好了,如何才能經過命令行進入系統呢?
[root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83a4c32c03ca centos "/bin/bash" 25 minutes ago Up 25 minutes
查看容器大小,空間
[root@xaviyunserver ~]# docker exec -it 83a4c3 bash [root@83a4c32c03ca /]# ls bin etc lib media opt root sbin sys usr dev home lib64 mnt proc run srv tmp var [root@83a4c32c03ca /]# df -h Filesystem Size Used Avail Use% Mounted on overlay 40G 3.8G 34G 10% / tmpfs 64M 0 64M 0% /dev tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/vda1 40G 3.8G 34G 10% /etc/hosts shm 64M 0 64M 0% /dev/shm tmpfs 920M 0 920M 0% /proc/scsi tmpfs 920M 0 920M 0% /sys/firmware
容器裏面有沒有IP呢?沒有
[root@83a4c32c03ca /]# ifconfig bash: ifconfig: command not found [root@83a4c32c03ca /]# yum install -y net-tools //安裝net-tools
[root@83a4c32c03ca /]# 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 5867 bytes 11263312 (10.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3745 bytes 287096 (280.3 KiB) 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 1 (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
查看系統IP,能夠看到新生成的虛擬網卡docker0,這兒VMware裏面的vmnet8同樣,沒生成一個虛擬機,會產生一個虛擬網卡和宿主機來通信。
好比這裏咱們就不用在有安裝net-tools的這個服務了
[root@xaviyunserver ~]# docker commit -m "install net-tools" -a "xavilinux" 83a4c32c03ca centos_with_net sha256:097367cfbba8cc4041fe2238aca01c03ffb00abe85ce34a0befecaad49b14556 [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_with_net latest 097367cfbba8 9 seconds ago 282MB ubuntu latest 113a43faa138 4 weeks ago 81.2MB xavilinux_centos latest 49f7960eb7e4 4 weeks ago 200MB centos latest 49f7960eb7e4 4 weeks ago 200MB
進入這個新創建的容器,
[root@xaviyunserver ~]# docker run -itd centos_with_net bash deda7b6cd703375c746a1c02d7864c29ca82c4742efc6f095180e5b6d8b730a3 [root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES deda7b6cd703 centos_with_net "bash" 8 seconds ago Up 7 seconds gallant_hypatia 83a4c32c03ca centos "/bin/bash" About an hour ago Up About an hour nostalgic_lamport
[root@xaviyunserver ~]# docker exec -it gallant_hypatia bash
http://openvz.org/Download/templates/precreated wget http://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz
若是虛擬機沒有rz命令,下載安裝lrzsz安裝包
[root@xaviyunserver ~]# yum install lrzsz -y
[root@xaviyunserver ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos7 sha256:1513b3b29ff7fff26ea3041f0afddaa06195b4d9dcce388b8a829e4261828ae5
[root@xaviyunserver ~]# docker run -itd centos7 bash 410e90b34976c82e2d2fde0d50aff1a240c441b1f94dd3d714f4d9963b0c314f [root@xaviyunserver ~]# docker exec -it 410e90b34976c bash [root@410e90b34976 /]#
[root@410e90b34976 /]# uname -a Linux 410e90b34976 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux [root@410e90b34976 /]# exit exit [root@xaviyunserver ~]# uname -a Linux xaviyunserver 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@xaviyunserver ~]# docker save -o centosxavi.tar centos_with_net [root@xaviyunserver ~]# du -sh centosxavi.tar 278M centosxavi.tar
[root@xaviyunserver ~]# docker rm -f deda7b6cd703 deda7b6cd703
[root@xaviyunserver ~]# docker rmi 097367cfbba8 Untagged: centos_with_net:latest Deleted: sha256:097367cfbba8cc4041fe2238aca01c03ffb00abe85ce34a0befecaad49b14556 Deleted: sha256:b887b67ee0202a8290b52ebeb07e5f418c7f4ad801e1c17b85303fca581c999b [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest 1513b3b29ff7 About an hour ago 435MB ubuntu latest 113a43faa138 4 weeks ago 81.2MB centos latest 49f7960eb7e4 4 weeks ago 200MB xavilinux_centos latest 49f7960eb7e4 4 weeks ago
[root@xaviyunserver ~]# docker load < centosxavi.tar d8066854a2c8: Loading layer [==================================================>] 82.61MB/82.61MB Loaded image: centos_with_net:latest [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest 1513b3b29ff7 About an hour ago 435MB centos_with_net latest 097367cfbba8 2 hours ago 282MB ubuntu latest 113a43faa138 4 weeks ago 81.2MB centos latest 49f7960eb7e4 4 weeks ago 200MB xavilinux_centos latest 49f7960eb7e4 4 weeks ago 200MB
docker save:
打包以後的images.tar包含postgres:9.6和mongo:3.4這兩個鏡像。
上述命令將會把postgres:9.6和mongo:3.4載入進來,若是本地鏡像庫已經存在這兩個鏡像,將會被覆蓋。
docker export: