镜像
镜像是Docker三大核心概念中最重要的,Docker运行容器前需要本地存在对应的镜像,如果镜像不存在,Docker会尝试从默认镜像仓库下载,用户也可以通过配置,使用自定义的镜像仓库。
1.获取镜像
docker [image] pull NAME[:TAG]
其中NAME是镜像仓库名称,TAG是镜像的标签。如果不显式指定TAG,则默认会选择latest标签。
[root@VM_0_8_centos ~]# docker pull ubuntu:18.04
Trying to pull repository docker.io/library/ubuntu ...
18.04: Pulling from docker.io/library/ubuntu
5bed26d33875: Pull complete
f11b29a9c730: Pull complete
930bda195c84: Pull complete
78bf9a5ad49e: Pull complete
Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d
Status: Downloaded newer image for docker.io/ubuntu:18.04
可以看到,镜像文件一般由若干层(layer)组成,比如5bed26d33875
是层的唯一id,当多个镜像包括相同的层时,本地仅存储层的一部分,从而可以减少存储空间。
如果从非官方的仓库下载,则需要在仓库名称前面指定完整的仓库地址。例如:
docker pull hub.c.163.com/public/ubuntu:18.04
2.查看镜像信息
2.1 使用images
命令列出镜像
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 18.04 4e5021d210f6 2 weeks ago 64.2 MB
docker.io/centos 7.2.1511 9aec5c5fe4ba 13 months ago 195 MB
docker.io/ansible/centos7-ansible latest 688353a31fde 3 years ago 447 MB
[root@VM_0_8_centos ~]#
2.2 使用tag
,命令添加镜像标签
[root@VM_0_8_centos ~]# docker tag ubuntu:18.04 myubuntu:18.04
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 18.04 4e5021d210f6 2 weeks ago 64.2 MB
myubuntu 18.04 4e5021d210f6 2 weeks ago 64.2 MB
实际上指向同一个镜像文件,只是别名不同而已。
2.3 使用inspect
命令查看详细信息
[root@VM_0_8_centos ~]# docker inspect ubuntu:18.04
[
{
"Id": "sha256:4e5021d210f65ebe915670c7089120120bc0a303b90208592851708c1b8c04bd",
"RepoTags": [
"docker.io/ubuntu:18.04",
"myubuntu:18.04"
],
"RepoDigests": [
"docker.io/ubuntu@sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d"
],
3.搜寻镜像
docker search [option] keyword
❑ -f, --filter filter:过滤输出内容;
❑ --format string:格式化输出内容;
❑ --limit int:限制输出结果个数,默认为25个;
❑ --no-trunc:不截断输出结果。
例如,搜索官方提供的带nginx关键字的镜像:
[root@VM_0_8_centos ~]# docker search --filter=is-official=true nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 12946 [OK]
4.删除和清理镜像
4.1 使用标签删除镜像
docker rmi IMAGE [IMAGE...]
❑ -f, -force:强制删除镜像,即使有容器依赖它;
❑ -no-prune:不要清理未带标签的父镜像
例如,删除myubuntu:18:04镜像,可以使用如下命令
[root@VM_0_8_centos ~]# docker rmi myubuntu:18.04
Untagged: myubuntu:18.0
4.2 使用镜像ID来删除镜像
docker rmi [ID]
4.3 清理镜像
使用Docker一段时间后,系统可能会遗留一些临时的镜像文件,以及一些没有被使用的镜像,可以通过docker image prune
命令来进行清理。
docker image prune
❑ -a, -all:删除所有无用镜像,不光是临时镜像;
❑ -filter filter:只清理符合给定过滤器的镜像;
❑ -f, -force:强制删除镜像,而不进行提示确认。
5.创建镜像
5.1 基于已有容器创建
docker [container] commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
❑ -a, --author="":作者信息;
❑ -c, --change=[]:提交的时候执行Dockerfile指令,包括CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR等;
❑ -m, --message="":提交消息;
❑ -p, --pause=true:提交时暂停容器运行。
下面演示如何使用该命令创建一个新镜像。
首先启动一个镜像,并在其中进行修改操作。
[root@VM_0_8_centos ~]# docker run -it ubuntu:18.04 /bin/bash
root@3ae087767bbd:/# touch test
root@3ae087767bbd:/# exit
exit
记住容器ID为3ae087767bbd
现在这个容器中镜像与Ubuntu:18.04相比,已经发生了一些改变,所以可以使用docker [container] commit命令来提交为一个新的容器。
[root@VM_0_8_centos ~]# docker commit -m "Added a new file" -a "Docker Newbee" 3ae087767bbd test:0.1
sha256:1bdfc9d4d51c501a2a4a27cbe95d995b2ae123cecbe82296133aed094df2d93e
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 1bdfc9d4d51c 13 seconds ago 64.2 MB
5.2 基于本地模板导入
docker [image] import [OPTIONS] file|URL|-[REPOSITORY[:TAG]]
5.3 基于Dockerfile创建
Dockerfile是一个文本文件,利用给定的指令描述基于某个父镜像创建新镜像的过程。
6.存出和载入镜像
6.1 存出镜像
如果要导出镜像到本地文件,可以使用docker [image] save
命令,这个命令支持-o、-output string
参数,可以导出镜像到指定的文件中。
[root@VM_0_8_centos ~]# docker save -o ubuntu_18.04.tar ubuntu:18.04
[root@VM_0_8_centos ~]# ll
total 65056
-rw-r--r-- 1 root root 0 Apr 3 10:34 init-config.yaml
-rw-r--r-- 1 root root 0 Apr 3 10:39 init.default.yaml
-rw------- 1 root root 66612224 Apr 8 22:45 ubuntu_18.04.tar
6.2 载入镜像
可以使用docker [image] load
将导出的tar文件再导入到本地镜像库中。支持-i、-input string
选项,从指定文件中读入镜像内容。
例如从文件ubunt_18.04.tar导入镜像到本地镜像列表:
[root@VM_0_8_centos ~]# docker load -i ubuntu_18.04.tar
Loaded image: ubuntu:18.04
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 18.04 2d6d7f5710f3 About an hour ago 66.6 MB
test 0.1 1bdfc9d4d51c 2 hours ago 64.2 MB
ubuntu 18.04 4e5021d210f6 2 weeks ago 64.2 MB
docker.io/ubuntu 18.04 4e5021d210f6 2 weeks ago 64.2 MB
docker.io/centos 7.2.1511 9aec5c5fe4ba 13 months ago 195 MB
docker.io/ansible/centos7-ansible latest 688353a31fde 3 years ago 447 MB
7.上传镜像
可以使用docker [image] push
命令上传镜像到仓库,默认上传到Docker Hub官方仓库。
docker [image] push NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]
不过需要先进行登录,之后登录信息会记录到本地~/.docker
目录下。