[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: