Docker命令解读-三

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可以通过PATHURL属性指出,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。支持的压缩格式为:bzip2gzipxz

-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指令:
CMDENTERPOINTENVEXPOSELABELONBUILDUSERVOLUMEWORKDIR
例如:

docker commit --change "ENV DEBUG true" c3f279d17e0a  SvenDowideit/testimage:version3
docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" c3f279d17e0a  SvenDowideit/testimage:version4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值