先查看下本地的鏡像,選取官網的centos做爲base image:html
[root@server ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 1e1148e4cc2c 5 weeks ago 202MB
建立一個目錄用於專門存放此demo的目錄,也就是Dockerfile所在的目錄nginx
[root@server ~]# mkdir myNginx [root@server ~]# cd myNginx/ [root@server myNginx]# touch Dockerfile [root@server myNginx]# ll 總用量 0 -rw-r--r-- 1 root root 0 1月 10 17:34 Dockerfile
下面開始編寫Dockerfile 文件,(注意Dockerfile的D須要大寫)c++
v1版:docker
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos # MAINTAINER MAINTAINER 381347268@qq.com # 安裝基礎工具包 RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下載nginx ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解壓nginx 並建立用戶 RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx # 更改工做目錄 WORKDIR /usr/local/src/nginx-1.12.2 # 編譯安裝nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 刪除多餘安裝包 RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz # 設置環境變量 ENV PATH=/usr/local/nginx/sbin:$PATH # 設置端口 EXPOSE 80
執行docker build 進行構建:json
[root@server myNginx]# ll 總用量 4 -rw-r--r-- 1 root root 1268 1月 10 17:40 Dockerfile [root@server myNginx]# docker build -t centos_nginx:v1 .
此處構建有點慢,由於須要安裝編譯nginx須要用到的軟件,及下載nginx;後面的.表明的是相對路徑的當前目錄,若是須要全路徑則爲/root/myNginx(就是找到Dockerfile文件)centos
構建成功後,查看新構建的鏡像:緩存
[root@server myNginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx v1 13d959cbec2b About a minute ago 504MB centos latest 1e1148e4cc2c 5 weeks ago 202MB
使用v1版本的鏡像啓動一個容器:bash
[root@server myNginx]# docker run -d -p 80:80 centos_nginx:v1 nginx -g "daemon off;" 961ef4274078b61a56667293ef69ee881fbf4171156c7990c94225eb508a172c
查看容器運行狀態:dom
[root@server myNginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 961ef4274078 centos_nginx:v1 "nginx -g 'daemon of…" 39 seconds ago Up 38 seconds 0.0.0.0:80->80/tcp mystifying_goldberg
進行訪問:tcp
這樣基於Dockerfile的一個簡單的實例構建完成,如今基於這個Dockerfile文件依次添加其它指令進行構建
添加CMD 指令:
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos # MAINTAINER MAINTAINER 381347268@qq.com # 安裝基礎工具包 RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下載nginx ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解壓nginx 並建立用戶 RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx # 更改工做目錄 WORKDIR /usr/local/src/nginx-1.12.2 # 編譯安裝nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 刪除多餘安裝包 RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz # 設置環境變量 ENV PATH=/usr/local/nginx/sbin:$PATH # 設置端口 EXPOSE 80 CMD /bin/sh -c 'nginx -g "daemon off;"'
而後進行構建v2版本:
[root@server myNginx]# docker build -t centos_nginx:v2 . Sending build context to Docker daemon 3.072kB Step 1/11 : FROM centos ---> 1e1148e4cc2c Step 2/11 : MAINTAINER 381347268@qq.com ---> Using cache ---> d41475691003 Step 3/11 : RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data ---> Using cache ---> 7739a6cdd37c Step 4/11 : ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ Downloading [==================================================>] 981.7kB/981.7kB ---> Using cache ---> a58676d797ad Step 5/11 : RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ && useradd -M -s /sbin/nologin nginx ---> Using cache ---> 7f671453fc20 Step 6/11 : WORKDIR /usr/local/src/nginx-1.12.2 ---> Using cache ---> be296a9725ea Step 7/11 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ---> Using cache ---> fcbc1da221cc Step 8/11 : RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz ---> Using cache ---> 2189ac28a36f Step 9/11 : ENV PATH=/usr/local/nginx/sbin:$PATH ---> Using cache ---> 70faf0a90e9f Step 10/11 : EXPOSE 80 ---> Using cache ---> 13d959cbec2b Step 11/11 : CMD /bin/sh -c 'nginx -g "daemon off;"' ---> Running in d07896b5ba56 Removing intermediate container d07896b5ba56 ---> 3b71646263d3 Successfully built 3b71646263d3 Successfully tagged centos_nginx:v2
因爲在構建的過程當中docker 會採用緩存機制,上面構建過程當中包含不少using cache,因此此次構件很是快,若是須要從新構建,不想使用cache 須要添加 --no-cache
查看v2版本的鏡像:
[root@server myNginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx v2 3b71646263d3 6 minutes ago 504MB
使用v2版本的鏡像啓動一個容器:
[root@server myNginx]# docker run -d -p 81:80 centos_nginx:v2 934ddc6f56a48f59b405c75c19b40ce5156388e34de37c078a56abc7c08a0602
而後查看容器狀態:
[root@server myNginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 934ddc6f56a4 centos_nginx:v2 "/bin/sh -c '/bin/sh…" 43 seconds ago Up 42 seconds 0.0.0.0:81->80/tcp pedantic_cocks
進行訪問:
新增長的CMD /bin/sh -c 'nginx -g "daemon off;"' 表示
當啓動一個容器時候默認運行的命令,若是在啓動容器時賦予了command的話,那麼定義的CMD中的命令將不會被執行,而會去執行command的命令
添加ENTRYPOINT 指令:
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos # MAINTAINER MAINTAINER 381347268@qq.com # 安裝基礎工具包 RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下載nginx ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解壓nginx 並建立用戶 RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx # 更改工做目錄 WORKDIR /usr/local/src/nginx-1.12.2 # 編譯安裝nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 刪除多餘安裝包 RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz # 設置環境變量 ENV PATH=/usr/local/nginx/sbin:$PATH # 設置端口 EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g", "daemon off;"]
當ENTRYPOINT和CMD連用時,CMD的命令是ENTRYPOINT命令的參數,二者連用至關於nginx -g "daemon off;" ;而當一塊兒連用的命令格式最好一致(此處選擇的都是json格式)
而後進行構建v3版本:
[root@server myNginx]# docker build -t centos_nginx:v3 .
查看v3版本的鏡像:
[root@server myNginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx v3 1aeba273c0af 21 seconds ago 504MB
使用v3版本的鏡像啓動一個容器:
[root@server myNginx]# docker run -d -p 82:80 centos_nginx:v3 2932d58ee8969587b04fa0155aadeb4b63a1934962d384624a8d7359725dfad3
而後查看容器狀態:
[root@server myNginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2932d58ee896 centos_nginx:v3 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:82->80/tcp boring_gauss
進行訪問:
這裏示例一個默認將nginx關閉的示例v3.1版:
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos # MAINTAINER MAINTAINER 381347268@qq.com # 安裝基礎工具包 RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下載nginx ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解壓nginx 並建立用戶 RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx # 更改工做目錄 WORKDIR /usr/local/src/nginx-1.12.2 # 編譯安裝nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 刪除多餘安裝包 RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz # 設置環境變量 ENV PATH=/usr/local/nginx/sbin:$PATH # 設置端口 EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g", "daemon on;"]
CMD 的命令修改成了後臺,咱們知道若是容器內的進程在後臺運行那麼容器將不會運行,構建v3.1版本:
[root@server myNginx]# docker build -t centos_nginx:v3.1 .
查看v3.1版本的鏡像:
[root@server myNginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx v3.1 ec17ab98a424 12 seconds ago 504MB
使用v3.1版本的鏡像啓動一個容器,可是在啓動的時候添加後面的command:
[root@server myNginx]# docker run -d -p 83:80 centos_nginx:v3.1 -g "daemon off;" 0143d4b91ca1f97be31f0427140dfb17bb4ad9530d4a4a19b70c93044f6332c5
能夠看見在後面新增了 -g "daemon off;",前面提過若是在啓動容器時增長了命令,那麼Dockerfile中的CMD中的命令將不會生效
查看容器運行狀態:
[root@server myNginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0143d4b91ca1 centos_nginx:v3.1 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:83->80/tcp loving_burnell
能夠看見容器的運行絲毫沒有問題,因而nginx 的服務依然仍是在前臺運行,沒有被影響,而新增的command (-g "daemon off;")將做爲ENTRYPOINT的新的參數以此爲準,因而進行訪問:
添加VOLUME指令:
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos # MAINTAINER MAINTAINER 381347268@qq.com # 安裝基礎工具包 RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下載nginx ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解壓nginx 並建立用戶 RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx # 掛載數據目錄 VOLUME ["/usr/local/nginx/html"] # 更改工做目錄 WORKDIR /usr/local/src/nginx-1.12.2 # 編譯安裝nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 刪除多餘安裝包 RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz # 設置環境變量 ENV PATH=/usr/local/nginx/sbin:$PATH # 設置端口 EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g"]
而後進行構建v4版本:
[root@server myNginx]# docker build -t centos_nginx:v4 .
查看v4版本的鏡像:
[root@server myNginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx v4 b142047c1a6e 19 seconds ago 504MB
使用v4版本的鏡像啓動一個容器:
[root@server myNginx]# docker run -d -p 84:80 --name nginx4 centos_nginx:v4 -g "daemon off;" 1d191f9bc49dc01ee2657508eba94ff1d02e5b4304eaf6ed5161e4b9000577ea
而後查看容器狀態:
[root@server myNginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d191f9bc49d centos_nginx:v4 "nginx -g 'daemon of…" 36 seconds ago Up 34 seconds 0.0.0.0:84->80/tcp nginx4
經過docker inspect 能夠看到宿主機上面的掛載目錄路徑:
[root@server ~]# docker inspect nginx4 "Mounts": [ { "Type": "volume", "Name": "5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf", "Source": "/var/lib/docker/volumes/5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf/_data", "Destination": "/usr/local/nginx/html", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],
進入到宿主機的掛載目錄而且查看目錄內的文件:
[root@server ~]# cd /var/lib/docker/volumes/5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf/_data [root@server _data]# pwd /var/lib/docker/volumes/5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf/_data [root@server _data]# ll 總用量 8 -rw-r--r-- 1 1001 1001 537 10月 17 2017 50x.html -rw-r--r-- 1 1001 1001 612 10月 17 2017 index.html
利用docker exec 進入到容器中,查看卷:
[root@server myNginx]# docker exec -it nginx4 /bin/bash [root@d2d2a2f287c6 nginx-1.12.2]# cd /usr/local/nginx/html/ [root@d2d2a2f287c6 html]# pwd /usr/local/nginx/html [root@d2d2a2f287c6 html]# ll total 8 -rw-r--r-- 1 1001 1001 537 Oct 17 2017 50x.html -rw-r--r-- 1 1001 1001 612 Oct 17 2017 index.html
如今在本地宿主機上的這個目錄修改index.html文件內容:
[root@server _data]# echo "<h1>Hello Docker.</h1>" > index.html [root@server _data]# cat index.html <h1>Hello Docker.</h1>
而後切換到容器中查看這個文件是否發生改變:
[root@d2d2a2f287c6 html]# cat index.html
<h1>Hello Docker.</h1>
進行訪問:
經過訪問發現,在宿主機上面進行了更改,容器內部也發生了變化,這樣就動態的實現網站數據動態更改。
添加ONBUILD 指令:
Dockerfile1中base image 爲A鏡像,並在Dockefile1中定義ONBUILD指令,構建成新的鏡像B鏡像
Dockerfile2中base image 爲B鏡像,構建成新鏡像C
當使用鏡像B啓動容器1不會執行OBNUILD中定義的內容,而使用C鏡像啓動的容器2則會執行ONBUILD定義的內容
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos # MAINTAINER MAINTAINER 381347268@qq.com # 安裝基礎工具包 RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下載nginx ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解壓nginx 並建立用戶 RUN tar -xvzf /opt/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx # 掛載數據目錄 ONBUILD VOLUME ["/data"] # 更改工做目錄 WORKDIR /usr/local/src/nginx-1.12.2 # 編譯安裝nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 刪除多餘安裝包 RUN rm -rf /opt/nginx/nginx-1.12.2.tar.gz # 設置環境變量 ENV PATH=/usr/local/nginx/sbin:$PATH # 設置端口 EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g"]
而後進行構建v5版本:
[root@server myNginx]# docker build -t centos_nginx:v5 .
使用v5版本的鏡像啓動一個容器:
[root@server myNginx]# docker run -d -p 85:80 --name nginx5 centos_nginx:v5 -g "daemon off;" 1f3c1672cce45e6f0f4e8d927de017fbe0a16f30858427d5cb3c2ec7f7b55d98
如今進入到nginx5容器內查看是否存在/data:
[root@server myNginx]# docker exec -it nginx5 /bin/bash [root@1f3c1672cce4 nginx-1.12.2]# ll /data ls: cannot access /data: No such file or directory
如今修改上面的Dockerfile的FROM基於的base image:
[root@server myNginx]# cat Dockerfile # 指定基礎鏡像 FROM centos_nginx:v5 # MAINTAINER MAINTAINER 381347268@qq.com
而後進行構建v6版本:
[root@server myNginx]# docker build -t centos_nginx:v6 . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM centos_nginx:v5 # Executing 1 build trigger ---> Running in a34359a1107d Removing intermediate container a34359a1107d ---> b1e2a7e80907 Step 2/2 : MAINTAINER 381347268@qq.com ---> Running in 901fa3500f51 Removing intermediate container 901fa3500f51 ---> a32bd0c300e2 Successfully built a32bd0c300e2 Successfully tagged centos_nginx:v6
使用v6版本的鏡像啓動一個容器:
[root@server myNginx]# docker run -d -p 86:80 --name nginx6 centos_nginx:v6 -g "daemon off;" f8ccf7ff63f875742ac93b54cc25a629e7df14c8e676e202def1191baf0b3c1f
查看容器狀態,並進入容器驗證/data/目錄:
[root@server myNginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8ccf7ff63f8 centos_nginx:v6 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:86->80/tcp nginx6 [root@server myNginx]# docker exec -it nginx6 /bin/bash [root@f8ccf7ff63f8 nginx-1.12.2]# ll /data total 0
因而可知鏡像v6包含了v5全部的內容,而且增長了ONBUILD的內容