一. 獲取映象
1.1. docker pull
映象是執行容器的前提,也就是說沒有映象就沒有辦法建立容器
獲取映象的命令:
docker pull
這個命令可以直接在docker Hub映象源下載映象
該命令的格式是:
docker pull NAME[:TAG]
其中name是映象倉庫的名稱(用來區分映象)tag是映象的標籤(用來標註版本),通常情況下我們描述一個映象需要包括"名稱+標籤"資訊
我們pull一個centos的映象
[root@rsync131 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Downloading [==========> ] 15.75MB/74.69MB 這裡我沒有指定標籤,它的下載的預設標籤是latest
下載映象的過程解析:
下載過程我們可以看出,映象檔案一般由若干層組成 256b176beaff 這樣的串是這個映象的唯一ID(這裡只是一個短的ID,實際上完整的ID包括256位元,由64個十六進位制字元組成)。使用docker pull 命令下載時會獲取並輸出映象的各層資訊。當不同的映象包括相同的層時,本地僅儲存層的一份內容,減小的儲存需要的空間。
pull 子命令支援的選項主要包括:
-a,--all-tags=true|false:是否獲取倉庫中的所有映象,預設是否。
二. 檢視映象資訊
2.1. docker images
使用docker images 命令可以列出本地主機上的已有映象基本資訊
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB REPOSITORY:來自哪個倉庫
TAG:映象的標籤
IMAGE ID:映象的唯一ID
CREATED:建立的時間
SIZE:映象的大小 其中映象的ID資訊是十分重要的,因為它是映象的唯一值
images子命令主要支援的選項如下:
-a,--all=true|false:列出所有的映象檔案(包括臨時檔案),預設為否
--digests=true|false:列出映象的數字摘要值,預設為否
-f,--filter=[]:過濾列出的映象,如dangling=true只顯示沒有被使用的映象
--format="TEMPLATE":控制輸出格式,如.ID代表ID資訊
--no-trunc=true|false:對輸出格式中太長的部分是否進行截斷,預設為是
-q,--quiet=true|false:僅輸出ID資訊,預設為否 更多的子命令可以通過man docker-images來檢視
2.2. docker tag
使用docker tag 可以給映象新增標籤資訊
[root@rsync131 ~]# docker tag centos:latest mycentos:1.0
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
mycentos 1.0 5182e96772bf 6 weeks ago 200MB 我們可以看到mycentos和centos的ID、大小完全一致,實際上他們就是一個映象檔案,只是個別名而已,docker tag 就相當於Linux建立的連結檔案
2.3. docker inspect
使用docker inspect命令可以檢視映象的詳細資訊,包括製作者、適應架構、各層的數字摘要等很多資訊
[root@rsync131 ~]# docker inspect centos
[
{
"Id": "sha256:5182e96772bf11f4b912658e265dfe0db8bd314475443b6434ea708784192892",
"RepoTags": [
"centos:latest",
"mycentos:1.0"
],
"RepoDigests": [
"centos@sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf"
],
"Parent": "",
"Comment": "",
"Created": "2018-08-06T19:21:48.235227329Z",
"Container": "d60ffc9ddd12462af4bdcdbe45b74f3b3f99b46607ada80c3ed877b7def84250",
"ContainerConfig": {
"Hostname": "d60ffc9ddd12",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:748eacc0f236df2fc9ba87c4d76a66cb10742120387e99e2acdb9454915c841d",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20180804",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "17.06.2-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:748eacc0f236df2fc9ba87c4d76a66cb10742120387e99e2acdb9454915c841d",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20180804",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 199723824,
"VirtualSize": 199723824,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/8d76bee4ae910273382e69f526ede7bbab349f6d0f04e6dc81a46c13f9f0fa92/merged",
"UpperDir": "/var/lib/docker/overlay2/8d76bee4ae910273382e69f526ede7bbab349f6d0f04e6dc81a46c13f9f0fa92/diff",
"WorkDir": "/var/lib/docker/overlay2/8d76bee4ae910273382e69f526ede7bbab349f6d0f04e6dc81a46c13f9f0fa92/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:1d31b5806ba40b5f67bde96f18a181668348934a44c9253b420d5f04cfb4e37a"
]
},
"Metadata": {
"LastTagTime": "2018-09-22T12:25:06.648773262+08:00"
}
}
]
其實返回的就是一個json格式的資訊,我們也可以用 -f 引數來獲取指定的資訊
[root@rsync131 ~]# docker inspect -f {{".Architecture"}} centos
amd64
2.4. docker history
使用docker history命令可以檢視映象的歷史資訊
既然映象檔案是由多層組成的,那麼怎麼知道每一層都幹了什麼呢,可以使用history命令來檢視
[root@rsync131 ~]# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
cd6d8154f1e1 2 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 2 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$… 2.76kB
<missing> 2 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 2 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:3df374a69ce696c21… 84.1MB 這裡有一些長的命令被自動截斷了,可以使用--no-trunc選項來顯示完整的資訊,太長了還是自己試一試吧 沒辦法把輸出貼上進來
三. 搜尋映象
3.1. docker search
使用docker search命令可以搜尋遠端倉庫的共享映象,預設是搜尋官方倉庫中的映象檔案
語法格式: docker search TERM 支援的選項包括: --automated=true|false:僅顯示自動建立的映象,預設為否 --no-trunc=true|false:輸出資訊不截斷顯示,預設為否 -s,--stars=X:指定僅顯示評論為指定星級以上的映象,預設為0,即輸出所有的進映象
搜尋所有自動建立評價為1+的帶nginx的映象
[root@rsync131 ~]# docker search --automated -s 3 nginx
Flag --automated has been deprecated, use --filter=is-automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1416 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 615 [OK]
jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 414 [OK]
webdevops/php-nginx Nginx with PHP-FPM 113 [OK]
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 68 [OK]
bitnami/nginx Bitnami nginx Docker Image 58 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 44 [OK]
tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 20 [OK]
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK]
wodby/drupal-nginx Nginx for Drupal container image 10 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 8 [OK]
webdevops/nginx Nginx container 8 [OK]
1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
behance/docker-nginx Provides base OS, patches and stable nginx f… 3 [OK]
四. 刪除映象
4.1. docker rmi
使用docker rmi 命令可以刪除指定的映象檔案
語法格式:
docker rmi IMAGE [IMAGE....] PS:當本地一個映象有多個標籤的時候,docker rmi 只會刪除指定的標籤映象而已 支援的選項引數:
-f :強制刪除映象檔案
通過標籤刪除
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
mycentos 1.0 5182e96772bf 6 weeks ago 200MB
centos latest 5182e96772bf 6 weeks ago 200MB
[root@rsync131 ~]# docker rmi mycentos:1.0
Untagged: mycentos:1.0
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
通過ID刪除
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
[root@rsync131 ~]# docker rmi 5182e96772bf
Untagged: centos:latest
Untagged: centos@sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Deleted: sha256:5182e96772bf11f4b912658e265dfe0db8bd314475443b6434ea708784192892
Deleted: sha256:1d31b5806ba40b5f67bde96f18a181668348934a44c9253b420d5f04cfb4e37a
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
強制刪除
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
[root@rsync131 ~]# docker rmi -f 5182e96772bf
Untagged: centos:latest
Untagged: centos@sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Deleted: sha256:5182e96772bf11f4b912658e265dfe0db8bd314475443b6434ea708784192892
Deleted: sha256:1d31b5806ba40b5f67bde96f18a181668348934a44c9253b420d5f04cfb4e37a
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
五. 建立映象
其實建立映象有三種方式,基於已有的映象的容器建立,基於本地模板匯入,基於Dockerfile建立,這裡只介紹下前兩種,因為dockerfile是一個很高階的東西會有專門的文章來介紹:點選Dockerfile
5.1. docker commit
docker commit 是基於已有的映象的容器建立
語法格式:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] 支援的選項包括:
-a,--author="":作者的資訊
-c,--change=[]:提交的時候執行的Dockerfile指令,包括CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR等
-m,--message="":提交的資訊
-p,--pause=true:提交時暫停容器執行
因為是基於容器建立的映象,所以要先建立個容器
# 建立個容器,touch個檔案,這裡相對源映象已經發生了改變 c86d860188f3 就是容器ID
[root@rsync131 ~]# docker run -it centos bash
[root@c86d860188f3 /]# touch a.txt
[root@c86d860188f3 /]# exit
exit # 建立映象基於c86d860188f3 容器
[root@rsync131 ~]# docker commit -m "add a.txt" -a "zhujingzhi" c86d860188f3 a_centos:1.0
sha256:64b8d4f3a1901353deeb31e8b646c0f04dd58accddc951814fdce237e2097a6f # 檢視映象
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
a_centos 1.0 64b8d4f3a190 19 seconds ago 200MB
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
5.2. docker import
也可以直接從一個作業系統檔案匯入一個映象,只要使用docker import命令
語法格式: docker import [OPTIONS] file|URL|-[REPOSITORY[:TAG]]
給朋友們個連結下載模板:https://download.openvz.org/template/precreated/
[root@rsync131 ~]# ll
總用量 345664
-rw-------. 1 root root 1513 8月 20 20:25 anaconda-ks.cfg
-rw-r--r-- 1 root root 145639219 9月 22 14:12 centos-7-x86_64-minimal.tar.gz [root@rsync131 ~]# cat centos-7-x86_64-minimal.tar.gz | docker import - centos-mini:1.0
sha256:4dec53e6e02af646126879ac69c8bee4ef6bdcf9fe0cc86d3a92959ce2e0b9cd [root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-mini 1.0 4dec53e6e02a 4 seconds ago 435MB
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
六. 映象的匯出和匯入
6.1. docker save
使用docker save 可以把映象匯出
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
a_centos 1.0 64b8d4f3a190 17 minutes ago 200MB
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB
[root@rsync131 ~]# docker save -o centos_bak.tar.gz centos
[root@rsync131 ~]# ll
總用量 203436
-rw-------. 1 root root 1513 8月 20 20:25 anaconda-ks.cfg
-rw------- 1 root root 208301056 9月 22 13:47 centos_bak.tar.gz
6.2. docker load
使用docker load 可以把使用docker save匯出的檔案匯入
[root@rsync131 ~]# docker load --input centos_bak.tar.gz
或者
[root@rsync131 ~]# docker load < centos_bak.tar.gz
1d31b5806ba4: Loading layer [==================================================>] 208.3MB/208.3MB
Loaded image: centos:latest
[root@rsync131 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
centos latest 5182e96772bf 6 weeks ago 200MB