Docker 第七章 Dockerfile 详解

 

[root@localhost Docker_iamge]# docker build --help

Usage:	docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build
                                (default "default")
      --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                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the 'name:tag' format       #打标签
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])
[root@localhost Docker_iamge]# 
[root@localhost Docker_iamge]# 

 

Dockerfile 格式编写

[root@localhost Docker_iamge]# ls
Dockerfile  index.html
[root@localhost Docker_iamge]# cat Dockerfile
# Description :test image

FROM busybox:latest
#一个Dockerfile 必须用`FROM`指令启动。该FROM指令指定您正在构建的基本映像
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines." 
#LABEL 标签

COPY   index.html /www/html/index.html
#该COPY指令从中复制新文件或目录<src> ,并将它们添加到路径中容器的文件系统中<dest> [root@localhost Docker_iamge]#

  

 docker build 构建镜像 

[root@localhost Docker_iamge]# docker build -t httpdtest:v1 .          
Sending build context to Docker daemon  15.87kB
Step 1/6 : FROM busybox:latest
 ---> 64f5d945efcc
Step 2/6 : LABEL "com.example.vendor"="ACME Incorporated"
 ---> Running in cfdd36a69097
Removing intermediate container cfdd36a69097
 ---> 6bbc54c2f4e4
Step 3/6 : LABEL com.example.label-with-value="foo"
 ---> Running in 3ae90f68ae1c
Removing intermediate container 3ae90f68ae1c
 ---> 92b662f0bfd9
Step 4/6 : LABEL version="1.0"
 ---> Running in 36194eff5eb8
Removing intermediate container 36194eff5eb8
 ---> 9e3a4c064dc7
Step 5/6 : LABEL description="This text illustrates that label-values can span multiple lines."
 ---> Running in cdfcef379b7a
Removing intermediate container cdfcef379b7a
 ---> 05ed662e3088
Step 6/6 : COPY   index.html /www/html/index.html
 ---> 6060a9cb9597
Successfully built 6060a9cb9597
Successfully tagged httpdtest:v1
[root@localhost Docker_iamge]# 
[root@localhost Docker_iamge]# docker image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
httpdtest                          v1                  6060a9cb9597        5 minutes ago       1.2MB
test                               v2.0                51f46a949468        8 days ago          1.2MB
test                               v1.0                648f9e13a37c        9 days ago          1.2MB
zy/busybox                         v0.1                4c9fa4db9491        9 days ago          1.2MB
dockerpracticecn/docker_practice   latest              b6bfd54275de        4 weeks ago         41.8MB
busybox                            latest              64f5d945efcc        4 weeks ago         1.2MB
nginx                              latest              53f3fd8007f7        5 weeks ago         109MB
[root@localhost Docker_iamge]# 

 

 [root@localhost Docker_iamge]# docker run --name q2 --rm httpdtest:v3 cat /www/html/index.html #检查是否包含添加的内容
  <h1> test copy line </h1>
  [root@localhost Docker_iamge]#

  

ADD 指令

ADD有两种形式:

ADD [--chown=<user>:<group>] <src>... <dest>
ADD [--chown=<user>:<group>] ["<src>",... "<dest>"] (包含空格的路径需要此表单)
注意:该--chown功能仅在用于构建Linux容器的Dockerfiles上受支持,并且不适用于Windows容器。由于用户和组所有权概念不能在Linux和Windows之间进行转换,因此使用/etc/passwd和/etc/group将用户名和组名转换为ID会限制此功能仅适用于基于Linux OS的容器。

该ADD指令从中复制新文件,目录或远程文件URL <src> ,并将它们添加到路径上图像的文件系统中<dest>。

<src>可以指定多
[root@localhost Docker_iamge]# cat Dockerfile 
# Description :test image

FROM busybox:latest
#一个Dockerfile 必须用`FROM`指令启动。该FROM指令指定您正在构建的基本映像
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates that label-values can span multiple lines." 
#LABEL 标签

COPY   index.html /www/html/index.html
#COPY   /etc/sysconfig/   /test/sysconfig/

ADD http://nginx.org/download/nginx-1.17.0.tar.gz /usr/local/nginx/
# [root@localhost Docker_iamge]#

  

WORKDIR   #指定默认的工作目录,在后续不指定路径时 WORKDIR 就是默认目录
WORKDIR /path/to/workdir
该WORKDIR指令集的工作目录对任何RUN,CMD, ENTRYPOINT,COPY和ADD它后面的说明Dockerfile。如果WORKDIR不存在,即使它未在任何后续Dockerfile指令中使用,也将创建它。

该WORKDIR指令可以在a中多次使用Dockerfile。如果提供了相对路径,则它将相对于前一条WORKDIR指令的路径 。例如:

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

  

VOLUME
VOLUME ["/data"]
该VOLUME指令创建具有指定名称的安装点,并将其标记为从本机主机或其他容器保存外部安装的卷。该值可以是JSON数组,VOLUME ["/var/log/"]或具有多个参数的普通字符串,例如VOLUME /var/log或VOLUME /var/log /var/db。有关通过Docker客户端提供的更多信息/示例和安装说明,请参阅 通过卷共享目录 文档。
[root@localhost Docker_iamge]# cat Dockerfile 
# Description :test image

FROM busybox:latest
#一个Dockerfile 必须用`FROM`指令启动。该FROM指令指定您正在构建的基本映像
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates that label-values can span multiple lines." 
#LABEL 标签

COPY   index.html /www/html/index.html
#COPY   /etc/sysconfig/   /test/sysconfig/


#ADD http://nginx.org/download/nginx-1.17.0.tar.gz /usr/local/nginx/
RUN mkdir -p /test/mysql/
VOLUME /test/mysql
#VOLUME 只能使用自动挂载的方法,在容器启动后能够看到对应的挂载文件,否则 inspect image 只能看到挂载点,看不到主机指定源目录
#指定Docker 的自动挂载 [root@localhost Docker_iamge]#

ENV

ENV
ENV <key> <value>
ENV <key>=<value> ...
该ENV指令将环境变量<key>设置为该值 <value>。此值将在构建阶段中的所有后续指令的环境中,并且也可以在许多内联替换。

该ENV指令有两种形式。第一种形式,ENV <key> <value>将单个变量设置为一个值。第一个空格后的整个字符串将被视为<value>- 包括空格字符。该值将针对其他环境变量进行解释,因此如果未对其进行转义,则将删除引号字符。

第二种形式ENV <key>=<value> ...允许一次设置多个变量。请注意,第二种形式在语法中使用等号(=),而第一种形式则不然。与命令行解析一样,引号和反斜杠可用于在值内包含空格。

例如:

ENV myName="John Doe" myDog=Rex\ The\ Dog \
    myCat=fluffy
和

ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy

  

-e   : 运行镜像时定义环境变量
[root@localhost Docker_iamge]# docker run -it --name aqd --rm -e tttt="ffffff" httptest:v8.0 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=892d0430a927 TERM=xterm tttt=ffffff DOC_ROOT=/src/data/test1/ HOME=/root [root@localhost Docker_iamge]# [root@localhost Docker_iamge]#

  

ARG
ARG <name>[=<default value>]
该ARG指令定义了一个变量,用户可以docker build使用该--build-arg <varname>=<value> 标志在构建时将该变量传递给构建器。如果用户指定了未在Dockerfile中定义的构建参数,则构建会输出警告。

[Warning] One or more build-args [foo] were not consumed.
Dockerfile可以包括一个或多个ARG指令。例如,以下是有效的Dockerfile:

FROM busybox
ARG user1
ARG buildno
...
警告:建议不要使用构建时变量来传递github密钥,用户凭据等秘密docker history。使用该命令,构建时变量值对于映像的任何用户都是可见的。

默认值
的ARG指令可以可选地包括一个默认值:

FROM busybox
ARG user1=someuser
ARG buildno=1
...
如果ARG指令具有默认值,并且在构建时没有传递值,则构建器将使用默认值。

范围

  

 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

转载于:https://www.cnblogs.com/zy09/p/11017382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值