镜像是Docker三大核心概念中最重要的,Docker运行容器前需要本地存在对应的镜像,如果镜像不存在,Docker会尝试先从默认镜像仓库下载,也可以通过配置,使用自定义的镜像仓库。
一、获取镜像
可以使用docker pull命令从Docker Hub镜像源下载镜像,该命令的使用方式如下:
root@ubuntu:~# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
其中,NAME是镜像的名称,TAG是镜像的标签(通常用来表示版本信息)。
例如,获取ubuntu的镜像可以使用如下命令:
root@ubuntu:~# docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
6cf436f81810: Pull complete
987088a85b96: Pull complete
b4624b3efe06: Pull complete
d42beb8ded59: Pull complete
Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Status: Downloaded newer image for ubuntu:18.04
如果不显示指定TAG,默认会选择latest标签,即最新版本的镜像,例如下面的命令将从Docker Hub下载最新版本的ubuntu镜像:
root@ubuntu:~# docker pull ubuntu
从下载过程可以看出,镜像一般由若干层(layer)组成,6cf436f81810这样的串是层的唯一id,使用docker pull命令下载会获取并输出镜像的各层信息。当不同的镜像包含相同的层时,本地仅存储了层的一份内容,减小了存储空间。
二、查看本地主机上已有镜像
2.1 查看本地镜像列表
使用docker images或docker image ls命令可以列出本地主机已有的镜像,命令的使用方式如下:
root@ubuntu:~# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
root@ubuntu:~# docker image ls --help
Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
ls, images, list
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
例如,下面的命令列出上一节下载的镜像:
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 47b19964fb50 4 weeks ago 88.1MB
从列出的信息中,可以看到几个字段信息:来自于哪个仓库、镜像的标签信息、镜像的id、创建时间、镜像大小。
2.2 使用inspect命令查看镜像的详细信息
使用docker inspect命令可以获取镜像的详细信息,该命令的使用方式如下:
root@ubuntu:~# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
例如,查看前面下载的镜像的详细信息:
root@ubuntu:~# docker inspect ubuntu:18.04
[
{
"Id": "sha256:47b19964fb500f3158ae57f20d16d8784cc4af37c52c49d3b4f5bc5eede49541",
"RepoTags": [
"ubuntu:18.04"
],
"RepoDigests": [
"ubuntu@sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210"
],
"Parent": "",
"Comment": "",
"Created": "2019-02-06T03:37:51.599085524Z",
"Container": "4fcabd24a4d0668c251f05105af1d986b1841713e7af90bf51df172ecefae2fa",
"ContainerConfig": {
"Hostname": "4fcabd24a4d0",
"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:5bbf4643e91ac20eb0ec60930e6d56e36eedab0ae09930a4bd414bb227590d28",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "18.06.1-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:5bbf4643e91ac20eb0ec60930e6d56e36eedab0ae09930a4bd414bb227590d28",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 88140808,
"VirtualSize": 88140808,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/e77fa659f0911646507378fdd1c3601fc35a31b759f1d6923aeb5f4042c2b373/diff:/var/lib/docker/overlay2/1dfa0fbfd53e61bfdcc1516529f52579989b1eb3698d25ab8007d2e2f6f3d3ef/diff:/var/lib/docker/overlay2/440db8e93ef09b94adb1f3d815f09e90590a9011c6300d5d455f60ca5ff49ce0/diff",
"MergedDir": "/var/lib/docker/overlay2/5d1d7ce0eba93061fae04844c3804abe5cf5f671dfef8d323876eb53b885499f/merged",
"UpperDir": "/var/lib/docker/overlay2/5d1d7ce0eba93061fae04844c3804abe5cf5f671dfef8d323876eb53b885499f/diff",
"WorkDir": "/var/lib/docker/overlay2/5d1d7ce0eba93061fae04844c3804abe5cf5f671dfef8d323876eb53b885499f/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:bebe7ce6215aee349bee5d67222abeb5c5a834bbeaa2f2f5d05363d9fd68db41",
"sha256:283fb404ea9415ab48456fd8a82b153b1a719491cdf7b806d1853b047d00f27f",
"sha256:663e8522d78b5b767f15b2e43885da5975068e3195bbbfa8fc3a082297a361c1",
"sha256:4b7d93055d8781d27259ba5780938e6a78d8ef691c94ee9abc3616c1b009ec4a"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
三、搜索镜像
使用docker search命令可以搜索Docker Hub官方仓库中的镜像,该命令的使用方式如下:
root@ubuntu:~# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
例如,搜索官方提供的带ubuntu关键字的镜像:
root@ubuntu:~# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 9239 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 273 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 205 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 156 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 96 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 95 [OK]
neurodebian NeuroDebian provides neuroscience research s… 56 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 49 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=m… 40 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK]
tutum/ubuntu Simple Ubuntu docker images with SSH access 19
i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 16
1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]
ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 12
eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 7 [OK]
codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 5 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK]
pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 2
1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
smartentry/ubuntu ubuntu with smartentry 1 [OK]
paasmule/bosh-tools-ubuntu Ubuntu based bosh-cli 1 [OK]
ossobv/ubuntu Custom ubuntu image from scratch (based on o… 0
pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0
1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 [OK]
1and1internet/ubuntu-16-rspec ubuntu-16-rspec 0 [OK]
四、删除和清理镜像
4.1、删除镜像
使用docker rmi或docker image rm命令可以删除镜像,命令的使用方式如下:
root@ubuntu:~# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
root@ubuntu:~# docker image rm --help
Usage: docker image rm [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Aliases:
rm, rmi, remove
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
例如,删除前面下载的ubuntu镜像:
root@ubuntu:~# docker rmi ubuntu:18.04
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Deleted: sha256:47b19964fb500f3158ae57f20d16d8784cc4af37c52c49d3b4f5bc5eede49541
Deleted: sha256:d4c69838355b876cd3eb0d92b4ef27b1839f5b094a4eb1ad2a1d747dd5d6088f
Deleted: sha256:1c29a32189d8f2738d0d99378dc0912c9f9d289b52fb698bdd6c1c8cd7a33727
Deleted: sha256:d801a12f6af7beff367268f99607376584d8b2da656dcd8656973b7ad9779ab4
Deleted: sha256:bebe7ce6215aee349bee5d67222abeb5c5a834bbeaa2f2f5d05363d9fd68db41
4.2、清理镜像
使用Docker一段时间后,系统中可能会遗留一些临时的镜像文件,以及一些没有被使用的镜像文件,可以通过docker image prune命令进行清理。改命令的使用方式如下:
root@ubuntu:~# docker image prune --help
Usage: docker image prune [OPTIONS]
Remove unused images
Options:
-a, --all Remove all unused images, not just dangling ones
--filter filter Provide filter values (e.g. 'until=<timestamp>')
-f, --force Do not prompt for confirmation
例如:
root@ubuntu:~# docker image prune -f
Total reclaimed space: 0B
五、创建镜像
创建镜像的方法主要有三种:基于已有镜像的容器创建、基于本地模板导入、基于Dockerfile创建。
5.1 基于已有容器创建
该方法主要使用docker commit命令,该命令的使用方式如下:
root@ubuntu:~# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
下面演示如何使用该命令创建一个新镜像。
首先,启动一个镜像,并在其中进行修改操作。例如,创建一个test文件,之后退出:
root@ubuntu:~# docker run -it ubuntu:18.04 /bin/bash
root@4801f79c5a19:/#
root@4801f79c5a19:/# touch test
root@4801f79c5a19:/#
root@4801f79c5a19:/# exit
此时,该容器与原ubuntu:18.04相比,已经发生了改变,可以使用docker commit命令来提交为一个新的镜像:
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4801f79c5a19 ubuntu:18.04 "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago condescending_clarke
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# docker commit 4801f79c5a19 test:0.1
sha256:fa2512ebcf705d925ae338e3410652e5aa75d37415803d92d66d455769490d4c
此时查看本地镜像列表,会看到新创建的镜像:
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 fa2512ebcf70 About a minute ago 88.1MB
ubuntu 18.04 47b19964fb50 4 weeks ago 88.1MB
5.2 基于本地模板导入
这种方式主要是使用docker import命令,该命令的使用方式如下:
root@ubuntu:~# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
5.3 基于Dockerfile文件创建
基于Dockerfile创建时最常见的方式。Dockerfile是一个文本文件,描述了使用指令基于某个父镜像创建新镜像的过程。
六、存出和载入镜像
6.1 存出镜像
如果要导出镜像到本地文件,可以使用docker save命令,该命令的使用方式如下:
root@ubuntu:~# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
例如,导出本地的ubuntu:18.04镜像到ubuntu_18.04.tar:
root@ubuntu:~# docker save -o ubuntu_18.04.tar ubuntu:18.04
root@ubuntu:~#
root@ubuntu:~# ll
total 88604
drwx------ 4 root root 4096 Mar 7 05:46 ./
drwxr-xr-x 24 root root 4096 Feb 24 02:33 ../
-rw------- 1 root root 26088 Mar 5 05:53 .bash_history
-rw-r--r-- 1 root root 3106 Oct 22 2015 .bashrc
drwx------ 2 root root 4096 Feb 24 02:25 .cache/
drwxr-xr-x 2 root root 4096 Mar 1 23:59 data/
-rw-r--r-- 1 root root 148 Aug 17 2015 .profile
-rw------- 1 root root 90668032 Mar 7 05:46 ubuntu_18.04.tar
-rw-r--r-- 1 root root 170 Mar 6 04:53 .wget-hsts
之后,就可以通过ubuntu_18.04.tar文件将镜像分享给他人。
6.2、载入镜像
可以使用docker load将导出的tar文件导入到本地镜像库,该命令的使用方式如下:
root@ubuntu:~# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
例如,从文件ubuntu_18.04.tar导入镜像到本地镜像列表:
root@ubuntu:~# docker load -i ubuntu_18.04.tar
Loaded image: ubuntu:18.04
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 fa2512ebcf70 34 minutes ago 88.1MB
ubuntu 18.04 47b19964fb50 4 weeks ago 88.1MB