在咱們開始瞭解linux運維開始,咱們就據說了docker,很是流行,幾乎大多數公司都在使用,但docker具體是什麼,有什麼做用,咱們卻不太瞭解。html
docker字面意思就是容器,它是一個開源的容器引擎,可以快速地部署環境和發佈代碼,可讓開發者打包應用以及依賴的庫,而後發佈到任何流行的linux發行版上,移植很方便。node
由go語言開發,基於apache2.0協議發佈,官網:www.docker.com ,GitHub:https://github.com/docker/docker.github.io 。版本分爲社區版ce和企業版ee,基於年月的時間線形式,當前最新穩定版爲18.06。python
docker相對於傳統虛擬化的優點:linux
1. 啓動很是快,秒級實現; 2. 資源利用率高,一臺高配置服務器能夠跑上千個docker容器; 3. 更快的部署和交付,一次建立和配置後,能夠在任意地方運行; 4. 內核級別的虛擬化,不須要額外的hypevisor支持,會有更高的性能和效率; 5. 容易遷移,平臺依賴性不高。
Docker概念:nginx
# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo #curl -o ,把輸入寫到該文件中,即把下載下來的文件寫入/etc/yum.repos.d/docker.repo中# yum list |grep dockerdocker-ce.x86_64 18.06.1.ce-3.el7 @docker-ce-stable cockpit-docker.x86_64 172-2.el7.centos extras containerd.io.x86_64 1.2.0-1.2.beta.2.el7 docker-ce-stable docker.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos docker-ce-selinux.noarch 17.03.3.ce-1.el7 docker-ce-stable docker-client.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos docker-client-latest.x86_64 1.13.1-58.git87f2fab.el7.centos docker-common.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos docker-compose.noarch 1.18.0-1.el7 epel docker-devel.x86_64 1.3.2-4.el7.centos extras docker-distribution.x86_64 2.6.2-2.git48294d9.el7 extras docker-forward-journald.x86_64 1.10.3-44.el7.centos extras docker-latest.x86_64 1.13.1-58.git87f2fab.el7.centos docker-latest-logrotate.x86_64 1.13.1-58.git87f2fab.el7.centos docker-latest-v1.10-migrator.x86_64 1.13.1-58.git87f2fab.el7.centos docker-logrotate.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos docker-lvm-plugin.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos docker-novolume-plugin.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos docker-python.x86_64 1.4.0-115.el7 extras docker-registry.x86_64 0.9.1-7.el7 extras docker-unit-test.x86_64 2:1.13.1-68.gitdded712.el7.centos docker-v1.10-migrator.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos golang-github-fsouza-go-dockerclient-devel.x86_64 kdocker.x86_64 4.9-1.el7 epel pcp-pmda-docker.x86_64 3.12.2-5.el7 base python-docker-py.noarch 1.10.6-4.el7 extras python-docker-pycreds.noarch 1.10.6-4.el7 extras python-docker-scripts.noarch 0.4.4-1.el7 epel python-dockerfile-parse.noarch 0.0.5-1.el7 epel python2-avocado-plugins-runner-docker.noarch python2-dockerpty.noarch 0.4.1-9.el7 epel python34-docker.noarch 2.6.1-1.el7 epel python34-docker-pycreds.noarch 0.2.1-1.el7 epel python34-dockerpty.noarch 0.4.1-9.el7 epel # yum install -y docker-ce
# systemctl start docker# ps aux |grep dockerroot 17893 0.5 5.0 598868 50260 ? Ssl 01:46 0:00 /usr/bin/dockerd root 17899 0.2 2.7 317288 27692 ? Ssl 01:46 0:00 docker-containerd --config /var/run/docker/containerd/containerd.toml# iptables -nvL Chain INPUT (policy ACCEPT 108 packets, 7280 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 59 packets, 5814 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 #每次啓動docker都會自動生成iptables規則
# docker pull centos #下載鏡像Using default tag: latest latest: Pulling from library/centos 256b176beaff: Pull complete Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf Status: Downloaded newer image for centos:latest
上面下載鏡像很慢,因此咱們能夠配置一個docker加速器。git
# vim /etc/docker/daemon.json{ "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"] #該url爲加速器地址,可自行去阿里雲申請本身的加速器地址}
配置完後,重啓docker,以後再下載鏡像會比以前快不少。github
# systemctl restart docker #重啓docker# docker pull ubuntuUsing default tag: latest latest: Pulling from library/ubuntu 124c757242f8: Pull complete 9d866f8bde2a: Pull complete fa3f2f277e67: Pull complete 398d32b153e8: Pull complete afde35469481: Pull complete Digest: sha256:de774a3145f7ca4f0bd144c7d4ffb2931e06634f11529653b23eba85aef8e378 Status: Downloaded newer image for ubuntu:latest
# docker images #查看本地鏡像REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB
# docker search jumpserver #搜索鏡像,能夠搜到不少其餘人作的鏡像NAME DESCRIPTION STARS OFFICIAL AUTOMATED jumpserver/jumpserver 10 jiaxiangkong/jumpserver_docker 開源跳板機(堡壘機):認證,受權,審計,自動化運維 10 hhding/jumpserver-docker ssh proxy node 3 [OK]njqaaa/jumpserver jumpserver 2 [OK]baselibrary/jumpserver jumpserver 1 [OK]zhegao/jumpserver Jumpserver 1.4.0 1 jumpserver/allinone jumpserver all in one 1 [OK]zqiannnn/jumpserver-ansible JumpServer Ansible Addon 1 [OK]jumpserver/guacamole guacamole for jumpserver 1 [OK]kubernetesio/sshd-jumpserver sshd-jumpserver 0 [OK]vikings/jumpserver 0 satoms/jumpserver 0 zsjohny/jumpserver bastion web ui 0 [OK]jumpserver/python 0 qiwihui/jumpserver jumpserver docker 0 [OK]jumpserver/coco 0 qq58945591/jumpserver JumpServer集成coco和luna,使用nginx進行反向代… 0 [OK]jumpserver/luna 0 lc13579443/jumpserver Jumpserver all in one Dockerfile 0 [OK]ibuler/jumpserver 0 qbtrade/jumpserver 0 jumpserver/core Jumpserver Official Docker Image 0 [OK]qbtrade/jumpserver_coco 0 jumpserver/base-env-alpine 0 mapsic/jumpserver jumpserver 0 [OK]
# docker tag centos lzx_centos #給鏡像打上標籤# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
打完標籤會從新另生成一份鏡像,不過IMAGE ID
不會改變,這代表這兩個鏡像實際上是同樣的。golang
此外還能夠這樣標識,改變TAG。web
# docker tag centos test:123# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest cd6d8154f1e1 7 days ago 84.1MBtest 123 5182e96772bf 5 weeks ago 200MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
#啓動鏡像爲容器;-i表示讓容器的標準輸入打開;-t表示分配一個僞終端;-d表示後臺啓動,參數須要放到鏡像名字前面# docker run -itd centos3790ac27cf7ef69a389390ebb11045d89cd05693615fddb74fd28dfe5adfbb2e# docker ps #查看啓動狀態的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3790ac27cf7e centos "/bin/bash" 5 seconds ago Up 4 seconds epic_beaver# docker ps -a # -a查看全部容器,包括啓動狀態和中止狀態CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3790ac27cf7e centos "/bin/bash" 4 minutes ago Up 4 minutes epic_beaver
# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MBtest 123 5182e96772bf 5 weeks ago 200MB# docker rmi test:123 #刪除鏡像,刪除時必需要帶上TAG,不然刪除不了Untagged: test:123# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
刪除時,後面的參數能夠是TAG,若是是TAG時,實際上刪除的是該TAG;當後面的參數爲IMAGE ID時,則會刪除整個鏡像,全部標籤也會一同刪除。docker
鏡像能夠經過docker pull
下載鏡像,也能夠本身自定義建立鏡像。
# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3790ac27cf7e centos "/bin/bash" 27 minutes ago Up 27 minutes epic_beaver# docker exec -it 3790ac bash #進入容器界面,容器ID能夠簡寫[root@3790ac27cf7e /]# lsanaconda-post.log dev home lib64 mnt proc run srv tmp var bin etc lib media opt root sbin sys usr[root@3790ac27cf7e /]# df -hFilesystem Size Used Avail Use% Mounted on overlay 18G 1.8G 17G 10% / tmpfs 64M 0 64M 0% /dev tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 18G 1.8G 17G 10% /etc/hosts shm 64M 0 64M 0% /dev/shm tmpfs 489M 0 489M 0% /proc/acpi tmpfs 489M 0 489M 0% /proc/scsi tmpfs 489M 0 489M 0% /sys/firmware #大小和宿主機一致[root@3790ac27cf7e /]# yum install -y net-tools #容器裏可使用的命令數量有限[root@3790ac27cf7e /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 #容器網卡有本身的ip ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 1009 bytes 12369051 (11.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 793 bytes 46160 (45.0 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 [root@3790ac27cf7e /]# exitexit# ifconfigdocker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 #多出來一個docker0網卡 inet6 fe80::42:c6ff:fe3a:47c2 prefixlen 64 scopeid 0x20<link> ether 02:42:c6:3a:47:c2 txqueuelen 0 (Ethernet) RX packets 793 bytes 35058 (34.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1001 bytes 12368403 (11.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255 inet6 fe80::84c7:fae0:d9f5:d9c0 prefixlen 64 scopeid 0x20<link> inet6 fe80::b6f9:83f6:f7f2:ece0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet) RX packets 130382 bytes 181561741 (173.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 33277 bytes 2573534 (2.4 MiB) 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 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 1145 bytes 133845 (130.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1145 bytes 133845 (130.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth86bb548: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #每開啓一個容器就多出來一個虛擬網卡 inet6 fe80::3c54:9eff:fea2:50ee prefixlen 64 scopeid 0x20<link> ether 3e:54:9e:a2:50:ee txqueuelen 0 (Ethernet) RX packets 793 bytes 46160 (45.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1009 bytes 12369051 (11.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#-m指定改動信息;-a指定做者相關信息;3790ac爲容器id,後面接新鏡像的名字# docker commit -m "install net-tools" -a "lzx" 3790ac centos_with_netsha256:ef7d535f820682b1ed041caf74c79fa32df6c26778f34ee18dd1448ac6f012ab# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos_with_net latest ef7d535f8206 16 seconds ago 293MB //多出來一個新鏡像centos_with_net ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
啓動新鏡像就能夠直接使用net-tools工具。
# docker run -itd centos_with_net bash #啓動新鏡像爲容器b74a447962c80b2caf79727d39ee5d965a27aad6c6293554870748ac53408131# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" 53 seconds ago Up 52 seconds vigorous_allen 3790ac27cf7e centos "/bin/bash" 45 minutes ago Up 45 minutes epic_beaver# docker exec -it vigorous_allen bash #進入容器界面,參數後面能夠跟容器ID,也能夠跟容器名字[root@b74a447962c8 /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 #從新啓動的容器ip與以前並不一樣 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 8 bytes 648 (648.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 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 [root@b74a447962c8 /]# ping www.qq.com PING https.qq.com (58.247.214.47) 56(84) bytes of data. 64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=1 ttl=127 time=13.8 ms 64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=2 ttl=127 time=14.6 ms 64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=3 ttl=127 time=13.5 ms 64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=4 ttl=127 time=16.9 ms ^C --- https.qq.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3011ms rtt min/avg/max/mdev = 13.558/14.758/16.972/1.353 ms #宿主機能夠聯網,容器就能夠聯網
[root@b74a447962c8 /]# exitexit# ifconfigdocker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:c6ff:fe3a:47c2 prefixlen 64 scopeid 0x20<link> ether 02:42:c6:3a:47:c2 txqueuelen 0 (Ethernet) RX packets 805 bytes 35814 (34.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1009 bytes 12369133 (11.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255 inet6 fe80::84c7:fae0:d9f5:d9c0 prefixlen 64 scopeid 0x20<link> inet6 fe80::b6f9:83f6:f7f2:ece0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet) RX packets 131136 bytes 181625077 (173.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 33771 bytes 2625917 (2.5 MiB) 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 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 1165 bytes 134885 (131.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1165 bytes 134885 (131.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth733e1d0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #新生成了一個虛擬網卡 inet6 fe80::2076:95ff:fe66:903c prefixlen 64 scopeid 0x20<link> ether 22:76:95:66:90:3c txqueuelen 0 (Ethernet) RX packets 12 bytes 924 (924.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 16 bytes 1378 (1.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth86bb548: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::3c54:9eff:fea2:50ee prefixlen 64 scopeid 0x20<link> ether 3e:54:9e:a2:50:ee txqueuelen 0 (Ethernet) RX packets 793 bytes 46160 (45.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1010 bytes 12369093 (11.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# wget https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz #下載模板# du -sh centos-7-x86_64-minimal.tar.gz 139M centos-7-x86_64-minimal.tar.gz
# cat centos-7-x86_64-minimal.tar.gz|docker import - centos7 #導入模板鏡像sha256:9934306dddc6b438e960de9523228dcc65b5ecd6692ae6a2f55d33f271dd7829# docker images #查看本地鏡像REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest 9934306dddc6 14 seconds ago 435MB #有剛剛導入的鏡像 centos_with_net latest ef7d535f8206 45 minutes ago 293MB ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
# docker run -itd centos7 bash #啓動鏡像爲容器,後面加上bash,不然報錯c4aa635a5defb558bc54877eca0213fa3bf9002651065aef6d1795f3a2f19aef# docker exec -it c4aa63 bash #進入容器界面[root@c4aa635a5def /]# cat /etc/redhat-release #查看系統版本CentOS Linux release 7.2.1511 (Core)
# docker save -o centos7.tar centos7 #前面爲文件名,後面跟鏡像名字# docker save -o centos7.tar centos7# lsanaconda-ks.cfg centos7.tar centos-7-x86_64-minimal.tar.gz
# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest 9934306dddc6 24 minutes ago 435MB centos_with_net latest ef7d535f8206 About an hour ago 293MB ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB# docker rmi 9934306 #刪除鏡像Error response from daemon: conflict: unable to delete 9934306dddc6 (cannot be forced) - image is being used by running container c4aa635a5def #刪除報錯,提示鏡像正在被容器運行# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c4aa635a5def centos7 "bash" 20 minutes ago Up 20 minutes agitated_tesla b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver# docker rm -f c4aa635a5def #強行刪除容器c4aa635a5def# docker rmi 9934306 #再刪除鏡像Untagged: centos7:latest Deleted: sha256:9934306dddc6b438e960de9523228dcc65b5ecd6692ae6a2f55d33f271dd7829 Deleted: sha256:788edba9eaa8ade63d8ba9d5747281c5da2b34b12a6c80f4dffd8ad9e05f68c1# docker images #查看本地鏡像REPOSITORY TAG IMAGE ID CREATED SIZE centos_with_net latest ef7d535f8206 About an hour ago 293MB ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
# docker load --input centos7.tar #用文件導入鏡像,也可使用docker load < centos7.tar 來導入鏡像788edba9eaa8: Loading layer 446.1MB/446.1MB Loaded image: centos7:latest# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest 9934306dddc6 32 minutes ago 435MB #有剛剛新導入的鏡像centos_with_net latest ef7d535f8206 About an hour ago 293MB ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
# docker push image_name 能夠把我的鏡像上傳到dockerhub官方網站上去,但前提是須要先註冊
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver# docker create -it centos7 bash #建立新的容器,但沒有運行95c783a08d9ef31c66948053bdee8d1bd502e9bf2816492cd840acc787acb1e2# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver# docker ps -a #查看全部容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95c783a08d9e centos7 "bash" 10 seconds ago Created nifty_davinci #這裏狀態也顯示createdb74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker start 95c783 #啓動容器95c783# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95c783a08d9e centos7 "bash" 2 minutes ago Up 2 seconds nifty_davinci //容器已經啓動 b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours
# docker restart 95c783 #重啓容器95c783# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95c783a08d9e centos7 "bash" 5 minutes ago Up 2 seconds nifty_davinci b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker stop 95c783 #關閉容器95c783# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95c783a08d9e centos7 "bash" 6 minutes ago Exited (137) 7 seconds ago nifty_davinci #這裏狀態提示退出b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours
# docker run -it centos bash #不加-d選項可直接進入容器界面[root@ee69e21a9360 /]# lsanaconda-post.log dev home lib64 mnt proc run srv tmp var bin etc lib media opt root sbin sys usr[root@ee69e21a9360 /]# ifconfigbash: ifconfig: command not found[root@ee69e21a9360 /]# exit #退出容器exit# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ee69e21a9360 centos "bash" 2 minutes ago Exited (127) 57 seconds ago modest_williams #exit退出以後容器就再也不運行95c783a08d9e centos7 "bash" 10 minutes ago Exited (137) 4 minutes ago nifty_davinci b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours
# docker run -itd --name lzx123 centos bash #自定義容器名字啓動004494c2c9f7e58cdbc23e19af42dd54b49db931975210702112d8c44ef705e6# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 004494c2c9f7 centos "bash" 8 seconds ago Up 7 seconds lzx123 #名字顯示爲剛自定義的名字b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver# docker exec -it lzx123 bash #經過容器名字進入對應的容器界面[root@004494c2c9f7 /]#
# docker run --rm -it centos bash -c "sleep 30" #--rm 讓容器執行命令後退出且刪除容器;-c指定執行的命令# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 004494c2c9f7 centos "bash" 6 minutes ago Up 6 minutes lzx123 ee69e21a9360 centos "bash" 12 minutes ago Exited (127) 11 minutes ago modest_williams 95c783a08d9e centos7 "bash" 21 minutes ago Exited (137) 14 minutes ago nifty_davinci b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker run -itd centos bash -c "echo 123"0d4528e9ba188610a2685cf64ff8971063f26318be2d18a2c699d181e48ce43c# docker logs 0d452 #查看容器運行的歷史信息123
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 004494c2c9f7 centos "bash" 13 minutes ago Up 13 minutes lzx123 b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver# docker attach 004494 #進入一個後臺運行的容器[root@004494c2c9f7 /]# exitexit# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
能夠看到,docker attach
不太好用,由於進入後臺容器後,用exit退出後容器直接中止運行。相比之下,使用docker exec -it container_id bash
進入容器以後,用exit退出後容器仍在運行。
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d4528e9ba18 centos "bash -c 'echo 123'" 7 minutes ago Exited (0) 7 minutes ago suspicious_varahamihira 004494c2c9f7 centos "bash" 18 minutes ago Exited (0) 5 minutes ago lzx123 ee69e21a9360 centos "bash" 24 minutes ago Exited (127) 23 minutes ago modest_williams 95c783a08d9e centos7 "bash" 33 minutes ago Exited (137) 26 minutes ago nifty_davinci b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver# docker rm 0d4528e #刪除容器0d4528e# docker rm 004494004494# docker rm ee69e2ee69e2# docker rm 95c78395c783# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
刪除正在運行中的容器會報錯,除非加上-f選項。
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver# docker export b74a44 > 111.tar #導出容器爲文件,這樣就能夠遷移到其它機器上# ls111.tar anaconda-ks.cfg centos7.tar centos-7-x86_64-minimal.tar.gz# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen 3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
導出以後,正在運行的容器也不會消失。
# cat 111.tar |docker import - lzx_test #導入容器爲鏡像,最後面爲自定義的鏡像名字sha256:5926537d3e30ea6d88f543d466e6418a3aa57679e6bd354eac7b2af1029d8067# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE lzx_test latest 5926537d3e30 About a minute ago 272MB #有剛剛導入的容器鏡像<none> <none> 531fcbd0fb9d 2 minutes ago 0B centos7 latest 9934306dddc6 About an hour ago 435MB centos_with_net latest ef7d535f8206 2 hours ago 293MB ubuntu latest cd6d8154f1e1 7 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
對於公司來說,把本身的鏡像推送到docker公共倉庫顯得不太合適,不過咱們能夠創建本身本地的docker私有倉庫。
# docker pull registry #registry鏡像是docker官方提供的一個鏡像,可用來建立本地私有倉庫Using default tag: latest latest: Pulling from library/registry d6a5679aa3cf: Pull complete ad0eac849f8f: Pull complete 2261ba058a15: Pull complete f296fda86f10: Pull complete bcd4a541795b: Pull complete Digest: sha256:5a156ff125e5a12ac7fdec2b90b7e2ae5120fa249cf62248337b6d04abc574c8 Status: Downloaded newer image for registry:latest
# docker run -d -p 5000:5000 registry #-p指定映射端口,左邊是宿主機監聽端口,右邊是容器監聽端口f708d00d51022f9a0ffb55619a0556603db8e1e3bbdf1ab79897ab68973b9803# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f708d00d5102 registry "/entrypoint.sh /etc…" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp pedantic_lamport
# curl 127.0.0.1:5000/v2/_catalog #查看私有倉庫{"repositories":[]} #如今沒有任何鏡像
打上標籤
# docker tag centos7 192.168.33.150:5000/centos7 #打標籤這一步比較重要,前面須要加上宿主機ip及監聽端口# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE lzx_test latest 5926537d3e30 17 hours ago 272MB 192.168.33.150:5000/centos7 latest 9934306dddc6 19 hours ago 435MB centos7 latest 9934306dddc6 19 hours ago 435MB centos_with_net latest ef7d535f8206 19 hours ago 293MB registry latest 2e2f252f3c88 47 hours ago 33.3MB ubuntu latest cd6d8154f1e1 8 days ago 84.1MB centos latest 5182e96772bf 5 weeks ago 200MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB
修改配置文件
# vim /etc/docker/daemon.json #更改成下面內容,不能添加,須要刪除以前的加速器url{ "insecure-registries": ["192.168.33.150:5000"]}# systemctl restart docker #修改配置文件後須要重啓docker# docker start f708d0f708d0# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f708d00d5102 registry "/entrypoint.sh /etc…" 21 minutes ago Up 4 seconds 0.0.0.0:5000->5000/tcp pedantic_lamport
上傳鏡像
# docker push 192.168.33.150:5000/centos7 #上傳鏡像The push refers to repository [192.168.33.150:5000/centos7]788edba9eaa8: Pushed latest: digest: sha256:c6e83c601856c3260c7e5c9789e22e0f40179739dca6e0feac1f596ca0f9452c size: 529
# curl 127.0.0.1:5000/v2/_catalog{"repositories":["centos7"]} #多出來剛剛新上傳的centos7鏡像
再上傳一個鏡像
# docker tag ubuntu 192.168.33.150:5000/ubuntu# docker push 192.168.33.150:5000/ubuntuThe push refers to repository [192.168.33.150:5000/ubuntu]8d7ea83e3c62: Pushed 6a061ee02432: Pushed f73b2816c52a: Pushed 6267b420796f: Pushed a30b835850bf: Pushed latest: digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20 size: 1357# curl 127.0.0.1:5000/v2/_catalog{"repositories":["centos7","ubuntu"]} #能夠看到剛剛上傳的ubuntu鏡像
這裏選另一臺機器,由於建立私有倉庫總不可能爲一臺機器服務。新的機器須要安裝docker,且啓動docker服務。
# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo# yum install -y docker-ce# systemctl start docker# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
# vim /etc/docker/daemon.json{ "insecure-registries": ["192.168.33.150:5000"]}# docker pull 192.168.33.150:5000/centos7 #下載鏡像centos7Using default tag: latest Error response from daemon: Get https://192.168.33.150:5000/v2/: http: server gave HTTP response to HTTPS client #有報錯
上面有報錯,查了下,有多是配置文件和容器沒有啓動致使。
檢查配置文件,發現兩臺機器配置文件一致,不存在問題,並且宿主機的容器也啓動了,到這裏我有點疑惑。
我決定宿主機換個映射端口試一下。
# systemctl restart docker # docker run -d -p 5001:5000 registry #更換映射端口爲5001a3f011dfe5d39586688701274d0f7cc752593c3a49251348bcfe04616632e8eb# curl 127.0.0.1:5001/v2/_catalog{"repositories":[]}# docker tag ubuntu 192.168.33.150:5001/ubuntu # vim /etc/docker/daemon.json #修改成下面內容{ "insecure-registries": ["192.168.33.150:5001"]}# systemctl restart docker# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a3f011dfe5d3 registry "/entrypoint.sh /etc…" About a minute ago Exited (2) 17 seconds ago vibrant_shannon bb193bc12150 registry "/entrypoint.sh /etc…" 3 minutes ago Exited (2) 2 minutes ago jolly_joliot 98f9faf1796d centos "/bin/bash" 44 minutes ago Exited (137) 12 minutes ago condescending_northcutt d0e1e06bc0c7 ubuntu "/bin/bash" About an hour ago Exited (0) 13 minutes ago cocky_turing 503e851a720f centos "/bin/bash" 2 hours ago Exited (137) 2 hours ago amazing_hodgkin ad4534b0f956 centos_with_net "/bin/bash" 2 hours ago Exited (137) 2 hours ago xenodochial_torvalds f708d00d5102 registry "/entrypoint.sh /etc…" 2 hours ago Exited (2) 13 minutes ago pedantic_lamport b74a447962c8 centos_with_net "bash" 21 hours ago Exited (137) 19 hours ago vigorous_allen 3790ac27cf7e centos "/bin/bash" 22 hours ago Exited (137) 19 hours ago epic_beaver# docker start a3f011a3f011# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a3f011dfe5d3 registry "/entrypoint.sh /etc…" 2 minutes ago Up 3 seconds 0.0.0.0:5001->5000/tcp vibrant_shannon# docker push 192.168.33.150:5001/ubuntuThe push refers to repository [192.168.33.150:5001/ubuntu]8d7ea83e3c62: Pushed 6a061ee02432: Pushed f73b2816c52a: Pushed 6267b420796f: Pushed a30b835850bf: Pushed latest: digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20 size: 1357# curl 127.0.0.1:5001/v2/_catalog{"repositories":["ubuntu"]}
新機器上修改配置文件
# systemctl start docker# ps aux |grep dockerroot 1273 0.6 5.2 592596 52092 ? Ssl 01:20 0:00 /usr/bin/dockerd root 1279 0.5 2.6 382824 26092 ? Ssl 01:20 0:00 docker-containerd --config /var/run/docker/containerd/containerd.toml root 1419 0.0 0.0 112704 964 pts/0 R+ 01:20 0:00 grep --color=auto docker# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE# vim /etc/docker/daemon.json{ "insecure-registries": ["192.168.33.150:5001"]}# systemctl restart docker# docker pull 192.168.33.150:5001/ubuntu #從私有倉庫下載鏡像Using default tag: latest latest: Pulling from ubuntu 124c757242f8: Pull complete 9d866f8bde2a: Pull complete fa3f2f277e67: Pull complete 398d32b153e8: Pull complete afde35469481: Pull complete Digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20 Status: Downloaded newer image for 192.168.33.150:5001/ubuntu:latest# docker images #本地鏡像就是剛剛下載的鏡像REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.33.150:5001/ubuntu latest cd6d8154f1e1 8 days ago 84.1MB
上面能夠看到,換了映射端口以後,從私有倉庫下載鏡像成功。
容器是由鏡像啓動的,若是容器裏面產生了新的數據,那麼當容器關閉或刪除時,這些數據也會一併刪除,對於數據是存在必定風險的。
爲了不容器中新產生的數據被刪除,咱們能夠掛載本地的目錄到容器裏。
#掛載本地目錄到容器,-v指定掛載目錄,左邊爲宿主機本地目錄,右邊爲容器裏目錄,會在容器中自動建立# docker run -itd -v /data:/data centos_with_net bashf6f4ef99fae3bc2ef9e87304ebb2231a87db0ea3ac20bd6aa6148f0fe0ce7bad# ls /data/svnroot# docker exec -it f6f4e bash[root@f6f4ef99fae3 /]# ls -l /data/total 0 drwxr-xr-x 3 root root 23 Aug 31 08:18 svnroot[root@f6f4ef99fae3 /]# mkdir /data/123 #容器裏面建立新目錄[root@f6f4ef99fae3 /]# exitexit# ls -l /data/ total 0 drwxr-xr-x 2 root root 6 Sep 14 02:02 123 #宿主機裏面有新目錄產生drwxr-xr-x 3 root root 23 Aug 31 04:18 svnroot
掛載目錄時能夠指定容器名字,若是不指定,會隨機生成一個名字。
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6f4ef99fae3 centos_with_net "bash" 29 minutes ago Up 29 minutes determined_goodall #隨機生成的容器名字a3f011dfe5d3 registry "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:5001->5000/tcp vibrant_shannon
開啓新的容器
# docker run -itd --volumes-from determined_goodall centos7 bash #掛載數據卷,使用上面的容器做爲數據卷容器打開新的容器4b9edc313afdad51f288605d7c05cade963b474a61afe15e6400661114164075# docker exec -it 4b9edc bash[root@4b9edc313afd /]# ls -l /data/ #下面能夠看到新容器的目錄與數據卷容器的目錄是相同的total 0 drwxr-xr-x 2 root root 6 Sep 14 02:02 123 drwxr-xr-x 3 root root 23 Aug 31 04:18 svnroot
有時候,咱們須要多個容器相互共享數據,相似於linux裏面的NFS,因此能夠搭建一個專門的數據卷容器,而後其它容器直接掛載該數據卷。
docker run -itd -v /data/ --name tetsvol centos bash#這裏的/data/是容器的/data目錄,並不是宿主機的/data目錄;testvol是自定義的數據卷容器名字
docker run -itd --volumes-from testvol centos7 bash#這裏是centos7鏡像的容器掛載centos鏡像的容器目錄
另外,若是每臺機器想要掛載的數據卷不同,能夠作軟連接,對想要掛載的目錄作軟連接,連接到同一個指定的目錄便可。
首先咱們想要備份的數據在數據卷中,新開一個容器掛載數據卷,同時須要把本地目錄掛載到新開的容器目錄中,這樣在新的容器目錄中新建的數據咱們就能夠直接在本地目錄中看到,接下來只要把要備份的數據打包放到新容器中目錄下就能夠了。
mkdir /data/backup #本地新建掛載目錄docker run --volumes-from testvol -v /data/backup/:/backup centos tar cvf /backup/data.tar /data# testvol爲數據卷容器名字;-v 指定掛載目錄;centos 爲新的容器鏡像;tar打包,/data爲要備份的目錄
過程與上面相反,先創建一個數據卷容器,再創建一個新的容器並掛載該數據卷容器,而後再把tar包放到掛載目錄下解包。
docker run -itd -v /data/ --name testvol2 centos bash #新建數據卷容器docker run --volumes-from testvol2 -v /data/backup:/backup centos tar xvf /backup/data.tar #掛載數據卷新建容器,並解包
更多資料參考: