3.1 获取镜像

命令:

  1. docker pull NAME[:TAG]

其中,

  • NAME 是镜像的名称;
  • TAG 是镜像的标签,常用于表示版本。如果不显示指定 TAG ,则默认为 latest

例:

  1. docker pull ubuntu:14.04
  2. docker pull ubuntu

下载过程中通过输出信息可以看出,镜像文件一般由若干层(layer)组成。类似 6c8sdf8908df 这样的串是层的唯一id。完整的id包括256 bit,由64个十六进制字符组成。使用 docker pull 命令下载时会获取并输出镜像的各层信息。当不同的镜像包含相同的层时,本地仅存储一份该层的文件,从而减小了存储空间需求。
严格来讲,完整的镜像名称需要添加仓库地址前缀。比如上面的命令,完整版为:

  1. docer pull registry.hub.docker.com/ubuntu:14.04

当使用的是Docker Hub服务时,镜像仓库前缀可以省略。但使用其他仓库时,需要指定。
docker pull 命令选项:

  • -a or --all-tags = true|false:是否获取仓库中的所有镜像。默认为false。

3.2 查看镜像信息

1. 使用 images 命令列出镜像

命令:

  1. docker images

列出的信息有:

  • REPOSITORY :仓库名称,或者说镜像名称,
  • TAG :标签。注意标签只是一个标记,不能通过标签判断两个镜像是否内容相同。
  • IMAGE ID镜像的ID,是镜像的唯一标识。两个Tag不同的镜像,可能ID相同,说明它们指向同一个镜像。
  • CREATED :创建时间。
  • SIZE :镜像大小。注意这里的大小只是逻辑大小,并不代表该镜像实际占用的空间。因为Docker采用分层文件系统,因此实际大小通常会小于逻辑大小。

该命令支持通过镜像名和tag来过滤镜像。当指定了镜像名时,只有镜像名与指定的镜像名完全匹配的镜像才会列出来。当同时指定了镜像名和tag时,只有两者都完全匹配的镜像才会列出来。注意,对于非docker hub的镜像,需要指定仓库前缀才能匹配。
选项:

Name, shorthand Default Description
--all , -a Show all images (default hides intermediate images)
--digests Show digests
--filter , -f Filter output based on conditions provided
--format Pretty-print images using a Go template
--no-trunc Don’t truncate output
--quiet , -q Only show numeric IDs

参考链接:https://docs.docker.com/engine/reference/commandline/images/

2. 使用 tag 命令创建镜像标签

命令:

  1. docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

功能:创建一个带tag的新镜像,它相当于一个“快捷方式”,或者说链接,指向原始镜像。

3. 使用 insepect 命令查看底层信息

  1. docker inspect [OPTIONS] NAME|ID [NAME|ID...]

4. 使用 history 命令查看镜像历史

  1. docker history [OPTIONS] IMAGE

选项:

Name, shorthand Default Description
--format 使用GO的模板来格式化输出
--human , -H true 以人类可读的方式显示时间和大小
--no-trunc 不要截断输出
--quiet , -q Only show numeric IDs

3.3 搜寻镜像

  1. docker search [OPTIONS] TERM

选项:

Name, shorthand Default Description
--automated deprecated
Only show automated builds
--filter , -f Filter output based on conditions provided
--format Pretty-print search using a Go template
--limit 25 Max number of search results
--no-trunc Don’t truncate output
--stars , -s deprecated
Only displays with at least x stars

例:

  1. docker search busybox

3.4 删除镜像

  1. docker rmi [OPTIONS] IMAGE [IMAGE...]

选项:

Name, shorthand Default Description
--force , -f 强制删除。用于删除正在运行中的容器的镜像。
--no-prune Do not delete untagged parents

可以使用以下两种方式来指定删除的目标镜像:

  • 镜像名+Tag
  • 镜像ID

如果使用镜像名+Tag的方式,那么当某个镜像文件关联多个Tag时,只会删除指定的tag,而不是删除镜像文件本身。如果某个镜像文件只有一个tag,那么删除该tag也会删除镜像本身。
如果使用镜像ID,则会删除这个镜像文件及其关联的所有Tag。


3.5 创建镜像

1. 基于已有镜像的容器创建

先启动一个容器,在容器中进行修改,然后将该容器保存为一个新的镜像。
关键命令:

  1. docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

选项:

Name, shorthand Default Description
--author , -a Author (e.g., “John Hannibal Smith hannibal@a-team.com”)
--change , -c Apply Dockerfile instruction to the created image
--message , -m Commit message
--pause , -p true Pause container during commit

示例:
1.以交互模式启动基础镜像,创建一个容器:

  1. docker run -it docker.neg/base/alpine-bash

2.在容器中执行修改命令,然后退出:

  1. touch test
  2. exit

3.查询上一步创建的容器IDI:

  1. docker ps -a

4.使用 docker commit 命令将容器提交为一个新镜像:

  1. docker commit -m "Add new file" -a "Jake" 82239dc39b3d test:0.1

5.查看本地镜像:

  1. docker images

2. 基于本地模板导入(导入压缩包)

  1. docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

选项:

Name, shorthand Default Description
--change , -c Apply Dockerfile instruction to the created image
--message , -m Set commit message for imported image
--platform experimental (daemon)API 1.32+
Set platform if server is multi-platform capable

3.6 存出和载入镜像

1. 存出镜像

将一个或多个镜像保存为一个tar文件。默认情况下输出流输出到STDOUT。通常都需要指定-o参数,来输出到指定的文件中。否则需要使用输出重定向。

  1. docker save [OPTIONS] IMAGE [IMAGE...]

选项:

Name, shorthand Default Description
--output , -o Write to a file, instead of STDOUT

2. 载入镜像

从一个tar文件或STDIN载入镜像。通常都需要-i参数,来指定载入的目标文件。否则需要使用输入重定向。

  1. docker load [OPTIONS]

选项:

Name, shorthand Default Description
--input , -i Read from tar archive file, instead of STDIN
--quiet , -q Suppress the load output

3. 上传镜像

  1. # 上传到docker hub
  2. docker push [OPTIONS] NAME[:TAG]
  3. # 上传到私有仓库
  4. docker push REGISTRY_HOST:[PORT]/NAME[:TAG]

选项:

Name, shorthand Default Description
--disable-content-trust true Skip image signing

例:上传到私有仓库:

  1. # 为镜像创建一个新标签,使其带上私有仓库地址
  2. docker tag test:0.1 docker.neg/po/test:0.1
  3. # 推送
  4. docker push docker.neg/po/test:0.1