更换Docker源
修改Docker配置文件/etc/default/docker,在最后增加1
DOCKER_OPTS="--registry-mirror=http://aad0405c.m.daocloud.io"
重启Docker1
service docker restart
获取镜像
通常情况下,描述一个镜像需要包括“名称” + “标签”。
例如:获得一个Ubuntu 14.04的系统镜像【在进行拉取之前先更改docker源。否则速度很慢】1
docker pull ubuntu:14.04
如果不指定TAG,那么会默认选择latest标签,例如:1
docker pull ubuntu
一般来说,镜像的latest标签代表着镜像是最新的,也就是说可能存在不稳定的情况。在生产环境中应该避免这种镜像。
下载过程分析1
2
3
4
5
6
7
8
9
10root@ubuntu:~# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
43cff9ac2d34: Pull complete
d25abb784368: Pull complete
7355f8e4aeb7: Pull complete
ea422b11e5e1: Pull complete
a4f1088e42dc: Pull complete
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:latest
从上面可以看出镜像由若干层组成,43cff9ac2d34这样的就代表这层的ID。这里特别要注意,当不同的镜像包含相同的层时,本地仅存一份内容,这样做的目的是为了减少需要的存储空间。
pull的时候,默认的是使用Docker Hub仓库上的镜像,我们称之为官方仓库。如果要从非官方仓库下载镜像,则需要加上仓库地址,例如:1
docker pull hub.c.163.com/public/ubuntu:14.04
查看镜像信息
列出镜像
1 | root@ubuntu:~# docker images |
REPOSITORY: 来自哪个仓库
TAG:镜像的标签
IMAGE ID:镜像的唯一标识【这个是可以重复的,重复说明他们指向同一个镜像】
CREATED:镜像最后的更新时间
SIZE:镜像的大小【越小越好,由于相同的镜像层本地只会存储一份,所以,物理上占用的存储空间会小于各镜像的逻辑体积之和】
其他命令选项
-a:列出所有的镜像文件,包含临时文件,默认为否
-f:过滤镜像,例如dangling=true,代表没有被使用的镜像。
-q:只显示镜像的ID
1 | docker images -a |
添加标签
先查看现有的镜像信息1
2
3
4
5root@ubuntu:~# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest adfebeb0ac91 3 weeks ago 111 MB
ubuntu 14.04 a35e70164dfb 3 weeks ago 222 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 2 years ago 237 MB
可以使用docker tag来为本次镜像添加新的标签。例如,以标签为ubuntu:latest的镜像为基础,新建一个名为rexyan_ubuntu:v1的镜像。1
docker tag ubuntu:latest rexyan_ubuntu:v1
查看结果1
2
3
4
5
6root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rexyan_ubuntu v1 adfebeb0ac91 3 weeks ago 111 MB
ubuntu latest adfebeb0ac91 3 weeks ago 111 MB
ubuntu 14.04 a35e70164dfb 3 weeks ago 222 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 2 years ago 237 MB
查看镜像详情
例如:查看标签为ubuntu:14.04的镜像的信息1
docker inspect ubuntu:14.04
输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86[
{
"Id": "sha256:a35e70164dfb0dd7a9fec8be0b00017a2277c5a221dcab8ffa75f876ed0aae3d",
"RepoTags": [
"ubuntu:14.04"
],
"RepoDigests": [
"ubuntu@sha256:0661e2dbc6072eadfd48c0c972681ffc11ca8ea5d0a9f5f4227df7d653f649fd"
],
"Parent": "",
"Comment": "",
"Created": "2018-03-06T22:16:52.079792907Z",
"Container": "e2d9db43ceab9935f29c6147c73a164eaaf32aecc1b0cb3285255cc1c293ea78",
"ContainerConfig": {
"Hostname": "e2d9db43ceab",
"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:b8c20fc2a0d0df0287d60b6d3065b10d91c200fa50309a0085c44ecc4e9faa22",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"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:b8c20fc2a0d0df0287d60b6d3065b10d91c200fa50309a0085c44ecc4e9faa22",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 222372215,
"VirtualSize": 222372215,
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:92fb50b4d953c72eb1d5c045bab47a78f5c02348d8f004f7229a7b8fef16608d",
"sha256:e95051d9cb9b95bf46df6cf5affab21157019659b3405e9c69f1f4a8376b24e7",
"sha256:3abb69fb15dc83c0bf7a562f7bda73844fd20ed8cc4aec7b28ebd4113797cd9a",
"sha256:dd420adea0d3f9f9da8500d4a8ec542ae0a32e78de557c9ce3755bd416431b5a",
"sha256:65262d4f5516e81457c1ad5e14da8fbf66a999557107bbc7c2a635cbf94e64d9"
]
}
}
]
这样列出的是全部的信息,如果我们只看一部分指定信息,可以使用下面命令。下面命令查看RootFS下的Type1
docker inspect ubuntu:14.04 -f {{".RootFS"."Type"}}
列出镜像文件层级信息
镜像都是由多个层组成,我们可以使用docker history来查看各层的信息1
docker history ubuntu:14.04
搜索镜像
我们可以查询官方仓库的上的镜像1
docker search nginx
结果1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 8262 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1300 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 540 [OK]
jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as... 336 [OK]
kong Open-source Microservice & API Management ... 170 [OK]
webdevops/php-nginx Nginx with PHP-FPM 97 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demons... 95
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 48 [OK]
bitnami/nginx Bitnami nginx Docker Image 45 [OK]
linuxserver/nginx An Nginx container, brought to you by Linu... 33
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 31 [OK]
tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 19 [OK]
wodby/drupal-nginx Nginx for Drupal container image 9 [OK]
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 8 [OK]
nginxdemos/nginx-ingress NGINX Ingress Controller for Kubernetes 8
webdevops/nginx Nginx container 8 [OK]
centos/nginx-18-centos7 Platform for running nginx 1.8 or building... 6
nginxdemos/hello NGINX webserver that serves a simple page ... 5 [OK]
1science/nginx Nginx Docker images that include Consul Te... 4 [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ng... 2 [OK]
behance/docker-nginx Provides base OS, patches and stable nginx... 2 [OK]
toccoag/openshift-nginx Nginx reverse proxy for Nice running on sa... 1 [OK]
travix/nginx NGinx reverse proxy 1 [OK]
mailu/nginx Mailu nginx frontend 0 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK]
其他参数
可以使用–filter 后跟条件来进行查询。【例如:查询nginx镜像,且只显示自动创建的镜像,其他的选项还有 is-official、stars,可以根据是否是官方的,以及评论的等级进行过滤】默认的查询结果是按照星级评价去排序的。1
docker search --filter is-automated=true nginx
删除镜像
使用标签删除
查看所有的镜像1
2
3
4
5
6root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rexyan_ubuntu v1 adfebeb0ac91 3 weeks ago 111 MB
ubuntu latest adfebeb0ac91 3 weeks ago 111 MB
ubuntu 14.04 a35e70164dfb 3 weeks ago 222 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 2 years ago 237 MB
使用标签删除rexyan_ubuntu:v1的镜像:【这里注意,rexyan_ubuntu:v1和ubuntu:latest的IMAGE ID相同,也就是说他们指向的是同一个镜像。我们这时删除rexyan_ubuntu:v1它并不会将IMAGE ID为adfebeb0ac91的镜像删除,因为还有一个镜像】1
docker rmi rexyan_ubuntu:v1
结果:1
2
3
4
5root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest adfebeb0ac91 3 weeks ago 111 MB
ubuntu 14.04 a35e70164dfb 3 weeks ago 222 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 2 years ago 237 MB
如果这个时候再将IMAGE ID为adfebeb0ac91的镜像删除,那么这个镜像就被真正的删除了。下面所示,删除了镜像文件的所有层。1
2
3
4
5
6
7
8
9root@ubuntu:~# docker rmi ubuntu:latest
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Deleted: sha256:adfebeb0ac9194caaa37b52b84e6ff09b858f8daacf19ed46eced547284fc82d
Deleted: sha256:e366e66208ed5e77a9ed40bfc7b782bcdf30a52b40065104abfa0c818958b2fd
Deleted: sha256:f768a58d55c43e1a30e5517565010c2f151bdae2ad50c015a3f0d703e36b711f
Deleted: sha256:dbffe316628589cf9fdbeec1132a8e3e165667353c09184cf4835a375ff0bb92
Deleted: sha256:3f9b143e33c3558e297033eaae7997e555a8c00664f198953048c8a6a57e745f
Deleted: sha256:5c610defde1afc14c5049019cfdd7e34cbb8703d38e9001aaf1d595ba39430b1
再次查看结果:1
2
3
4root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 a35e70164dfb 3 weeks ago 222 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 2 years ago 237 MB
使用镜像ID删除
当你删除的ID是被多个镜像指向的时候是无法删除的。还有就是当这个镜像已经被用来创建容器,那么也是不能删除的,需先使用docker rm删除容器,然后在使用dcker rmi 删除镜像
先查看所有镜像1
2
3
4
5
6root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 f975c5035748 3 weeks ago 112 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 2 years ago 237 MB
rexyan v2 2fe5c4bba1f9 2 years ago 237 MB
rexyan v3 2fe5c4bba1f9 2 years ago 237 MB
删除被多个指向的镜像,可以看见不能删除1
2root@ubuntu:~# docker rmi 2fe5c4bba1f9
Error response from daemon: conflict: unable to delete 2fe5c4bba1f9 (must be forced) - image is referenced in multiple repositories
当删除只有一个指向的镜像,这样就不存在问题,可以直接删除1
2
3
4
5
6
7
8
9root@ubuntu:~# docker rmi f975c5035748
Untagged: ubuntu:16.04
Untagged: ubuntu@sha256:52286464db54577a128fa1b1aa3c115bd86721b490ff4cbd0cd14d190b66c570
Deleted: sha256:f975c50357489439eb9145dbfa16bb7cd06c02c31aa4df45c77de4d2baa4e232
Deleted: sha256:0bd983fc698ee9453dd7d21f8572ea1016ec9255346ceabb0f9e173b4348644f
Deleted: sha256:08fe90e1a1644431accc00cc80f519f4628dbf06a653c76800b116d3333d2b6d
Deleted: sha256:5dc5eef2b94edd185b4d39586e7beb385a54b6bac05d165c9d47494492448235
Deleted: sha256:14a40a140881d18382e13b37588b3aa70097bb4f3fb44085bc95663bdc68fe20
Deleted: sha256:a94e0d5a7c404d0e6fa15d8cd4010e69663bd8813b5117fbad71365a73656df9
创建一个容器1
root@ubuntu:~# docker run ubuntu:16.04 echo "test"
查看所有容器1
2
3
4root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72b4b205a190 ubuntu:16.04 "echo test" About a minute ago Exited (1) About a minute ago infallible_torvalds
f3847498e33e ubuntu:16.04 "echo test" About a minute ago Exited (1) About a minute ago angry_mahavira
尝试着删除用来用来创建容器的这个镜像,会看到提示被使用,应该停止这个容器1
2root@ubuntu:~# docker rmi f975c5035748
Error response from daemon: conflict: unable to delete f975c5035748 (must be forced) - image is being used by stopped container f3847498e33e
这时,我们有两个选择,使用-f参数强制删除【前面遇到的一个镜像被多个标签指向的情况也能用-f来强制删除】,或者先删除容器,后删除镜像
强制删除事例:1
docker rmi -f f975c5035748
另一种先删除容器【创建了两个,所以两个都要删除】1
2
3
4root@ubuntu:~# docker rm 72b4b205a190
72b4b205a190
root@ubuntu:~# docker rm f3847498e33e
f3847498e33e
在删除镜像,这样就成功删除了1
2
3
4
5
6
7
8
9root@ubuntu:~# docker rmi f975c5035748
Untagged: ubuntu:16.04
Untagged: ubuntu@sha256:7d715ef7f37de67f7a6489e783fc3ab9dc64041f6926126073bf4d0e6590dbc2
Deleted: sha256:f975c50357489439eb9145dbfa16bb7cd06c02c31aa4df45c77de4d2baa4e232
Deleted: sha256:0bd983fc698ee9453dd7d21f8572ea1016ec9255346ceabb0f9e173b4348644f
Deleted: sha256:08fe90e1a1644431accc00cc80f519f4628dbf06a653c76800b116d3333d2b6d
Deleted: sha256:5dc5eef2b94edd185b4d39586e7beb385a54b6bac05d165c9d47494492448235
Deleted: sha256:14a40a140881d18382e13b37588b3aa70097bb4f3fb44085bc95663bdc68fe20
Deleted: sha256:a94e0d5a7c404d0e6fa15d8cd4010e69663bd8813b5117fbad71365a73656df9
创建镜像
创建镜像的方法,除了这里介绍的这两种外,还有使用Dockerfile来创建的,后面再学习到。
基于已有镜像的容器创建
新建一个容器,并进入bash,并新建一个文件1
2
3root@ubuntu:~# docker run -it ubuntu:16.04 /bin/bash
root@943bd5b5f887:/# touch rexyan.text
root@943bd5b5f887:/# exit
查看当前启动、并创建文件的docker,记下id1
2
3root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
943bd5b5f887 ubuntu:16.04 "/bin/bash" About a minute ago Exited (0) About a minute ago festive_meitner
提交一个新的镜像,-m是提交信息,-a是作者信息,943bd5b5f887是我们启动并修改文件的容器,rexyan:v1的新创建的镜像的标签1
docker commit -m"add new file" -a"rexyan" 943bd5b5f887 rexyan:v1
查看镜像,会发现新增了一个,标签为rexyan:v11
2
3
4root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rexyan v1 cee5de10da32 About a minute ago 111 MB
ubuntu 16.04 adfebeb0ac91 3 weeks ago 111 MB
参数:
-m:提交信息
-a:作者信息
-c:提交的时候执行Dockerfile指令
-p:提交时候暂停容器运行
验证我们刚刚创建的镜像:使用刚刚创建的镜像来创建容器,看看里看看里面是否有rexyan.test这个文件即可。
基于本地模版导入
示例:1
cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu:14.04
存出和载入镜像
存出
1 | root@ubuntu:~# docker save -o ubuntu_export_file.tar.gz ubuntu:16.04 |
载入
删除所有的镜像,使得本地仓库为空1
2root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
将刚刚打包的文件加载到docker仓库1
2
3
4
5
6
7root@ubuntu:~# docker load --input ubuntu_export_file.tar.gz
5c610defde1a: Loading layer [==================================================>] 114.9 MB/114.9 MB
1cb600ff8c94: Loading layer [==================================================>] 15.87 kB/15.87 kB
199a30dc6a17: Loading layer [==================================================>] 13.82 kB/13.82 kB
cec5d9e51cf1: Loading layer [==================================================>] 5.632 kB/5.632 kB
64e8d47e7876: Loading layer [==================================================>] 3.072 kB/3.072 kB
Loaded image: ubuntu:16.04
查看结果1
2
3root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 adfebeb0ac91 3 weeks ago 111 MB
上传镜像
上传之前得有一个https://hub.docker.com的帐号,并且新建一个REPOSITORY
上传准备,修改tag信息。将本地仓库的ubuntu:16.04修改标签为yanrs/test:v1,其中yanrs是用户名,test是名称,v1是标签1
docker tag ubuntu:16.04 yanrs/test:v1
推送1
未成功
总结
1 | docker pull ubuntu:14.04 # pull ubuntu镜像,版本为14.04 |