0%

docker命令分析--镜像相关2


作者: 耗子007


所有命令均基于docker1.11版本

镜像构建

这部分主要包含,镜像制作相关的命令分析。镜像制作有三种情况:

  • 从零开始,基于rootfs制作
  • 基于已有镜像,用Dockerfile制作
  • 保存运行容器为镜像

对应的三个命令如下:

  • import 导入tar包内容,创建一个文件系统镜像
  • build 基于Dockerfile构建镜像
  • commit 把容器的修改制作为一个新的镜像

import命令

1
2
3
4
5
6
7
8
9
Usage: docker import file|URL|- [REPOSITORY[:TAG]]

Create an empty filesystem image and import the contents of the
tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then
optionally tag it.

-c, --change=[] Apply specified Dockerfile instructions while importing the image
--help Print usage
-m, --message= Set commit message for imported image

注意:import是会先创建一个空的文件系统镜像,然后把tar包的内容导入。这和后面提到的load命令的操作是不一样的。

import支持三种读取文件方式:

  • 直接指定本地路径,例如:docker import /path/to/exampleimage.tgz
  • 指定远程URL,例如:docker import http://example.com/exampleimage.tgz
  • 通过STDIN,分两种情况,一个是tar包文件,例如:cat exampleimage.tgz | docker import - exampleimagelocal:new ;一个是目录,例如:tar -c . | docker import - exampleimagedir

import的-c选项在已创建的镜像上用Dockerfile的指令对镜像做修改,目前支持的指令有:CMD|ENTRYPOINT|ENV|EXPOSE|ONBUILD|USER|VOLUME|WORKDIR。
例如,给新制作的镜像增加一个DEBUG的环境变量:tar -c . | docker import –change “ENV DEBUG true” - exampleimagedir

build命令

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
Usage: docker build [OPTIONS] PATH | URL | -

Build a new image from the source code at PATH

--build-arg=[] Set build-time variables
--cpu-shares CPU Shares (relative weight)
--cgroup-parent="" Optional parent cgroup for the container
--cpu-period=0 Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota=0 Limit the CPU CFS (Completely Fair Scheduler) quota
--cpuset-cpus="" CPUs in which to allow execution, e.g. `0-3`, `0,1`
--cpuset-mems="" MEMs in which to allow execution, e.g. `0-3`, `0,1`
--disable-content-trust=true Skip image verification
-f, --file="" Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--help Print usage
--isolation="" Container isolation technology
--label=[] Set metadata for an image
-m, --memory="" Memory limit for all build containers
--memory-swap="" A positive integer equal to memory plus swap. Specify -1 to enable unlimited swap.
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm=true Remove intermediate containers after a successful build
--shm-size=[] Size of `/dev/shm`. The format is `<number><unit>`. `number` must be greater than `0`. Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you omit the unit, the system uses bytes. If you omit the size entirely, the system uses `64m`.
-t, --tag=[] Name and optionally a tag in the 'name:tag' format
--ulimit=[] Ulimit options

build是根据Dockerfile和上下文构建Docker镜像,这里的上下文是PATH或者URL所指目录的文件。Dockerfile获取方式也是三种:

  • 直接指定本地路径,例如以当前目录的Dockerfile和文件上下文构建:docker build .
  • 指定远程URL,例如从github下载:docker build github.com/creack/docker-firefox
  • 通过STDIN,两种情况,一个是不包含上下文:docker build - < Dockerfile;一个是带上下文docker build - < context.tar.gz

关于build命令的选项,这里就描述几个常用的,其他的可以参考官方文档。

-t:用于指定构建的镜像的repository名和tag,值的格式为“name:tag”,例如:docker build -t vieux/apache:2.0 .
也可以给一个镜像指定多个tag,例如:docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
-f:指定Dockerfile文件路径,默认的Dockerfile名就是Dockerfile,build命令的上下文路径里面必须有Dockerfile文件,
但是可以通过-f来指定其他名字的Dockerfile文件,例如:docker build -f Dockerfile.debug .
–build-arg:设置build过程中的参数,该参数的有效期为build过程,例如设置HTTP_PROXY环境变量:docker build –build-arg HTTP_PROXY=http://10.20.30.2:1234 .

commit命令

1
2
3
4
5
6
7
8
9
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

-a, --author="" Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change=[] Apply specified Dockerfile instructions while committing the image
--help Print usage
-m, --message="" Commit message
-p, --pause=true Pause container during commit

commit以运行的容器构建Docker镜像,可以在命令最后面直接指定新构建的镜像repository和tag。
commit命令可以方便的保存当前容器的状态到镜像中,然后可以方便的迁移到另外一台机器中继续运行,用于测试或者调试是很好的方法。不过,管理镜像还是用Dockerfile更合理。

commit命令的选项比较简单,主要包括:

  • -a 设置镜像的作者
  • -c 和import命令一样
  • -m 设置commit的信息
  • -p 可以设置构建镜像过程中,是否停止容器中进程运行,默认情况下是停止容器中的进程。

镜像操作

镜像操作主要是镜像的导入、导出、删除、查看镜像列表以及查看镜像的历史信息,对应命令如下:

  • load/save
  • import/export
  • images
  • rmi
  • history

导入导出

镜像的导入导出有两组命令,分别是import/export和load/save。

  1. 导入

import可以参考上文。
load命令:只是从tar包或者STDIN中加载镜像。

1
2
3
4
5
6
7
Usage: docker load [OPTIONS]

Load an image from a tar archive or STDIN

--help Print usage
-i, --input="" Read from a tar archive file, instead of STDIN. The tarball may be compressed with gzip, bzip, or xz
-q, --quiet Suppress the load output. Without this option, a progress bar is displayed.

import和load的区别在于:

  • import会创建一个空的文件系统镜像,然后才会把tar包或者STDIN中的内容导入到空的镜像中。(会从零开始创建一个镜像)
  • load只是把tar包或者STDIN中的镜像导入,这说明tar包或者STDIN中的输入本身就是一个镜像。(简单的导入已有镜像)
  1. 导出

export命令:导出容器的文件系统到tar文件。

1
2
3
4
5
6
Usage: docker export [OPTIONS] CONTAINER

Export the contents of a container's filesystem as a tar archive

--help Print usage
-o, --output="" Write to a file, instead of STDOUT

两种用法:

  • docker export hexo > myhexo.tar
  • docker export –output=”myhexo.tar” hexo

注: export不导出数据卷的内容

save命令:把一个或者多个镜像导出到tar文件。

1
2
3
4
5
6
Usage: docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

--help Print usage
-o, --output="" Write to a file, instead of STDOUT

注:save会把所有父层以及name:tag导出,除非重名name:tag。
几种用法:

  • 使用标准输出导出一个镜像: docker save busybox > busybox.tar
  • 指定输出流导出一个镜像:docker save –output busybox.tar busybox
  • 导出整个repository:docker save -o fedora-all.tar fedora

export和save都是导出容器镜像,区别在:

  • export是导出容器的文件系统
  • save是保存加载的容器镜像

查看镜像列表

1
2
3
4
5
6
7
8
9
10
11
Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter=[] Filter output based on conditions provided
--format Pretty-print images using a Go template
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs

history用于列出镜像列表,主要用法如下:

  • 默认显示顶层的镜像、它们的repository名、tag以及镜像大小(如果镜像ID一样而且有多个tag或者repository,则会列出多次):docker images
  • 以repository名列出镜像列表(repository名必须完全匹配):docker images java
  • 以repository名和tag列出镜像列表(必须完全匹配):docker images java:8
  • 显示镜像的完整ID:docker images –no-trunc
  • 以摘要列出镜像列表(只有v2以上版本的镜像才有digest):docker images –digests
  • 以filter过滤条件列出镜像列表,目前支持两种,第一个,过滤untagged镜像docker images –filter “dangling=true”;
  • 第二个,过滤label,格式为label (label= or label==),示例如:docker images –filter “label=com.example.version”

单独描述一下format的用法,format是用来格式化输出的,使用Go语言模板实现,支持格式如下:

Placeholder Description
.ID Image ID
.Repository Image repository
.Tag Image tag
.Digest Image digest
.CreatedSince Elapsed time since the image was created.
.CreatedAt Time when the image was created.
.Size Image disk size.

例如只显示镜像的ID和repository名字:docker images –format “{ {.ID} }: { {.Repository} }”

删除镜像

1
2
3
4
5
6
7
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

-f, --force Force removal of the image
--help Print usage
--no-prune Do not delete untagged parents

注意:

  • 镜像的长ID、短ID、tag或者digest都可以用于删除它
  • 如果一个镜像有多个tag引用它,删除这个镜像之前,必须先删除所有tag引用。
  • 当使用tag删除一个镜像时,她的digest引用自动会被删除
  • 指定-f和镜像的ID,rmi命令会自动untag和删除所有匹配的镜像

查看镜像的历史

1
2
3
4
5
6
7
8
Usage: docker history [OPTIONS] IMAGE

Show the history of an image

-H, --human=true Print sizes and dates in human readable format
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs

history会列出镜像的build历史,例如:

1
2
3
4
5
6
7
8
# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
104bec311bcd 10 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 10 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'doc 7 B
<missing> 10 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 1.895 kB
<missing> 10 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B
<missing> 10 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /u 745 B
<missing> 10 weeks ago /bin/sh -c #(nop) ADD file:7529d28035b43a2281 128.9 MB