docker之dockerfile详细学习1

梦想与她 提交于 2019-12-07 14:42:15

制作容器
    基于容器做镜像
    dockerfile
===============================================
Dockerfile Format
1、不区分大小写
2、从上倒下执行(非注释行)
3、必须指定一个基础镜像
formart
   #comment
   INSTRUCTION arguments
===============================================
工作逻辑
    专用目录
    Dockerfile首字母大写,在Dockerfile中不区分大小写,推大写
    需要引用的文件必须是当前目录或者之下的目录
    .dockeringore--排除,排除之后就不再打包到容器
    docker build

指令
FROM --最重要的,第一个非注释的行,用于为映像文件构建过程指定的基准镜像。默认情况docker build会在docker主机上查找镜像文件,如果不存在去Docker hub Registry上拉。
MAINTANIER(depreacted)--用于提供相关信息
LABEL--用于提供相关信息 替代MAINTANIER
COPY--用于从Docker主机复制文件到创建的新映像文件 COPY src ..des或者 COPY ["src"  "dest"],目录自己不会被复制,下面的文件或者字幕了会被复制。
ADD--类是于copy指令,ADD支持使用tar文件和url路径,同copy、解压、URL(直接下载)build使用默认值
WORKDIR--指出目录相当于环境变量   工作目录    
VOLUME--用于在image中创建挂载点  
EXPOSE--暴露端口,不是直接暴露,如果需要暴露需要加-P,默认是tcp
ENV--用于为镜像定位所需要的环境变量,并可被Dockerfile文件中位于其后的其他指令所调用 ENV key value或者ENV key=value..key1=value1...

两者都是运行命令的
RUN--当你基于dockerfile构建镜像时,docker build过程。RUN逐一运行
CMD--用于把镜像启动为容器时,docker run过程  CMD只能有一个生效。CMD command   
ENTRYPOINT-类是CMD,用于为容器指定默认运行程序,和CMD不同的是不会被docker run中的命令覆盖。和CMD联合使用,这样可以传参数,shell可以使用
USER--用于指定运行image时活运行Dockerfile中任何RUN、CMD、ENTRYPOINT指令指定的程序的用户名和UID,默认情况下container运行身份是root
HEALTHCHECK--健康状态检查,默认30s检查一次,timeout时间30s  0(success)、1(unhealthy)、2(reserved)

shell--运行程序默程序,使用的较少
STOHSIGNAL--发送信号给容器 使用较少
ARG--和ENV很像,只是在build过程中使用,--build-arg,一个dcokerfile使用较多的场景
ONBUILD--用于dockerfile中定义触发器,延时执行,做成一个镜像,被别人用作基础竟像的时候执行,onbuild不能自我嵌套,而且不能触发FROM和MAINTAINER指令。


注意
    能把一条指令成一条,尽量一条
    json数组中要使用双引号
    
以下是一条一条指令实验
==============================================
实验
[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1
[root@localhost img1]# vi Dockerfile
#Des:test image
FROM busy:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
[root@localhost img1]# docker build -h
Flag shorthand -h has been deprecated, please use --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 img1]# docker build -t wolfhttpd:v0.1-1 ./    -t直接打上tag
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/3 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Running in 3d8b6d6624f6
Removing intermediate container 3d8b6d6624f6
 ---> 6eab500a1260
Step 3/3 : COPY index.html /data/web/html/
 ---> 889882d019f9
Successfully built 889882d019f9
Successfully tagged wolfhttpd:v0.1-1
查看
[root@localhost img1]# docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
wolfhttpd                                              v0.1-1              889882d019f9        42 seconds ago      1.15MB
查看是否成功
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-1 cat /data/web/html/index.html
<h1>wolf container no 1</h1>
[root@localhost img1]# 
----------------------------------------
实验2
[root@localhost img1]# ll
total 12
-rw-r--r--. 1 root root  186 Dec 26 22:43 Dockerfile
-rw-r--r--. 1 root root   29 Dec 26 22:34 index.html
drwxr-xr-x. 2 root root 4096 Dec 26 22:42 yum.repos.d
[root@localhost img1]# cat Dockerfile 
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/             加一行,这里目录如果相同,尽量和上一条整合,这里惜字如金

[root@localhost img1]# docker build -t wolfhttpd:v0.1-2 ./
Sending build context to Docker daemon  20.48kB
Step 1/4 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/4 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> 6eab500a1260
Step 3/4 : COPY index.html /data/web/html/
 ---> Using cache
 ---> 889882d019f9
Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/
 ---> bd5594568203
Successfully built bd5594568203
Successfully tagged wolfhttpd:v0.1-2
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-2 ls /etc/yum.repos.d/
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
CentOS-fasttrack.repo
docker-ce.repo
[root@localhost img1]# 
--------------------------------------------
实验3
[root@localhost img1]# cat Dockerfile 
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
[root@localhost img1]# docker build -t wolfhttpd:v0.1-3 ./
Sending build context to Docker daemon  20.48kB
Step 1/5 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/5 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> 6eab500a1260
Step 3/5 : COPY index.html /data/web/html/
 ---> Using cache
 ---> 889882d019f9
Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> bd5594568203
Step 5/5 : ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
Downloading [==================================================>]  1.028MB/1.028MB
 ---> ad866f098832
Successfully built ad866f098832
Successfully tagged wolfhttpd:v0.1-3
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-3 ls /usr/local/src
nginx-1.15.8.tar.gz
---------------------------------------------
实验4
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
ADD nginx-1.15.8.tar.gz /usr/local/src/                                   相当与直接tar -xzvf -C

[root@localhost img1]# docker build -t wolfhttpd:v0.1-4 ./
Sending build context to Docker daemon  1.049MB
Step 1/5 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/5 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> 6eab500a1260
Step 3/5 : COPY index.html /data/web/html/
 ---> Using cache
 ---> 889882d019f9
Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> bd5594568203
Step 5/5 : ADD nginx-1.15.8.tar.gz /usr/local/src/
 ---> 7d6d799fd70e
Successfully built 7d6d799fd70e
Successfully tagged wolfhttpd:v0.1-4
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-3 ls /usr/local/src
nginx-1.15.8.tar.gz
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-4 ls /usr/local/src
nginx-1.15.8
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-4 ls /usr/local/src/nginx-1.15.8
CHANGES
CHANGES.ru
LICENSE
README
auto
conf
configure
contrib
html
man
src
------------------------------------------------
实验5
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/src
ADD nginx-1.15.8.tar.gz ./    
---------------------------------------------
实验6
[root@localhost img1]# cat Dockerfile 
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.8.tar.gz ./src/
VOLUME /data/mysql/

[root@localhost img1]# docker build -t wolfhttpd:v0.1-5 ./
Sending build context to Docker daemon  1.049MB
Step 1/7 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/7 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> 6eab500a1260
Step 3/7 : COPY index.html /data/web/html/
 ---> Using cache
 ---> 889882d019f9
Step 4/7 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> bd5594568203
Step 5/7 : WORKDIR /usr/local/
 ---> Running in e24c83599eb5
Removing intermediate container e24c83599eb5
 ---> 12096bece293
Step 6/7 : ADD nginx-1.15.8.tar.gz ./src/
 ---> 71446b44e1ec
Step 7/7 : VOLUME /data/mysql/
 ---> Running in ae36a4d7a641
Removing intermediate container ae36a4d7a641
 ---> af03cafbdaf9
Successfully built af03cafbdaf9
Successfully tagged wolfhttpd:v0.1-5
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-5 mount
rootfs on / type rootfs (rw)
/dev/mapper/docker-253:0-67471500-bbdfc0105478f9acc328e239abbde32f0b21f55e3b5eb3e9fbab445465c424e9 on / type xfs (rw,seclabel,relatime,nouuid,attr2,inode64,logbsize=64k,sunit=128,swidth=128,noquota)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,seclabel,nosuid,size=65536k,mode=755)
devpts on /dev/pts type devpts (rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
sysfs on /sys type sysfs (ro,seclabel,nosuid,nodev,noexec,relatime)
tmpfs on /sys/fs/cgroup type tmpfs (ro,seclabel,nosuid,nodev,noexec,relatime,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (ro,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/net_cls type cgroup (ro,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (ro,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset)
mqueue on /dev/mqueue type mqueue (rw,seclabel,nosuid,nodev,noexec,relatime)
/dev/mapper/centos-root on /data/mysql type xfs (rw,seclabel,relatime,attr2,inode64,noquota)    这里挂载上了 或者使用docker inspect 命令查看
/dev/mapper/centos-root on /etc/resolv.conf type xfs (rw,seclabel,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/hostname type xfs (rw,seclabel,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/hosts type xfs (rw,seclabel,relatime,attr2,inode64,noquota)
shm on /dev/shm type tmpfs (rw,seclabel,nosuid,nodev,noexec,relatime,size=65536k)
proc on /proc/bus type proc (ro,relatime)
proc on /proc/fs type proc (ro,relatime)
proc on /proc/irq type proc (ro,relatime)
proc on /proc/sys type proc (ro,relatime)
proc on /proc/sysrq-trigger type proc (ro,relatime)
tmpfs on /proc/acpi type tmpfs (ro,seclabel,relatime)
tmpfs on /proc/kcore type tmpfs (rw,seclabel,nosuid,size=65536k,mode=755)
tmpfs on /proc/keys type tmpfs (rw,seclabel,nosuid,size=65536k,mode=755)
tmpfs on /proc/timer_list type tmpfs (rw,seclabel,nosuid,size=65536k,mode=755)
tmpfs on /proc/timer_stats type tmpfs (rw,seclabel,nosuid,size=65536k,mode=755)
tmpfs on /proc/sched_debug type tmpfs (rw,seclabel,nosuid,size=65536k,mode=755)
tmpfs on /proc/scsi type tmpfs (ro,seclabel,relatime)
tmpfs on /sys/firmware type tmpfs (ro,seclabel,relatime)
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-5 sleep 60    

[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-5 sleep 60
[root@localhost img1]# docker inspect wolfhttpd    
-------------------------------------------------
实验7
[root@localhost img1]# cat Dockerfile 
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.8.tar.gz ./src/
VOLUME /data/mysql/
EXPOSE 80/tcp                               默认是tcp,不加/tcp也是tcp
[root@localhost img1]# docker build -t wolfhttpd:v0.1-6 ./
Sending build context to Docker daemon  1.049MB
Step 1/8 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/8 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> 6eab500a1260
Step 3/8 : COPY index.html /data/web/html/
 ---> Using cache
 ---> 889882d019f9
Step 4/8 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> bd5594568203
Step 5/8 : WORKDIR /usr/local/
 ---> Using cache
 ---> 12096bece293
Step 6/8 : ADD nginx-1.15.8.tar.gz ./src/
 ---> Using cache
 ---> 71446b44e1ec
Step 7/8 : VOLUME /data/mysql/
 ---> Using cache
 ---> af03cafbdaf9
Step 8/8 : EXPOSE 80/tcp
 ---> Running in aae5fffbe049
Removing intermediate container aae5fffbe049
 ---> 44c7bf47977c
Successfully built 44c7bf47977c
Successfully tagged wolfhttpd:v0.1-6
[root@localhost img1]# docker run --name wolfweb01 --rm wolfhttpd:v0.1-6 /bin/httpd -f -h /data/web/html 
[root@master ~]# docker inspect -f {{.NetworkSettings.IPAddress}} wolfweb01
172.17.0.6
换个窗口curl
[root@master ~]# curl 172.17.0.6
<h1>wolf container no 1</h1>    
[root@master ~]# docker port wolfweb01       这里并没有暴露端口,所以这里只能本机通过本机访问
-------------------------------------------------
实验8    真正暴露端口-P
[root@master img1]# docker run --name wolfweb01 --rm -P wolfhttpd:v0.1-6 /bin/httpd -f -h /data/web/html 
[root@master ~]# docker port wolfweb01
80/tcp -> 0.0.0.0:32768            
这是浏览器访问http://10.249.100.206:32768/ 
-------------------------------------------------
实验9
[root@master img1]# cat Dockerfile 
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
ENV DOC_ROOT=/data/web/html/ \
    WEB_SERVER_PACKAGE="nginx-1.15.8"
COPY index.html ${DOC_ROOT:-/data/web/html/}   这样写是怕没有这个目录
#COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
#ADD nginx-1.15.8.tar.gz ./src/
ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/
VOLUME /data/mysql/
EXPOSE 80/tcp

[root@master img1]# docker build -t wolfhttpd:0.1-7 ./
Sending build context to Docker daemon  1.049MB
Step 1/9 : FROM busybox:latest
 ---> 758ec7f3a1ee
Step 2/9 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> ac9b8947e7a1
Step 3/9 : ENV DOC_ROOT=/data/web/html/     WEB_SERVER_PACKAGE="nginx-1.15.8"
 ---> Running in 38efbb376b04
Removing intermediate container 38efbb376b04
 ---> 8810786c11b6
Step 4/9 : COPY index.html ${DOC_ROOT:-/data/web/html/}
 ---> 9f57c2ee48bf
Step 5/9 : COPY yum.repos.d /etc/yum.repos.d/
 ---> 99cbe9167ad6
Step 6/9 : WORKDIR /usr/local/
 ---> Running in 920d94f8ea09
Removing intermediate container 920d94f8ea09
 ---> ea1b2f88aef7
Step 7/9 : ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/
 ---> 4979716938e1
Step 8/9 : VOLUME /data/mysql/
 ---> Running in e1d09b8aba0e
Removing intermediate container e1d09b8aba0e
 ---> 34345593b60c
Step 9/9 : EXPOSE 80/tcp
 ---> Running in 53472b7b520a
Removing intermediate container 53472b7b520a
 ---> 0e2df844f402
Successfully built 0e2df844f402
Successfully tagged wolfhttpd:0.1-7
[root@master img1]# docker run --name wolfweb01 --rm -P wolfhttpd:0.1-7 ls /usr/local/src/
nginx-1.15.8
[root@master img1]# docker run --name wolfweb01 --rm -P wolfhttpd:0.1-7 ls /usr/local/src/nginx-1.15.8/
CHANGES
CHANGES.ru
LICENSE
README
auto
conf
configure
contrib
html
man
src
[root@master img1]# docker run --name wolfweb01 --rm -P wolfhttpd:0.1-7 ls /data/web/html
index.html
----------------------------------------------------
实验10
[root@master img1]# docker run --help

Usage:    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --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)
  -d, --detach                         Run container in background and print container ID
      --detach-keys string             Override the key sequence for detaching a container
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables                          这里也可以传值
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network string                 Connect a container to a network (default "default")
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container's port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container
  
[root@master img1]# docker run --name wolfweb01 --rm -P wolfhttpd:0.1-7 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=31c4d234e43f
DOC_ROOT=/data/web/html/
WEB_SERVER_PACKAGE=nginx-1.15.8
HOME=/root

直接使用-e,直接向环境变量赋值
[root@master img1]# docker run --name wolfweb01 --rm -P -e WEB_SERVER_PACKAGE="nginx-1.15.1" wolfhttpd:0.1-7 printenv    
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2fadc00a9a21
WEB_SERVER_PACKAGE=nginx-1.15.1
DOC_ROOT=/data/web/html/
HOME=/root

dockerfile优先,走dockerfile,因为已经build了 记成事实
[root@master img1]# docker run --name wolfweb01 --rm -P -e WEB_SERVER_PACKAGE="nginx-1.15.1" wolfhttpd:0.1-7 ls /usr/local/src
nginx-1.15.8
------------------------------------------
实验11
[root@master img1]# cat Dockerfile 
#Des:test image
FROM busybox:latest
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"

ENV DOC_ROOT=/data/web/html/ \
    WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz"
COPY index.html ${DOC_ROOT:-/data/web/html/}
#COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
WORKDIR /usr/local/

#ADD nginx-1.15.8.tar.gz ./src/
#ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/
VOLUME /data/mysql/
EXPOSE 80/tcp

RUN cd /usr/local/src && \
    tar -xf ${WEB_SERVER_PACKAGE}
[root@master img1]# docker build -t wolfhttpd:v0.1-9 ./
Sending build context to Docker daemon   1.05MB
Step 1/10 : FROM busybox:latest
 ---> 758ec7f3a1ee
Step 2/10 : MAINTAINER "wolf <1098331428@qq.com>"
 ---> Using cache
 ---> ac9b8947e7a1
Step 3/10 : ENV DOC_ROOT=/data/web/html/     WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz"
 ---> Using cache
 ---> bbf1510d7a94
Step 4/10 : COPY index.html ${DOC_ROOT:-/data/web/html/}
 ---> Using cache
 ---> 6330c422bc8d
Step 5/10 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> 1190a7ace558
Step 6/10 : ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
Downloading [==================================================>]  1.028MB/1.028MB
 ---> Using cache
 ---> b2f8fae1ec81
Step 7/10 : WORKDIR /usr/local/
 ---> Using cache
 ---> dc6a8cfe7d8b
Step 8/10 : VOLUME /data/mysql/
 ---> Using cache
 ---> 0ecf0c724a3d
Step 9/10 : EXPOSE 80/tcp
 ---> Using cache
 ---> d579c948cd22
Step 10/10 : RUN cd /usr/local/src &&     tar -xf ${WEB_SERVER_PACKAGE}
 ---> Running in ae65619e7d1a
Removing intermediate container ae65619e7d1a
 ---> fe92824fb737
Successfully built fe92824fb737
Successfully tagged wolfhttpd:v0.1-9
[root@master img1]# docker run --name wolfweb01 --rm -P -it wolfhttpd:v0.1-9 ls /usr/local/src/
nginx-1.15.8         nginx-1.15.8.tar.gz
-----------------------------------------------
#Des:test image
FROM centos
MAINTAINER "wolf <1098331428@qq.com>"
#LABEL maintainer="wolf <1098331428@qq.com>"
RUN yum install -y epel-release && yum  makecache && yum install nginx
-----------------------------------------------
shell是内核解释器
RUN command   command通常是一个shell命令,且以/bin/sh -c来运行它,这时pid不为1,容器不能接受unix信号。
RUN ["<executable>","<param1>","<param2>"]  这里就是pid为1

实验12
[root@master img2]# cat Dockerfile 
FROM busybox
LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"

ENV WEB_DOC_ROOT="/data/web/html/"

RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html

CMD /bin/httpd -f -h ${WEB_DOC_ROOT}

[root@master img2]# docker build -t wolfhttpdv:v0.2-1 ./

这条命令卡住
[root@master img2]# docker run --name wolfweb2 --rm -P -it wolfhttpdv:v0.2-1     这里默认运行pid 1不是shell ,是httpd,不会自动进行交互式接口通信,必须使用exec

[root@master img2]# docker inspect wolfweb2
    "Cmd": [
                "/bin/sh",
                "-c",
                "/bin/httpd -f -h ${WEB_DOC_ROOT}"
这里启动的也是httpd                
[root@master img2]# docker ps 
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                                            NAMES
e6ca4664152e        wolfhttpdv:v0.2-1              "/bin/sh -c '/bin/ht…"   6 minutes ago       Up 6 minutes                                                         wolfweb2
                
使用exec进入容器
[root@master img2]# docker exec -it wolfweb2 /bin/sh
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/httpd -f -h /data/web/html/   这里httpd为1
    8 root      0:00 /bin/sh
   16 root      0:00 ps
/ # printenv
WEB_DOC_ROOT=/data/web/html/
HOSTNAME=e6ca4664152e
SHLVL=1
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
/ #
---------------------------------------------------
实验13
[root@master img2]# cat Dockerfile 
FROM busybox
LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"

ENV WEB_DOC_ROOT="/data/web/html/"

RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html

#CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
CMD ["/bin/httpd","-f","-h ${WEB_DOC_ROOT}"]                              这里$调用为shell命令,因为默认运行不是shell的子进程,内核不认识shell命令,所以报下面的错误

[root@master img2]# docker build -t wolfhttpd:v0.2-2 ./

[root@master img2]# docker inspect wolfhttpd:v0.2-2

           "Cmd": [
                "/bin/httpd",
                "-f",
                "-h ${WEB_DOC_ROOT}"
在启动看看
[root@master img2]# docker run --name wolfhttpd2 -it --rm -P wolfhttpd:v0.2-2
httpd: can't change directory to ' ${WEB_DOC_ROOT}': No such file or directory   

修改Dcokerfile
[root@master img2]# cat Dockerfile 
FROM busybox
LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"

ENV WEB_DOC_ROOT="/data/web/html/"

RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html

#CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
CMD ["/bin/sh","-c","/bin/httpd","-f","-h /data/web/html"]  
[root@master img2]# docker inspect wolfhttpd:v0.2-4
[root@master img2]# docker run --name wolfhttpd2 -it --rm -P wolfhttpd:v0.2-4
-----------------------------------------------------
实验14
[root@master img2]# cat Dockerfile 
FROM busybox
LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"

ENV WEB_DOC_ROOT="/data/web/html/"

RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html

#CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
#CMD ["/bin/sh","-c","/bin/httpd","-f","-h /data/web/html/"]
ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT

[root@master img2]# docker build -t wolfhttpd:v0.2-5 ./
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM busybox
 ---> 758ec7f3a1ee
Step 2/5 : LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"
 ---> Using cache
 ---> 4561b9069835
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> 30980cf60d2c
Step 4/5 : RUN mkdir -p $WEB_DOC_ROOT &&     echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
 ---> Using cache
 ---> 86cbdaad823f
Step 5/5 : ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
 ---> Running in 3195bfcc31c9
Removing intermediate container 3195bfcc31c9
 ---> a8fc3cfbecef
Successfully built a8fc3cfbecef
Successfully tagged wolfhttpd:v0.2-5
[root@master img2]# docker run --name wolfweb2 -it --rm -P wolfhttpd:v0.2-5  这里ok
[root@master img2]# docker run --name wolfweb2 -it --rm -P wolfhttpd:v0.2-5 ls /data/web/html/  这里卡住,不接受ls
如果要想覆盖,必须加--entrypoint
[root@master img2]# docker run --name wolfweb2 -it --rm -P --entrypoint "ls /data/web/html" wolfhttpd:v0.2-5 ls /data/web/html/  
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"ls /data/web/html\": stat ls /data/web/html: no such file or directory": unknown.
--------------------------------------------------
实验15
FROM busybox
LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"

ENV WEB_DOC_ROOT="/data/web/html/"

RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html

CMD ["/bin/httpd","-f","-h /data/web/html}"]                       这种格式是当作参数传给ENTRYPOINT
ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}

----------------------------------------------------------
实验16
FROM busybox
LABEL maintainer="MageEdu <1098331428@qq.com>" app="httpd"

ENV WEB_DOC_ROOT="/data/web/html/"

RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html

   
CMD ["/bin/httpd","-f","-h /data/web/html}"]                这里cmd传给entrypoint
ENTRYPOINT ["/bin/sh","-c"]                                 目的是支持shell

[root@master img2]# docker image inspect wolfhttpd:v0.2-7 
            "Cmd": [
                "/bin/httpd",
                "-f",
                "-h /data/web/html}"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:1df68466a23cd041e94530d512947ac0121123bab70bf554b86e1e1cc4085b25",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/bin/sh",
                "-c"
            ],
[root@master img2]# docker run --name wolfweb2 -it --rm -P wolfhttpd:v0.2-7

[root@master img2]# docker run --name wolfweb2 -it -P wolfhttpd:v0.2-7 ls /data/
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
[root@master img2]# docker run --name wolfweb2 -it -P wolfhttpd:v0.2-7 "ls /data"
docker: Error response from daemon: Conflict. The container name "/wolfweb2" is already in use by container "f322a3cc561ce75268de999f39768a3caebd39b3ef4d00618cd619a35e08ec7f". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
[root@master img2]# docker rm wolfweb2
wolfweb2
[root@master img2]# docker run --name wolfweb2 -it -P wolfhttpd:v0.2-7 "ls /data"  现在可以用ls
web

上面这样组合做,可以传参数被shell解析,容器接受配置要靠
-----------------------------------------------------------------
实验17
[root@master img3]# cat entrypoint.sh 
#!/bin/sh
#
cat > /etc/nginx/conf.d/www.conf << EOF

server {
    server_name $HOSTNAME;
    listen ${IPI:-0.0.0.0}:${PORT:-80};
    root ${NGX_DOC_ROOT:-/usr/share/nginx/html};
}
EOF

exec "$@" 

[root@master img3]# cat Dockerfile 
FROM nginx:1.14-alpine
LABEL maintainer="Wolf <1098331428@qq.com>"

ENV NGX_DOC_ROOT="/data/web/html/"

ADD entrypoint.sh /bin/

CMD ["/usr/sbin/nginx","-g","daemon off;"]

ENTRYPOINT ["/bin/entrypoint.sh"]
[root@master img3]# 

[root@master img3]# docker build -t myweb:v0.3-5 ./

[root@master img3]# docker run --name myweb1 --rm -P myweb:v0.3-5 

[root@master img3]# docker exec -it 5e2987969396 /bin/sh
/ # ls
bin    dev    etc    home   lib    media  mnt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # cat /etc/nginx/conf.d/www.conf 

server {
    server_name 5e2987969396;
    listen 0.0.0.0:80;
    root /data/web/html/;
}
/ #
--------------------------------------------------------
实验18
加上主页
[root@master img3]# cat Dockerfile 
FROM nginx:1.14-alpine
LABEL maintainer="Wolf <1098331428@qq.com>"

ENV NGX_DOC_ROOT="/data/web/html/"

ADD index.html ${NGX_DOC_ROOT}
ADD entrypoint.sh /bin/

CMD ["/usr/sbin/nginx","-g","daemon off;"]

ENTRYPOINT ["/bin/entrypoint.sh"]            生产环境大部分这样写

[root@master img3]# ls
Dockerfile  entrypoint.sh  index.html
[root@master img3]# docker build -t myweb:v0.3-6 ./
[root@master img3]# docker run --name myweb1 --rm -P myweb:v0.3-6 

进入容器
[root@master img3]# docker exec -it myweb1 /bin/sh
/ # ls /etc/nginx/conf.d/www.conf 
/etc/nginx/conf.d/www.conf
/ # cat /etc/nginx/conf.d/www.conf 

server {
    server_name bb3995bf7afe;
    listen 0.0.0.0:80;
    root /data/web/html/;
}
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
-Y on/off    Use proxy

/ # wget -O - -q bb3995bf7afe
<h1>New Doc Root For nginx</h1>
/ # 

在看之前启动的窗口
127.0.0.1 - - [01/Jan/2019:07:02:16 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-"
172.17.0.6 - - [01/Jan/2019:07:02:40 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"

换个端口
[root@master img3]# docker run --name myweb1 --rm -P -e "PORT=8080" myweb:v0.3-6 
[root@master img3]# docker exec -it myweb1 /bin/sh
/ # netstat -tnp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN  

/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 nginx: master process /usr/sbin/nginx -g daemon off;   这里nginx pid为1
   10 nginx     0:00 nginx: worker process
   11 root      0:00 /bin/sh
   20 root      0:00 ps
------------------------------------------------------
实验19
[root@master img3]# cat Dockerfile 
FROM nginx:1.14-alpine
LABEL maintainer="Wolf <1098331428@qq.com>"

ENV NGX_DOC_ROOT="/data/web/html/"

ADD index.html ${NGX_DOC_ROOT}
ADD entrypoint.sh /bin/

EXPOSE 80/tcp

HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:${PORT:-80}/

CMD ["/usr/sbin/nginx","-g","daemon off;"]

ENTRYPOINT ["/bin/entrypoint.sh"]

[root@master img3]# docker build -t myweb:v0.3-7 ./

这里30秒自动检查一次
[root@master img3]# docker run --name myweb1 --rm -P -e "PORT=8080" myweb:v0.3-7 
127.0.0.1 - - [01/Jan/2019:07:38:11 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"
127.0.0.1 - - [01/Jan/2019:07:38:41 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"
127.0.0.1 - - [01/Jan/2019:07:39:11 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"
127.0.0.1 - - [01/Jan/2019:07:39:41 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"
127.0.0.1 - - [01/Jan/2019:07:40:12 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"
127.0.0.1 - - [01/Jan/2019:07:40:42 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"
--------------------------------------------------
实验20
搞一个失败的
[root@master img3]# cat Dockerfile 
FROM nginx:1.14-alpine
LABEL maintainer="Wolf <1098331428@qq.com>"

ENV NGX_DOC_ROOT="/data/web/html/"
ADD index.html ${NGX_DOC_ROOT}
ADD entrypoint.sh /bin/
EXPOSE 80/tcp
HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/    没这个端口
CMD ["/usr/sbin/nginx","-g","daemon off;"]
ENTRYPOINT ["/bin/entrypoint.sh"]
[root@master img3]# docker build -t myweb:v0.3-8 ./
[root@master img3]# docker exec -it myweb1 /bin/sh
/ # ls
bin    data   dev    etc    home   lib    media  mnt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # netstat -nlt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      

[root@master img3]# docker run --name myweb1 --rm -P -e "PORT=8080" myweb:v0.3-8
这里暂时没什么信息出来
----------------------------------------------------
实验21
[root@master img3]# cat Dockerfile 
FROM nginx:1.14-alpine

ARG author="Wolf <1098331428@qq.com>"    这里
 
LABEL maintainer="${author}"

ENV NGX_DOC_ROOT="/data/web/html/"

ADD index.html ${NGX_DOC_ROOT}
ADD entrypoint.sh /bin/

EXPOSE 80/tcp

HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/


CMD ["/usr/sbin/nginx","-g","daemon off;"]

ENTRYPOINT ["/bin/entrypoint.sh"]

[root@master img3]# docker build --build-arg author="laolang <1@qq.com>" -t myweb:v0.3-9 ./

[root@master img3]# docker image inspect myweb:v0.3-9
    "Labels": {
                "maintainer": "laolang <1@qq.com>"
----------------------------------------------------------------
实验22
[root@master ~]# cat img3/Dockerfile 
FROM nginx:1.14-alpine

ARG author="Wolf <1098331428@qq.com>"

LABEL maintainer="${author}"

ENV NGX_DOC_ROOT="/data/web/html/"

ADD index.html ${NGX_DOC_ROOT}
ADD entrypoint.sh /bin/

EXPOSE 80/tcp

HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/

ONBUILD ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/     给别人引用是使用

CMD ["/usr/sbin/nginx","-g","daemon off;"]

ENTRYPOINT ["/bin/entrypoint.sh"]
[root@master img3]# docker build -t myweb:v0.3-11 ./

Sending build context to Docker daemon  4.096kB
Step 1/11 : FROM nginx:1.14-alpine
 ---> c5b6f731fbc0
Step 2/11 : ARG author="Wolf <1098331428@qq.com>"
 ---> Using cache
 ---> d4e4e6be6577
Step 3/11 : LABEL maintainer="${author}"
 ---> Using cache
 ---> 9f658efd06e5
Step 4/11 : ENV NGX_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> 590098f3a4f3
Step 5/11 : ADD index.html ${NGX_DOC_ROOT}
 ---> Using cache
 ---> c09c452e01cb
Step 6/11 : ADD entrypoint.sh /bin/
 ---> Using cache
 ---> e513c46ff077
Step 7/11 : EXPOSE 80/tcp
 ---> Using cache
 ---> bb0253812192
Step 8/11 : HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/
 ---> Using cache
 ---> 3248975f5350
Step 9/11 : ONBUILD ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/   这里并没有下载
 ---> Running in bfc314792cf0
Removing intermediate container bfc314792cf0
 ---> 69b8628b2175
Step 10/11 : CMD ["/usr/sbin/nginx","-g","daemon off;"]
 ---> Running in ec32763eb170
Removing intermediate container ec32763eb170
 ---> 6142392642ca
Step 11/11 : ENTRYPOINT ["/bin/entrypoint.sh"]
 ---> Running in 6dea0e97365c
Removing intermediate container 6dea0e97365c
 ---> ead4bb0f4a5b
Successfully built ead4bb0f4a5b
Successfully tagged myweb:v0.3-11
[root@master img3]# 

[root@master img3]# docker build --build-arg author="laolang <1@qq.com>" -t myweb:v0.3-11 ./
Sending build context to Docker daemon  4.096kB
Step 1/11 : FROM nginx:1.14-alpine
 ---> c5b6f731fbc0
Step 2/11 : ARG author="Wolf <1098331428@qq.com>"
 ---> Using cache
 ---> d4e4e6be6577
Step 3/11 : LABEL maintainer="${author}"
 ---> Using cache
 ---> e8b3afaa33c2
Step 4/11 : ENV NGX_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> 8add3272b2f8
Step 5/11 : ADD index.html ${NGX_DOC_ROOT}
 ---> Using cache
 ---> f85d1a062166
Step 6/11 : ADD entrypoint.sh /bin/
 ---> Using cache
 ---> 4853edd7ef12
Step 7/11 : EXPOSE 80/tcp
 ---> Using cache
 ---> 8867a4941223
Step 8/11 : HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/
 ---> Using cache
 ---> 354cb57a95de
Step 9/11 : ONBUILD ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
 ---> Running in f91fae6fdadb
Removing intermediate container f91fae6fdadb
 ---> 11c5b5143652
Step 10/11 : CMD ["/usr/sbin/nginx","-g","daemon off;"]
 ---> Running in 07cf866d758a
Removing intermediate container 07cf866d758a
 ---> 3cd14d73d4bb
Step 11/11 : ENTRYPOINT ["/bin/entrypoint.sh"]
 ---> Running in c086fa2bf583
Removing intermediate container c086fa2bf583
 ---> 60d0970fc15c
Successfully built 60d0970fc15c
Successfully tagged myweb:v0.3-11
[root@master img3]# cd ..
[root@master ~]# mkdir img4
[root@master ~]# cat Dockerfile 
FROM myweb:v0.3-11              这里引用我自己做的镜像

RUN mkdir /tmp/test

[root@master ~]# docker build -t test:v0.1-1 ./   
Sending build context to Docker daemon  2.152MB
Step 1/2 : FROM myweb:v0.3-11
# Executing 1 build trigger
Downloading [==================================================>]  1.028MB/1.028MB    这里触发下载了
 ---> 8d244351d147
Step 2/2 : RUN mkdir /tmp/test
 ---> Running in bf6c090dd1b3
Removing intermediate container bf6c090dd1b3
 ---> ce7906fc7a75
Successfully built ce7906fc7a75

如果还不熟悉去官方仔细看,仔细读
https://github.com/docker-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!