一、继续上章节Docker学习7 CMD命令后。

  11、ENTRYPOINT

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

    Docker 学习8 Dockerfile详解2 随笔 第1张

    a、容器启动后相当于会启动ENTRYPOINT + CMD 命令,CMD相当于参数传给entrypoint的

[root@localhost images2]# cat Dockerfile 
FROM  busybox
LABEL maintainer="wohaoshuai <wohaoshuai@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 ${WEB_DOC_ROOT}"]

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

[root@localhost images2]# docker build -t wohaoshuaihttpd:v0.2-5 ./
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM  busybox
 ---> af2f74c517aa
Step 2/5 : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
 ---> Using cache
 ---> b94158ebd25b
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html"
 ---> Using cache
 ---> 128dc125c148
Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} &&     echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
 ---> Using cache
 ---> 79e6d697305f
Step 5/5 : ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
 ---> Running in 322af685179f
Removing intermediate container 322af685179f
 ---> 26837ef4211b
Successfully built 26837ef4211b
Successfully tagged wohaoshuaihttpd:v0.2-5

[root@localhost images2]# docker run -it --rm -P --name wohaoshuai1 wohaoshuaihttpd:v0.2-5 ls /data/web/html


[root@localhost images2]# docker inspect -f {{.Args}} wohaoshuai1   #此时可以看到我们覆盖的CMD命令自动追加到了ENTRYPOINT提供的命令后面,相当于容器一启动的时候就会启动 ENTRYPOINT + CMD命令
[-c /bin/httpd -f -h ${WEB_DOC_ROOT} ls /data/web/html]

    b、ENTRYPOINT + CMD组合

[root@localhost images2]# cat Dockerfile 
FROM  busybox
LABEL maintainer="wohaoshuai <wohaoshuai@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}"]

ENTRYPOINT ["/bin/sh","-c"]

[root@localhost images2]# docker build -t wohaoshuaihttpd:v0.2-7 ./
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM  busybox
 ---> af2f74c517aa
Step 2/6 : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
 ---> Using cache
 ---> b94158ebd25b
Step 3/6 : ENV WEB_DOC_ROOT="/data/web/html"
 ---> Using cache
 ---> 128dc125c148
Step 4/6 : RUN mkdir -p ${WEB_DOC_ROOT} &&     echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
 ---> Using cache
 ---> 79e6d697305f
Step 5/6 : CMD ["/bin/httpd -f -h ${WEB_DOC_ROOT}"]
 ---> Running in 633c17c96e88
Removing intermediate container 633c17c96e88
 ---> 997c922b10f0
Step 6/6 : ENTRYPOINT ["/bin/sh","-c"]
 ---> Running in 3838dded3dfe
Removing intermediate container 3838dded3dfe
 ---> 9df599309f9a
Successfully built 9df599309f9a
Successfully tagged wohaoshuaihttpd:v0.2-7
[root@localhost images2]# docker run -it --rm -P --name wohaoshuai1 wohaoshuaihttpd:v0.2-7


[root@localhost ~]# docker inspect -f {{.Config.Cmd}}  wohaoshuai1 
[/bin/httpd -f -h ${WEB_DOC_ROOT}]
[root@localhost ~]# docker inspect -f {{.Config.Entrypoint}} wohaoshuai1 
[/bin/sh -c]

[root@localhost images2]# docker run -it --rm -P --name wohaoshuai1 wohaoshuaihttpd:v0.2-7 ls /data
bin data dev etc home proc root sys tmp usr var
[root@localhost images2]#

  c、启动nginx案例

[root@localhost images3]# cat Dockerfile 
FROM nginx:1.14-alpine
LABEL maintainer="wohaoshuai <wohaoshuai@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@localhost images3]# 
[root@localhost images3]# 
[root@localhost images3]# cat entrypoint.sh 
#!/bin/sh 
#
cat > /etc/nginx/conf.d/www.conf <<EOF
server {    
    server_name $HOSTNAME;
    listen ${IP:-0.0.0.0}:${PORT:-80};
    root    ${NGX_DOC_ROOT:-/usr/share/nginx/html};
}
EOF

#执行所有参数
exec "$@" 


[root@localhost images3]# docker build -t wohaoshuaihttpd:v0.3-6 ./
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM nginx:1.14-alpine
 ---> 8a2fb25a19f5
Step 2/7 : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
 ---> Using cache
 ---> d073a723c02f
Step 3/7 : ENV NGX_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> e1f5eb1ad38b
Step 4/7 : ADD index.html ${NGX_DOC_ROOT}
 ---> e070ca432d81
Step 5/7 : ADD entrypoint.sh /bin/
 ---> f45f6cba97b6
Step 6/7 : CMD ["/usr/sbin/nginx","-g","daemon off;"]
 ---> Running in c4e622170dc8
Removing intermediate container c4e622170dc8
 ---> 0bfde2a829f4
Step 7/7 : ENTRYPOINT ["/bin/entrypoint.sh"]
 ---> Running in bb3787180bc6
Removing intermediate container bb3787180bc6
 ---> 0ae588a1c9ff
Successfully built 0ae588a1c9ff
Successfully tagged wohaoshuaihttpd:v0.3-6
[root@localhost images3]# docker run --name wohaoshuai1 --rm -P  wohaoshuaihttpd:v0.3-6
172.17.0.1 - - [22/Apr/2019:08:53:10 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.2 - - [22/Apr/2019:09:03:48 +0000] "GET / HTTP/1.1" 200 32 "-" "Wget" "-"

[root@localhost ~]# docker exec -it wohaoshuai1 /bin/sh
/ # cat /etc/nginx/conf.d/
default.conf  www.conf
/ # cat /etc/nginx/conf.d/www.conf 
server {    
    server_name 9243f356a5b7;
    listen 0.0.0.0:80;
    root    /data/web/html/;
}
/ # wget -O - -q 9243f356a5b7
<h1>NEW DOC ROOT for NGINX</h1>
/ # ps

  PID USER TIME COMMAND
  1 root 0:00 nginx: master process /usr/sbin/nginx -g daemon off;  #因为脚本中使用了exec 因此确保了主进程ID号为 1
  9 nginx 0:00 nginx: worker process
  18 root 0:00 /bin/sh
  24 root 0:00 ps

   12、USER

    Docker 学习8 Dockerfile详解2 随笔 第2张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄