title: Docker命令解读(三)
date: 2015-11-22 19:35:36
tags: docker
categories: Docker Commands
Docker命令解读系列文章将系统讲解Docker使用的命令,方便大家学习Docker的基本操作。在写这个系列文章的时候,主要参考了Docker官方的文档,有些内容是直接的翻译。原文档地址 Docker Docs
转载请注明出处
这篇文章讲解下面几个命令:
* build
* build with PATH
* build with URL
* build woth -
* -t 标识
* -f标识
* commit
build
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=false Always remove intermediate containers
--help=false Print usage
-m, --memory="" Memory limit for all build containers
--memory-swap="" Total memory (memory + swap), `-1` to disable swap
--no-cache=false Do not use cache when building the image
--pull=false Always attempt to pull a newer version of the image
-q, --quiet=false Suppress the verbose output generated by the containers
--rm=true Remove intermediate containers after a successful build
-t, --tag="" Repository name (and optionally a tag) for the image
--ulimit=[] Ulimit options
docker build
命令用来通过Dockerfile
和上下文(context
)文件创建镜像,context
可以通过PATH
或URL
属性指出,build
进程有context
下所有文件的访问权限。
通过URL
你可以指定一个git
代码库,这样git
代码库的作用就是context
。Docker将git库中的内容clone到你的计算机上,然后将它们传送给docker daemon
,用来创建镜像。
当使用git
时,可以指定分支和具体的文件夹,中间用冒号:
分割。例如:
docker build https://github.com/docker/rootfs.git#container:docker
上面的例子中的上下文context
是container分支下的docker文件夹。
更多的格式如下:
除了指定上下文之外,你也可以通过URL
指定一个单独的Dockerfile
文件,或者通过STDIN
标准输入流传递Dockerfile
文件,格式为:
docker build - < Dockerfile
如果你通过URL
指定一个文件或者通过STDIN
传递了一个文件,Docker会将其内容作为Dockerfile
的内容,并且任何-f
,--file
参数都会被忽略。这种情况下,没有上下文context
。
默认情况下,Docker
会在context
的根目录中寻找名为Dockerfile
的文件。你可以通过-f
, --file
参数指定一个文件作为Dockerfile
文件。但指定的文件必须包含在context
中,如果你要指定一个相对路径,必须是相对于当前文件夹的。
通常情况下,最好新建一个文件夹,并且仅将创建镜像需要的文件或文件夹拷贝到新建的文件夹下。或者,你也可以编写一个.dockerignore
文件来指定那些文件或文件夹是用不到的。请参考.dockerignore file
build命令执行成功后,build进程会返回一个0值,若失败返回非0值。
build with PATH
一般情况下,我们会通过.
将当前文件夹作为上下文传递给docker daemon
,如:
docker build .
这样,Docker
会在当前文件夹下寻找名为“Dockerfile”的文件,并将当前文件夹下的所有的文件和文件夹传递给docker daemon
进程(如果没有编写.dockerignore
)。记住,docker client
是可以和远程docker daemon
通信的,这种情况下,Docker
需要将context
中的所有文件和文件夹通过网络传送给docker daemon
,所以,请不要将无用的内容包括在context
中。
Note:当运行docker build
命令时,终端输出的”Sending build context”文件就是表示在向docker daemon
传送文件。
build with URL
例如:
docker build github.com/creack/docker-firefox
这将会clone git库到本地并将其作为context,其根目录下的名为“Dockerfile”的文件被用作build时的Dockerfile
。
你可以通过两种方式指定git库,git://
或git@
。
build with -
这种方式下,Docker
会从STDIN
中读取一个Dockerfile
,且这种方式没有context
上下文。所以将不会有任何本地文件或文件夹传送给docker daemon
。并且Dockerfile
中的ADD
指令也仅在指定的是一个URL时有效。
但下面的方式:
docker build - < context.tar.gz
将会从STDIN
读取一个压缩的上下文context
并传送给docker daemon
。支持的压缩格式为:bzip2
,gzip
和xz
。
-t标识
-t
标识用来指定build后的镜像的repository
和标签Tag
(可选项)。格式为:
docker build -t vieux/apache:2.0 .
-f标识
-f
标识用来指定build时的Dockerfile
文件。例如:
docker build -f Dockerfile.debug .
上面的命令使用当前目录.
下的“Dockerfile.debug”文件作为Dockerfile
。
commit
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=false Print usage
-m, --message="" Commit message
-p, --pause=true Pause container during commit
commit
命令用于从容器创建镜像,但官方更推荐使用Dockerfile
创建镜像。
Note:commit
操作不会提交容器中的卷volumes
中的任何信息,即卷中的信息不会出现在创建额镜像中。
默认情况下,被commit
的容器中的所有进程都会暂停paused
,如果你并不期望这么做,可以通过-p
或--pause
标识改变它。
--change
选项用来像要创建的镜像添加Dockerfile
指令,支持如下的Dockerfile
指令:
CMD
,ENTERPOINT
,ENV
,EXPOSE
,LABEL
,ONBUILD
,USER
,VOLUME
,WORKDIR
例如:
docker commit --change "ENV DEBUG true" c3f279d17e0a SvenDowideit/testimage:version3
docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" c3f279d17e0a SvenDowideit/testimage:version4