本文收錄在容器技術學習系列文章總目錄html
基於容器製做鏡像,已經在上篇Docker系列06—基於容器製做鏡像並上傳到Docker Registry詳細講解過了,你們有須要能夠去上篇查找;這篇主要講解基於Dockerfile,使用docker build 命令製做鏡像。mysql
Docker中有個很是重要的概念叫作——鏡像(Image)。Docker 鏡像是一個特殊的文件系統,除了提供容器運行時所需的程序、庫、資源、配置等文件外,還包含了一些爲運行時準備的一些配置參數(如匿名卷、環境變量、用戶等)。鏡像不包含任何動態數據,其內容在構建以後也不會被改變。nginx
鏡像的定製實際上就是定製每一層所添加的配置、文件。若是咱們能夠把每一層修改、安裝、構建、操做的命令都寫入一個腳本,用這個腳原本構建、定製鏡像,那麼以前說起的沒法重複的問題、鏡像構建透明性的問題、體積的問題就都會解決。這個腳本就是 Dockerfile。web
Dockerfile 是一個文本文件,其內包含了一條條的指令(Instruction),每一條指令構建一層,所以每一條指令的內容,就是描述該層應當如何構建。sql
(1)Dockerfile 編寫的基本結構docker
Dockerfile 通常分爲四部分:基礎鏡像信息、維護者信息、鏡像操做指令和容器啓動時執行指令,’#’ 爲 Dockerfile 中的註釋。shell
(2)一臺主機能夠有多個Dockerfilejson
要使用多個Dockerfile 建立鏡像,能夠在不一樣目錄編寫Dockerfile,而後在Dockerfile 所在的目錄下構建新的鏡像;vim
注意:Dockerfile 中所包含的須要的內容;如COPY的文件、目錄等,都須要在Dockerfile 同級目錄下存在;數組
(3)docker build 基於dockerfile製做鏡像的命令
① 格式:
docker build [OPTIONS] PATH | URL | -
② 選項:
FROM <repository>[:<tag>] 或 FROM <repository>@<digest>
注:
# Description: test image FROM busybox:latest
MAINTAINER <authtor's detail>
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.com>"
COPY <src>... <dest> 或 COPY ["<src>",... "<dest>"]
注:
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.com>" COPY index.html /data/web/html/ #要確保dockerfile 同級路徑下有index.html文件
(2)在dockerfile同級目錄下準備好index.html文件
$ vim index.html <h1>Busybox httpd server</h1>
(3)使用build 製做鏡像
$ docker build -t busyboxhttpd:v0.1 ./ Sending build context to Docker daemon 3.072 kB Step 1/3 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/3 : MAINTAINER "Along <along@along.com>" ---> Running in 0d6f4c1c0e8d ---> 5d2cda1631d7 Removing intermediate container 0d6f4c1c0e8d Step 3/3 : COPY index.html /data/web/html/ ---> 2e0fe0eb0cb7 Removing intermediate container 536fdc71472a Successfully built 2e0fe0eb0cb7
(4)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 --rm busyboxhttpd:v0.1 cat /data/web/html/index.html <h1>Busybox httpd server</h1>
注:--rm:在容器關閉時,直接刪除容器,方便實驗。
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.com>" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/
注:若是是複製目錄,則其內部文件或子目錄會被遞歸複製,但<src>目錄自身不會被複制;須要把複製目錄名字也寫在容器中要複製的路徑下!
(2)在dockerfile同級目錄下準備好yum.repos.d 目錄
[root@along img1]# cp -r /etc/yum.repos.d/ ./ [root@along img1]# ls yum.repos.d along.repo docker-ce.repo epel-release-latest-7.noarch.rpm epel.repo epel-testing.repo
(3)使用build 製做鏡像
[root@along img1]# docker build -t busyboxhttpd:v0.2 ./ Sending build context to Docker daemon 27.65 kB Step 1/4 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/4 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/4 : COPY index.html /data/web/html/ ---> Using cache ---> 2e0fe0eb0cb7 Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/ ---> 68586925bdf9 Removing intermediate container a01f824efa3e Successfully built 68586925bdf9
(4)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 --rm busyboxhttpd:v0.2 ls /etc/yum.repos.d/ along.repo docker-ce.repo epel-release-latest-7.noarch.rpm epel-testing.repo epel.repo
ADD <src> .. <dest> 或 ADD ["<src>".. "<dest>"]
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.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 的<src> 是網上的nginx下載路徑
(2)使用build 製做鏡像
[root@along img1]# docker build -t busyboxhttpd:v0.3 ./ Sending build context to Docker daemon 27.65 kB Step 1/5 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/5 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/5 : COPY index.html /data/web/html/ ---> Using cache ---> 2e0fe0eb0cb7 Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/ ---> Using cache ---> 68586925bdf9 Step 5/5 : ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ Downloading 1.028 MB/1.028 MB ---> 91c71b469a9e Removing intermediate container abbad8dcdefb Successfully built 91c71b469a9e
(3)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 --rm busyboxhttpd:v0.3 ls /usr/local/src nginx-1.15.8.tar.gz
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.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/
(2)在dockerfile同級目錄下準備好yum.repos.d 目錄
[root@along img1]# wget http://nginx.org/download/nginx-1.15.8.tar.gz
(3)使用build 製做鏡像
[root@along img1]# docker build -t busyboxhttpd:v0.4 ./ Sending build context to Docker daemon 1.056 MB Step 1/5 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/5 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/5 : COPY index.html /data/web/html/ ---> Using cache ---> 2e0fe0eb0cb7 Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/ ---> Using cache ---> 68586925bdf9 Step 5/5 : ADD nginx-1.15.8.tar.gz /usr/local/src/ ---> f99baec4b992 Removing intermediate container 7f02f3fe649e
(4)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 --rm busyboxhttpd:v0.4 ls /usr/local/src /usr/local/src/nginx-1.15.8 /usr/local/src: nginx-1.15.8 /usr/local/src/nginx-1.15.8: CHANGES CHANGES.ru LICENSE README auto conf configure contrib html man src
WORKDIR <dirpath>
在Dockerfile文件中,WORKDIR指令可出現屢次,其路徑也能夠爲相對路徑,不過,其是相對此前一個WORKDIR指令指定的路徑;
另外,WORKDIR也可調用由ENV指定定義的變量;
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.com>" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ WORKDIR /usr/local/ ADD nginx-1.15.8.tar.gz ./src/
VOLUME <mountpoint> 或 VOLUME ["<mountpoint>"]
注:若是掛載點目錄路徑下此前在文件存在,docker run命令會在卷掛載完成後將此前的全部文件複製到新掛載的卷中
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.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
(2)使用build 製做鏡像
[root@along img1]# docker build -t busyboxhttpd:v0.5 ./ Sending build context to Docker daemon 1.056 MB Step 1/7 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/7 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/7 : COPY index.html /data/web/html/ ---> Using cache ---> 2e0fe0eb0cb7 Step 4/7 : COPY yum.repos.d /etc/yum.repos.d/ ---> Using cache ---> 68586925bdf9 Step 5/7 : WORKDIR /usr/local/ ---> Using cache ---> 84ad38ba3b4d Step 6/7 : ADD nginx-1.15.8.tar.gz ./src/ ---> Using cache ---> b32e992f51c5 Step 7/7 : VOLUME /data/mysql ---> Running in 270bd9a938eb ---> 90d592698082 Removing intermediate container 270bd9a938eb Successfully built 90d592698082
(3)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 --rm -it busyboxhttpd:v0.5 /bin/sh /usr/local # --- 另打開一個終端,查詢存儲卷 [root@along ~]# docker inspect -f {{.Mounts}} web1 [{volume b788b8a50d69953e2b086b3b54ba683154647319a481246cb7ab2ff927b21372 /var/lib/docker/volumes/b788b8a50d69953e2b086b3b54ba683154647319a481246cb7ab2ff927b21372/_data /data/mysql local true }]
EXPOSE <port>[/ <protocol>] [<port>[/ <protocol>] ....
注:
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.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
(2)使用build 製做鏡像
[root@along img1]# docker build -t busyboxhttpd:v0.6 ./ Sending build context to Docker daemon 1.056 MB Step 1/8 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/8 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/8 : COPY index.html /data/web/html/ ---> Using cache ---> 2e0fe0eb0cb7 Step 4/8 : COPY yum.repos.d /etc/yum.repos.d/ ---> Using cache ---> 68586925bdf9 Step 5/8 : WORKDIR /usr/local/ ---> Using cache ---> 84ad38ba3b4d Step 6/8 : ADD nginx-1.15.8.tar.gz ./src/ ---> Using cache ---> b32e992f51c5 Step 7/8 : VOLUME /data/mysql ---> Using cache ---> 90d592698082 Step 8/8 : EXPOSE 80/tcp ---> Running in 53b13561994d ---> 182463025992 Removing intermediate container 53b13561994d Successfully built 182463025992
(4)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 -P --rm -it busyboxhttpd:v0.6 /bin/httpd -f -h /data/web/html --- 另打開一個終端,驗證httpd 服務的80端口 [root@along ~]# docker inspect -f {{.NetworkSettings.IPAddress}} web1 #查詢容器的IP 172.17.0.2 [root@along ~]# curl 172.17.0.2:80 <h1>Busybox httpd server</h1> --- 在宿主機經過暴露的端口訪問httpd 服務 [root@along ~]# docker port web1 80/tcp -> 0.0.0.0:32768 [root@along ~]# curl 127.0.0.1:32768 <h1>Busybox httpd server</h1>
注:就算dockerfile 中有EXPOSE 指令暴露端口,可是不是真正的暴露;須要在啓動容器時,使用-P 選項真正的暴露端口。
ENV <key> <value> 或 ENV <key>=<value> . .
注:
(1)編寫dockerfile文件
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.com>" ENV DOC_ROOT=/data/web/html/ \ WEB_SERVER_PACKAGE="nginx-1.15.8" COPY index.html ${DOC_ROOT} 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 ${WEB_SERVER_PACKAGE}.tar.gz ./src/ VOLUME /data/mysql EXPOSE 8080:80/tcp
(2)使用build 製做鏡像
Sending build context to Docker daemon 1.056 MB Step 1/9 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/9 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/9 : ENV DOC_ROOT /data/web/html/ WEB_SERVER_PACKAGE "nginx-1.15.8" ---> Running in b268b672236a ---> 295bf19a8395 Removing intermediate container b268b672236a Step 4/9 : COPY index.html ${DOC_ROOT} ---> c29d3c8f6e0d Removing intermediate container 33238b2ba6bf Step 5/9 : COPY yum.repos.d /etc/yum.repos.d/ ---> 3a2a2f571b07 Removing intermediate container bc49392442bc Step 6/9 : WORKDIR /usr/local/ ---> a695a7f0265f Removing intermediate container aa12de3e47db Step 7/9 : ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/ ---> aca798ede129 Removing intermediate container 4cf601151915 Step 8/9 : VOLUME /data/mysql ---> Running in 497595d537a7 ---> 8f52b722011c Removing intermediate container 497595d537a7 Step 9/9 : EXPOSE 8080:80/tcp ---> Running in 1dac584db62d ---> b10c56601efb Removing intermediate container 1dac584db62d Successfully built b10c56601efb
(3)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web1 -P --rm -it busyboxhttpd:v0.7 ls /usr/local/src /data/web/html /data/web/html: index.html /usr/local/src: nginx-1.15.8 --- 也可使用printenv 查看變量驗證 [root@along ~]# docker run --name web1 --rm -it busyboxhttpd:v0.7 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE=nginx-1.15.8 --- 在啓動容器時,使用docker run -e 設置修改變量 [root@along ~]# docker run --name web1 -e WEB_SERVER_PACKAGE=nginx-1.15.7 --rm -it busyboxhttpd:v0.7 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin WEB_SERVER_PACKAGE=nginx-1.15.7 DOC_ROOT=/data/web/html/
RUN <command> 或 RUN ["<executable>", "<param1>", "<param2>"]
注:
(1)編寫dockerfile文件:使用RUN 執行解壓命令
# Description: test image FROM busybox:latest MAINTAINER "Along <along@along.com>" ENV DOC_ROOT=/data/web/html/ \ WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz" COPY index.html ${DOC_ROOT} COPY yum.repos.d /etc/yum.repos.d/ WORKDIR /usr/local/ ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} ./src/ #ADD ${WEB_SERVER_PACKAGE} ./src/ VOLUME /data/mysql EXPOSE 8080:80/tcp RUN cd ./src && \ tar -xf ${WEB_SERVER_PACKAGE}
(2)使用build 製做鏡像
[root@along img1]# docker build -t busyboxhttpd:v0.8 ./ Sending build context to Docker daemon 1.056 MB Step 1/10 : FROM busybox:latest ---> 758ec7f3a1ee Step 2/10 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/10 : ENV DOC_ROOT /data/web/html/ WEB_SERVER_PACKAGE "nginx-1.15.8.tar.gz" ---> Running in ff660867b971 ---> fef5c3f0295a Removing intermediate container ff660867b971 Step 4/10 : COPY index.html ${DOC_ROOT} ---> 825ae434ab56 Removing intermediate container 0e22f2a6b9c2 Step 5/10 : COPY yum.repos.d /etc/yum.repos.d/ ---> 33b5aa2e5b9b Removing intermediate container 51722b8fcee3 Step 6/10 : WORKDIR /usr/local/ ---> 31912ce0b9b1 Removing intermediate container ecfe195635a2 Step 7/10 : ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} ./src/ Downloading 1.028 MB/1.028 MB ---> 5d46a9337dfe Removing intermediate container d38191e8b1ff Step 8/10 : VOLUME /data/mysql ---> Running in 5c18e8b76af2 ---> 2d1ee498340b Removing intermediate container 5c18e8b76af2 Step 9/10 : EXPOSE 8080:80/tcp ---> Running in 8c32c08cdb78 ---> 17700871c846 Removing intermediate container 8c32c08cdb78 Step 10/10 : RUN cd ./src && tar -xf ${WEB_SERVER_PACKAGE} ---> Running in 555b43cc59db ---> 21973fc25ad4 Removing intermediate container 555b43cc59db Successfully built 21973fc25ad4
(3)基於此新建鏡像運行容器,進行驗證:已經執行了解壓命令
[root@along ~]# docker run --name web1 -P --rm -it busyboxhttpd:v0.7 ls /usr/local/src nginx-1.15.8
CMD <command> 或 CMD [「<executable>",「<param1>","<param2>"] 或 CMD ["<param1>","<param2>"]
注:
(1)編寫dockerfile文件
FROM busybox LABEL maintainer="Along <along@along.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}
(2)使用build 製做鏡像
[root@along image2]# docker build -t busyboxhttpd:v1.1 ./ Sending build context to Docker daemon 2.048 kB Step 1/5 : FROM busybox ---> 758ec7f3a1ee Step 2/5 : LABEL maintainer "Along <along@along.com>" app "httpd" ---> Using cache ---> ec064f9da09a Step 3/5 : ENV WEB_DOC_ROOT "/data/web/html" ---> Using cache ---> fedfecef57c8 Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html ---> Running in a9b9f66db1fd ---> 7ab194ffda82 Removing intermediate container a9b9f66db1fd Step 5/5 : CMD /bin/httpd -f -h ${WEB_DOC_ROOT} ---> Running in ecb0430965d0 ---> 3ec2f9b160b7 Removing intermediate container ecb0430965d0 Successfully built 3ec2f9b160b7
(3)基於此新建鏡像運行容器,進行驗證,httpd正常運行
[root@along ~]# docker run --name web2 --rm -d busyboxhttpd:v1.1 20aa07198943887db51173e245392f75e3654525cb32242f2b04f0b3e007e47d [root@along ~]# docker inspect -f {{.NetworkSettings.IPAddress}} web2 172.17.0.2 [root@along ~]# curl 172.17.0.2 <h1>Busybox httpd server</h1> --- 也可使用exec 進入web2容器內進行驗證 [root@along ~]# docker exec -it web2 /bin/sh / # ps PID USER TIME COMMAND 1 root 0:00 /bin/httpd -f -h /data/web/html 5 root 0:00 /bin/sh 9 root 0:00 ps / # printenv WEB_DOC_ROOT=/data/web/html / # exit --- 使用CMD定義的命令,在啓動容器時,會被後面追加的指令覆蓋;與下面ENTRYPOINT 指令對比 [root@along ~]# docker kill web2 web2 [root@along ~]# docker run --name web2 --rm busyboxhttpd:v1.1 ls / bin data dev etc [root@along ~]# curl 172.17.0.2 被ls /覆蓋,因此沒有執行httpd服務
ENTR YPOINT <command> ENTRYPOINT ["<executable>", "<param1>", "<param2>"]
注:
(1)編寫dockerfile文件
FROM busybox LABEL maintainer="Along <along@along.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 ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
(2)使用build 製做鏡像
[root@along image2]# docker build -t busyboxhttpd:v1.2 ./ Sending build context to Docker daemon 2.048 kB Step 1/5 : FROM busybox ---> 758ec7f3a1ee Step 2/5 : LABEL maintainer "Along <along@along.com>" app "httpd" ---> Using cache ---> ec064f9da09a Step 3/5 : ENV WEB_DOC_ROOT "/data/web/html" ---> Using cache ---> fedfecef57c8 Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html ---> Using cache ---> 7ab194ffda82 Step 5/5 : ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT} ---> Running in aebee4cd3d4f ---> 2e3853256eab Removing intermediate container aebee4cd3d4f Successfully built 2e3853256eab
(3)基於此新建鏡像運行容器,進行驗證
[root@along ~]# docker run --name web2 --rm busyboxhttpd:v1.2 ls / 發現是不會執行ls / 這個命令;仍然執行的是ENTRYPOINT中設置的命令;與上面CMD 指令對比; [root@along ~]# curl 172.17.0.2 #httpd服務仍然執行,沒有被ls / 指令覆蓋 <h1>Busybox httpd server</h1>
HEALTHCHECK [OPTIONS] CMD command (經過在容器內運行命令來檢查容器運行情況) HEALTHCHECK NONE (禁用從基礎映像繼承的任何運行情況檢查)
(1)OPTIONS 選項:
(2)返回值:
(1)編寫dockerfile文件
檢測web2容器的10080端口(其實打開的是80端口,10080並無,全部會檢測失敗)
FROM busybox LABEL maintainer="Along <along@along.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} HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/
(2)使用build 製做鏡像
[root@docker2 image2]# docker build -t busyboxhttpd:v1.3 ./ Sending build context to Docker daemon 2.048kB Step 1/6 : FROM busybox ---> 3a093384ac30 Step 2/6 : LABEL maintainer="Along <along@along.com>" app="httpd" ---> Using cache ---> 164a0af1b5dc Step 3/6 : ENV WEB_DOC_ROOT="/data/web/html" ---> Using cache ---> 930023fca80b Step 4/6 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html ---> Using cache ---> 1df364fca443 Step 5/6 : CMD /bin/httpd -f -h ${WEB_DOC_ROOT} ---> Running in 671f9491d0c3 Removing intermediate container 671f9491d0c3 ---> 71451fa629e9 Step 6/6 : HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:${PORT:-80}/ ---> Running in 474395f265da Removing intermediate container 474395f265da ---> 757ab550ea25 Successfully built 757ab550ea25 Successfully tagged busyboxhttpd:v1.3
(3)基於此新建鏡像運行容器,進行驗證
[root@docker2 ~]# docker run --name web2 --rm -d busyboxhttpd:v1.3 7060a6717e77acecd683a6b05332ab7cec128b836e6aa2d7d5ce8fb1b38b7fd6 --- 容器剛建立,還沒檢測完時,容器仍是health 健康狀態 [root@docker2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7060a6717e77 busyboxhttpd:v1.3 "/bin/sh -c '/bin/ht…" 3 seconds ago Up 3 seconds (health: starting) web2 --- 30s 檢測完時,沒有檢測到10080端口,容器變爲unhealthy不健康狀態 [root@docker2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7060a6717e77 busyboxhttpd:v1.3 "/bin/sh -c '/bin/ht…" 30 seconds ago Up 30 seconds (unhealthy) web2
ONBUILD < Instruction>
(1)編寫第一個Dockerfile文件,準備做爲第二個Dockerfile文件的FROM基礎鏡像
FROM busybox MAINTAINER "Along <along@along.com>" ENV WEB_DOC_ROOT="/data/web/html" RUN mkdir -p ${WEB_DOC_ROOT} && \ echo "<h1>Busybox httpd server1</h1>" > ${WEB_DOC_ROOT}/index.html ONBUILD RUN echo "<h1>Busybox httpd server2</h1>" >> /data/web/html/index.html
(2)編寫第2個Dockerfile文件,FROM 基於第1個Dockerfile
FROM busyboxhttpd:v2.1
(3)基於2個Dockerfile文件新建鏡像,並運行容器,進行驗證
① 基於第1個Dockerfile文件新建鏡像
[root@along image3]# docker build -t busyboxhttpd:v2.1 ./ Sending build context to Docker daemon 2.048 kB Step 1/5 : FROM busybox ---> 758ec7f3a1ee Step 2/5 : MAINTAINER "Along <along@along.com>" ---> Using cache ---> 5d2cda1631d7 Step 3/5 : ENV WEB_DOC_ROOT "/data/web/html" ---> Running in 210437fc0778 ---> 0a3becd99802 Removing intermediate container 210437fc0778 Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server1</h1>" > ${WEB_DOC_ROOT}/index.html && /bin/chown nobody ${WEB_DOC_ROOT}/index.html /bin/httpd ---> Running in e370d8843211 ---> 4c1f59085f78 Removing intermediate container e370d8843211 Step 5/5 : ONBUILD run echo "<h1>Busybox httpd server2</h1>" >> /data/web/html/index.html ---> Running in f47258319e1d ---> 232b1c393634 Removing intermediate container f47258319e1d Successfully built 232b1c393634
② 基於第2個Dockerfile文件新建鏡像
[root@along ~]# docker build -t busyboxhttpd:v2.2 ./ Sending build context to Docker daemon 2.467 MB Step 1/1 : FROM busyboxhttpd:v2.1 # Executing 1 build trigger... Step 1/1 : RUN echo "<h1>Busybox httpd server2</h1>" >> /data/web/html/index.html ---> Using cache ---> a2a6ae382228 Successfully built a2a6ae382228
③ 基於二個新鏡像啓動容器驗證
[root@along ~]# docker run --name web2 --rm busyboxhttpd:v2.1 cat /data/web/html/index.html <h1>Busybox httpd server1</h1> --- 證實ONBUILD指令,只在第2個Dockerfile文件中生效 [root@along ~]# docker run --name web2 --rm busyboxhttpd:v2.2 cat /data/web/html/index.html <h1>Busybox httpd server1</h1> <h1>Busybox httpd server2</h1>
USER <UID>| <U JserName >
須要注意的是,<UID>能夠爲任意數字,但實踐中其必須爲/etc/ passwd中某用戶的有效UID,不然,docker run命令將運行失敗
ARG <name>[= <default value>]
Dockerfile能夠包含一個或多個ARG指令
15.2.2 示例
(1)在dockerfile 中定義ARG
FROM busybox:latest ARG author="Along <along@along.com>" MAINTAINER $ARG
(2)構建此時鏡像時,修改此age
docker build --build-arg author="Tom <tom@tom.com>" -t test:v0.1 ./
SHELL ["executable", "parameters"]
STOPSIGNAL signal