Docker介紹,安裝docker,映象管理,通過容器建立映象,使用模板建立映象
Docker介紹
官網 www.docker.com github https://github.com/docker/docker.github.io 開源的容器引擎,可以讓開發者打包應用以及依賴的庫,然後釋出到任何流行的linux發行版上,移植很方便 由go語言編寫,基於apache2.0協議釋出 基於linux kernel,要想在win下執行需要藉助一個vm(虛擬機器)來實現 自2013年開始,近些年發展迅猛 docker從1.13x開始,版本分為社群版ce和企業版ee,並且基於年月的時間線形式,當前最新穩定版為17.09 參考 http://blog.csdn.net/chenhaifeng2016/article/details/68062414
docker和傳統的虛擬化比較
docker核心概念:
映象,是一個只讀的模板,類似於安裝系統用到的那個iso檔案,我們通過映象來完成各種應用的部署。
容器,映象類似於作業系統,而容器類似於虛擬機器本身。它可以被啟動、開始、停止、刪除等操作,每個容器都是相互隔離的。
倉庫,存放映象的一個場所,倉庫分為公開倉庫和私有倉庫。 最大的公開倉庫是Docker hub(hub.docker.com),國內公開倉庫(dockerpool.com)
安裝docker
先下載一個repo的yum源:
[[email protected] ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
不下載yum源也可以用本地的yum安裝,只不過會版本比較老。
安裝docker:
[[email protected] ~]# yum install -y docker-ce
啟動docker:
[[email protected] ~]# systemctl start docker [[email protected] ~]# ps aux |grep docker root 3168 4.6 5.0 592564 50040 ? Ssl 12:53 0:00 /usr/bin/dockerd root 3174 1.6 2.7 317248 27720 ? Ssl 12:53 0:00 docke-containerd --config /var/run/docker/containerd/containerd.toml root 3316 0.0 0.0 112680 976 pts/0 R+ 12:53 0:00 grep --color=auto docker
docker會在啟動時候自動新增一些iptables規則,最好將iptables規則儲存起來。而且,作為docker伺服器,一般不要動這臺機器上的iptables規則。一旦弄疵了規則,docker服務就要重啟,重啟完服務還得重啟容器。
映象管理
docker下載一個centos映象:
[[email protected] ~]# docker pull centos
這個是在docker官方下載的映象。在國外,可能下載速度回比較慢。可以通過配置docker加速器加快下載速度。
配置docker加速器:
[[email protected] ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
這個URL是加速器地址,可以到阿里雲申請。
配置完成之後。從起docker服務,下載映象就會快些。
檢視映象:
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
名字 , 標籤 , 唯一標識 ,建立時間 ,大小 搜尋一下映象:
[[email protected] ~]# docker search jumpserver
這個就可以根據你得需要,去選擇下載映象。
給映象打標籤:
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
[[email protected] ~]# docker tag centos shuailinux_centos
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
shuailinux_centos latest 5182e96772bf 2 months ago 200MB
[[email protected] ~]# docker tag centos tset:111
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
shuailinux_centos latest 5182e96772bf 2 months ago 200MB
tset 111 5182e96772bf 2 months ago 200MB
centos latest 5182e96772bf 2 months ago 200MB
把映象啟動成容器:
[[email protected] ~]# docker run -itd centos
cf4d1603dde4724819fda9a54eaacc96ae2702dcb0aec61e8a05f0e804d18095
-i : 讓容器的標準輸入開啟 -t : 分配一個偽終端 -d : 標識後臺啟動
檢視執行的容器,-a : 檢視所有容器
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf4d1603dde4 centos "/bin/bash" 12 seconds ago Up 4 seconds gracious_lalande
刪除指定映象:
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
shuailinux_centos latest 5182e96772bf 2 months ago 200MB
tset 111 5182e96772bf 2 months ago 200MB
[[email protected] ~]# docker rmi tset
Error: No such image: tset
[[email protected] ~]# docker rmi tset:111
Untagged: tset:111
[[email protected] ~]# docker rmi shuailinux_centos
Untagged: shuailinux_centos:latest
通過容器建立映象
進入到一個啟動的容器裡:
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf4d1603dde4 centos "/bin/bash" 6 hours ago Up 6 hours gracious_lalande
[[email protected] ~]# docker exec -it cf4d16 bash
[[email protected] /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
在容器裡面,也是可以執行一些命令,還可以使用yum.
[[email protected] /]# ifconfig
bash: ifconfig: command not found
[[email protected] /]# ip add
bash: ip: command not found
[[email protected] /]# yum install -y net-tools
這個IP是docker自己搞得IP
[[email protected] /]# 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 5529 bytes 13443112 (12.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3683 bytes 202645 (197.8 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
你在本機系統裡檢視網絡卡會有docker專用的網絡卡。
[[email protected] ~]# ifconfig
docker0: 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:1ff:fe28:f453 prefixlen 64 scopeid 0x20<link>
ether 02:42:01:28:f4:53 txqueuelen 0 (Ethernet)
RX packets 3683 bytes 151083 (147.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5521 bytes 13442464 (12.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth0b66efc: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::4097:2fff:fe59:66d7 prefixlen 64 scopeid 0x20<link>
ether 42:97:2f:59:66:d7 txqueuelen 0 (Ethernet)
RX packets 3683 bytes 202645 (197.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5529 bytes 13443112 (12.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
把容器建立成映象: docker commit -m “change somth” -a “somebody info” container_id new_image_name //container_id通過docker ps -a獲取,後面的new_image_name為新映象名字 例如: docker commit -m “install net-tools” -a “Aming” 2c74d574293f centos_with_nettool 這個命令有點像svn的提交,-m 加一些改動資訊,-a 指定作者相關資訊 2c74d這一串為容器id,再後面為新映象的名字
[[email protected] ~]# docker commit -m "install net-tools" -a "shuai" cf4d1603dde4 centos-with-net
sha256:cea798f6bbf6f28ed79d4eef50ee18cdde1a6e9aad7ed03931b8c644d8b37393
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-with-net latest cea798f6bbf6 11 seconds ago 299MB
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
將新建的映象執行起來:可以直接執行ifconfig命令
[[email protected] ~]# docker run -itd centos-with-net bash
8749952da5172bf1bb3e72e577b501b45bd377311a874c95c12f3718b8909f1a
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8749952da517 centos-with-net "/bin/bash" 14 seconds ago Up 7 seconds cranky_swartz
cf4d1603dde4 centos "/bin/bash" 7 hours ago Up 7 hours gracious_lalande
[[email protected] ~]# docker exec -it 8749952da517 bash
[[email protected] /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
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
docker使用模板建立映象
先下載一個模板:
[[email protected] ~]# wget https://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz
把模板導成映象檔案:
[[email protected] ~]# cat centos-6-x86-minimal.tar.gz |docker import - centos6
sha256:64326114b1eff608e49f436acd1a4058478b6ebbd5c222368e26bc6352228b87
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6 latest 64326114b1ef 18 seconds ago 512MB
centos-with-net latest cea798f6bbf6 About an hour ago 299MB
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
啟動製作的映象:
[[email protected] ~]# docker run -itd centos6 bash
0850110acb986d32e365206446e133c4a94d1f00ee2e3926d5ef9a9578b8e646
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0850110acb98 centos6 "bash" 17 seconds ago Up 10 seconds compassionate_cori
8749952da517 centos-with-net "/bin/bash" About an hour ago Up About an hour cranky_swartz
cf4d1603dde4 centos "/bin/bash" 8 hours ago Up 8 hours gracious_lalande
[[email protected] ~]# docker exec -it 0850110acb98 bash
把映象匯出來為一個檔案:
[[email protected] ~]# docker save -o centos7_with_nettools.tar centos-with-net
[[email protected] ~]# du -sh centos7_with_nettools.tar
294M centos7_with_nettools.tar
用檔案恢復映象:
刪掉映象前要先停掉容器
[[email protected] ~]# docker rm -f 8749952da517
8749952da517
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6 latest 64326114b1ef 14 minutes ago 512MB
centos-with-net latest cea798f6bbf6 About an hour ago 299MB
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
[[email protected] ~]# docker rmi cea798f6bbf6
Untagged: centos-with-net:latest
Deleted: sha256:cea798f6bbf6f28ed79d4eef50ee18cdde1a6e9aad7ed03931b8c644d8b37393
Deleted: sha256:fabc68085404013af1f36dc08e6449d536557206ff47fc4e1565c4702d3659d9
從儲存檔案中匯入映象:
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6 latest 64326114b1ef 14 minutes ago 512MB
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
[[email protected] ~]# docker load < centos7_with_nettools.tar
c9e25e335de2: Loading layer 99.07MB/99.07MB
Loaded image: centos-with-net:latest
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6 latest 64326114b1ef 16 minutes ago 512MB
centos-with-net latest cea798f6bbf6 About an hour ago 299MB
ubuntu latest cd6d8154f1e1 4 weeks ago 84.1MB
centos latest 5182e96772bf 2 months ago 200MB
docker push image_name //可以把自己的映象傳到dockerhub官方網站上去,但前提是需要先註冊一個使用者