docker commit
指令
進行鏡像的自動構建docker build
命令能夠將一些用戶可調用的命令行指令去構建對應的鏡像FROM
php
FROM <repository>[:tag]
: repostory是鏡像倉庫名稱,tag是鏡像的標籤,如不指定,默認是latestFROM <repository>@<digist>
:digest是鏡像的ID(校驗碼)MAINTAINERhtml
MAINTAINER "wanghui <wanghui@tencent.com>"
LABELnode
LABEL <key>=<value> <key1>=<value1>
必須
是咱們的工做目錄COPY <src>... <dest>
COPY ["<src>",...,"<dest>"]
:路徑中由空白字符時,使用此格式/
結尾[root@centos7-node1 ~]# mkdir /data/build_workshop -p [root@centos7-node1 ~]# cd /data/build_workshop/ [root@centos7-node1 build_workshop]# echo "<h1>this is a test page</h1>" > index.html [root@centos7-node1 ~]# cd /data/build_workshop/ [root@centos7-node1 build_workshop]# vim Dockerfile FROM busybox:latest LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY index.html /data/web/html/ [root@centos7-node1 build_workshop]# docker build . -t myimg:v0.1 [root@centos7-node1 build_workshop]# docker run --name t1 --rm -it myimg:v0.1 /bin/sh / # ls /data/web/html/ index.html [root@centos7-node1 build_workshop]# mkdir /data/build_workshop/pages/ [root@centos7-node1 build_workshop]# mv /data/build_workshop/index.html /data/build_workshop/pages/ [root@centos7-node1 build_workshop]# echo "<h1>test1 page</h1>" > /data/build_workshop/pages/test1.html [root@centos7-node1 build_workshop]# echo "<h1>test2 page</h1>" > /data/build_workshop/pages/test2.html [root@centos7-node1 build_workshop]# vim /data/build_workshop/.dockerignore #忽略拷貝文件 pages/test2.html [root@centos7-node1 build_workshop]# vim /data/build_workshop/Dockerfile FROM busybox:latest LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY pages/*.html /data/web/html/ [root@centos7-node1 build_workshop]# docker build . -t myimg:v0.2 [root@centos7-node1 build_workshop]# docker run --name t1 --rm -it myimg:v0.2 /bin/sh #驗證 / # ls /data/web/html/ index.html test1.html
ADD <src>... <dest>
ADD ["<src>","...","<dest>"]
/
結尾,則src指定的文件警備下載並直接建立爲dest/
結尾,則文件名URL指定的文件會被下載保存爲dest/filename
tar -x
命令,然而URL獲取的他人文件不會被自動展開/
結尾的目錄路徑/
結尾,則被視爲一個普通文件,src的內容將被寫入到dest[root@centos7-node1 ~]# mkdir /data/build_workshop/add_example [root@centos7-node1 ~]# cd /data/build_workshop/add_example [root@centos7-node1 add_example]# wget http://nginx.org/download/nginx-1.16.1.tar.gz [root@centos7-node1 add_example]# vim Dockerfile FROM busybox:latest LABEL maintainer="wanghui <wanghui@yeecall.com>" ADD http://nginx.org/download/nginx-1.18.0.tar.gz /data/ ADD nginx-1.16.1.tar.gz /data/ [root@centos7-node1 add_example]# docker run --name t1 --rm -it myimg:v0.3 /bin/sh / # ls /data/ nginx-1.16.1 nginx-1.18.0.tar.gz
WORKDIRmysql
語法:nginx
WORKDIR /var/log
WORKDIR $STATEPATH
: 能夠引用環境變量,結合ENV使用[root@centos7-node1 ~]# cd /data/build_workshop/ [root@centos7-node1 build_workshop]# cp add_example/ worksdir_example -r && cd worksdir_example [root@centos7-node1 worksdir_example]# vim Dockerfile ADD http://nginx.org/download/nginx-1.18.0.tar.gz /data/ FROM busybox:latest LABEL maintainer="wanghui <wanghui@yeecall.com>" WORKDIR /usr/ ADD nginx-1.16.1.tar.gz src/ [root@centos7-node1 worksdir_example]# docker build . -t myimg:v0.4 [root@centos7-node1 worksdir_example]# docker run --name t1 --rm -it myimg:v0.4 /bin/sh /usr # ls src/nginx-1.16.1/
VOLUME <mountpoint>
VOLUME ["<mountpoint>"]
[root@centos7-node1 build_workshop]# mkdir /data/build_workshop/volumes_example/pages/ [root@centos7-node1 build_workshop]# cd /data/build_workshop/volumes_example [root@centos7-node1 volumes_example]# echo "<h1>test1 page</h1>" > /data/build_workshop/volumes_example/pages/test1.html [root@centos7-node1 volumes_example]# echo "<h1>test2 page</h1>" > /data/build_workshop/volumes_example/pages/test2.html [root@centos7-node1 volumes_example]# vim Dockerfile FROM busybox:latest LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY pages/*.html /data/web/html/ ADD http://nginx.org/download/nginx-1.15.8.tar.gz /tmp/ WORKDIR /usr/ VOLUME /data/web/html/ [root@centos7-node1 volumes_example]# docker build . -t myimg:v0.5 [root@centos7-node1 volumes_example]# docker run --name t1 -it --rm myimg:v0.5 /bin/sh /usr # cd /data/web/html/ /data/web/html # ls index.html test1.html test2.html [root@centos7-node1 ~]# docker inspect -f {{.Mounts}} t1 #另起窗口,找到Source,發現Source映射目錄的內容與容器中的同樣[隨機存儲卷]
EXPOSE <port>[/<protocol>] ...
: 指定端口和協議EXPOSE 11211/udp 11211/tcp
:一次指定多個端口FROM busybox:latest LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY pages/*.html /data/web/html/ ADD http://nginx.org/download/nginx-1.15.8.tar.gz /tmp/ WORKDIR /usr/ VOLUME /data/web/html/ EXPOSE 80/tcp [root@centos7-node1 expose_example]# docker build . -t myimg:v0.6 [root@centos7-node1 volumes_example]# docker run --name t1 -it -P --rm myimg:v0.6 /bin/sh /usr # httpd -f -h /data/web/html/ #啓動httpd服務 [root@centos7-node1 expose_example]# docker port t1 #查看映射,而後訪問便可
$variable_name
或${variable_name}
ENV <key> <value>
: 一次只能設置一個變量ENV <key>=<value>
: 一次能夠設置多個變量,特數字答覆須要使用\
轉義[推薦使用]FROM busybox:latest ENV webhome="/data/web/html/" LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY pages/*.html ${webhome} ADD http://nginx.org/download/nginx-1.15.8.tar.gz ${webhome} WORKDIR ${webhome} ADD nginx-1.16.1.tar.gz ./ VOLUME ${webhome} EXPOSE 80/tcp [root@centos7-node1 env_example]# docker build . -t myimg:v0.7
ARG <name>=[default]
FROM busybox:latest ARG webhome="/data/web/html/" LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY pages/*.html ${webhome} ADD http://nginx.org/download/nginx-1.15.8.tar.gz ${webhome} WORKDIR ${webhome} ADD nginx-1.16.1.tar.gz ./ VOLUME ${webhome} EXPOSE 80/tcp [root@centos7-node1 arg_example]# docker build . -t myimg:v0.8 #正常不加參數的編譯,使用的是默認的參數 [root@centos7-node1 arg_example]# docker build --build-arg webhome="/webdata/htdocs/" . -t myimg:v0.8 #加入參數的編譯 [root@centos7-node1 arg_example]# docker run --name t1 -it --rm myimg:v0.8 /bin/sh #結果測試
RUN <command>
:command一般是一個shell命令,且以/bin/sh -c
來運行它, 這意味着此進程在容器中的PID不爲1,不能接受Unix信號,所以,當使用docker stop <container>
中止容器時,此進程接收不到SIGTERM信號RUN ["<executable>","param1","param2"]
:參數是一個json格式的數組,其中excutable是要運行的命令,後面的param是要傳遞給命令的選項或者參數,然而此種格式的參數不會以/bin/sh -c
來發起,所以常見的shell操做,如變量替換以及通配符操做將不會進行,不過,若是要運行依賴此shell特性的話,能夠將其替換成以下的格式: RUN ["bin/sh","-c","<executable>","<param1>"]
FROM busybox:latest ARG webhome="/data/web/html/" LABEL maintainer="wanghui<wanghui@yeecall.com>" COPY pages/*.html ${webhome} ADD http://nginx.org/download/nginx-1.15.8.tar.gz ${webhome} WORKDIR ${webhome} ADD nginx-1.16.1.tar.gz ./ VOLUME ${webhome} EXPOSE 80/tcp RUN mkdir -p /web/bbs && \ ["/bin/sh","-c","echo helle >> /web/bbs/index.html"] [root@centos7-node1 run_example]# docker build . -t myimg:v0.9
FROM centos:7 LABEL maintainer="wanghui <122725501@qq.com>" ARG docroot=/var/www/html/ RUN yum makecache && \ yum -y install httpd php php-mysql && \ yum clean all && \ rm -fr /var/cache/yum/* [root@centos7-node1 ap]# docker run --name web1 --rm -it php-httpd:v0.1 bash
docker run
的命令行所覆蓋CMD <command>
CMD ["<executable>","param1","param2"]
CMD ["param1","param2"]
: 用於爲ENTRYPOINT
提供默認參數FROM centos:7 LABEL maintainer="wanghui <122725501@qq.com>" ARG docroot=/var/www/html/ RUN yum makecache && \ yum -y install httpd php php-mysql && \ yum clean all && \ rm -fr /var/cache/yum/* CMD ["/usr/sbin/httpd","-DFOREGROUND"] [root@centos7-node1 ap]# docker build . -t php-httpd:v0.2 [root@centos7-node1 ap]# docker run --name web1 --rm php-httpd:v0.2 [root@centos7-node1 ap]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@centos7-node1 ap]# docker run --name web1 -it php-httpd:v0.2 /bin/bash [root@47818a3ba906 /]# ps -ef #httpd被覆蓋了 UID PID PPID C STIME TTY TIME CMD root 1 0 1 09:41 pts/0 00:00:00 /bin/bash
--entrypoint
選項參數能夠覆蓋ENTRYPOINT指定的程序ENTRYPOINT <command>
ENTRYPOINT ["<executable>","param1","param2"]
[root@centos7-node1 ap]# tree ./ ./ ├── Dockerfile ├── entrypoint.sh ├── ok.html └── phpinfo.php [root@centos7-node1 ap]# cat Dockerfile FROM centos:7 LABEL maintainer="wanghui <122725501@qq.com>" ARG docroot=/var/www/html/ RUN yum makecache && \ yum -y install curl httpd php php-mysql && \ yum clean all && \ rm -fr /var/cache/yum/* ADD ok.html phpinfo.php ${docroot} ADD entrypoint.sh /bin/ EXPOSE 80/tcp VOLUME ${docroot} HEALTHCHECK --interval=3s --timeout=3s --start-period=2s CMD curl -f http://localhost/ok.html || exit 1 CMD ["/usr/sbin/httpd","-DFOREGROUND"] ENTRYPOINT ["/bin/entrypoint.sh"] [root@centos7-node1 ap]# cat entrypoint.sh #!/bin/bash listen_port=${LISTEN_PORT:-80} server_name=${SERVER_NAME:-localhost} doc_root=${DOC_ROOT:-/var/www/html/} cat > /etc/httpd/conf.d/myweb.conf <<EOF #LISTEN $listen_port <VirtualHost *:${listen_port}> ServerName "$server_name" DocumentRoot "$doc_root" <Directory "$doc_root"> Options none AllowOverride none Require all granted </Directory> </VirtualHost> EOF exec "$@" Dockerfile entrypoint.sh ok.html phpinfo.php [root@centos7-node1 ap]# cat ok.html OK [root@centos7-node1 ap]# cat phpinfo.php <?php phpinfo(); ?>
USERweb
USAER <uid>|username
/etc/passwd
中的有效用戶,不然容器會運行失敗HEALTHCHECKsql
HEALTHCHECK CMD command
:檢查容器內的服務是否運行正常HEALTHCHECK none
:不須要健康檢查--interval
: 檢查週期,默認30s--timeout
:等待超時,默認30s--start-period
: 開始檢測的時間,默認是0s--retries
:重試次數ONBUILD <INSTRUCTION>
[root@centos7-node1 ~]# cd /data/build_workshop/ [root@centos7-node1 build_workshop]# mkdir testimg/ myicme/ -p [root@centos7-node1 build_workshop]# vim testimg/Dockerfile FROM busybox:latest LABEL maintainer="wanghui <122725501@qq.com>" RUN mkdir -p /data/web/html ONBUILD ADD http://nginx.org/download/nginx-1.18.0.tar.gz /usr/src [root@centos7-node1 build_workshop]# docker build ./testimg/ -t testimg:v0.1 # 構建testing:v0.1 [root@centos7-node1 build_workshop]# vim myicme/Dockerfile FROM testimg:v0.1 LABEL from="testimg:v0.1 [root@centos7-node1 build_workshop]# docker build ./myicme/ -t myicme:v0.1 #此時構建纔會執行下載testing:v0.1 Sending build context to Docker daemon 2.048kB Step 1/2 : FROM testimg:v0.1 # Executing 1 build trigger Downloading [==> ] 47.73kB/1.04MB
[root@centos7-node1 ~]# docker image save php-httpd:v0.6 -o php-httpd.tar #導出鏡像 [root@centos7-node1 ~]# docker image load -i php-httpd.tar # 導入鏡像 [root@centos7-node1 ~]# docker history php-httpd:v0.6 #構建歷史查看