Docker鏡像-基於DockerFile制作編譯版nginx鏡像
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.下載nginx的安裝包
1>.打開nginx官網,找到最新穩定版下載即可(http://nginx.org/en/download.html)
2>.下載nginx安裝包
[root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cd /yinzhengjie/softwares/dockerfile/web/nginx/ [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ll total 12 -rwxr-xr-x 1 root root 461 Jan 21 16:37 docker-build.sh -rw-r--r-- 1 root root 1833 Jan 21 16:44 Dockerfile -rw-r--r-- 1 root root 1979 Jan 21 16:31 nginx.conf [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# wget http://nginx.org/download/nginx-1.14.2.tar.gz --2020-01-21 16:53:09-- http://nginx.org/download/nginx-1.14.2.tar.gz Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3 Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1015384 (992K) [application/octet-stream] Saving to: ‘nginx-1.14.2.tar.gz’ 100%[==================================================================================================================================>] 1,015,384 355KB/s in 2.8s 2020-01-21 16:53:13 (355 KB/s) - ‘nginx-1.14.2.tar.gz’ saved [1015384/1015384] [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ll total 1004 -rwxr-xr-x 1 root root 461 Jan 21 16:37 docker-build.sh -rw-r--r-- 1 root root 1833 Jan 21 16:44 Dockerfile -rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz -rw-r--r-- 1 root root 1979 Jan 21 16:31 nginx.conf [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#
3>.屬性編譯安裝nginx的步驟(如果你不清楚nginx的編譯步驟請估計寫不出來Dockerfile的,因此你得了解nginx的編譯安裝步驟,不熟悉的沒關系,看一下我之前的筆記即可,熟悉的同學跳過該步驟)
博主推薦閱讀: https://www.cnblogs.com/yinzhengjie/p/12031651.html
二.准備測試數據
1>.模擬打包生產環境代碼
[root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll total 0 [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# echo "<h1>YinZhengjie's Nginx Web Server</h1>" > index.html [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# echo "<h1>YinZhengjie's Nginx Web Server 2020</h1>" > index2020.html [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll total 8 -rw-r--r-- 1 root root 45 Jan 21 17:38 index2020.html -rw-r--r-- 1 root root 40 Jan 21 17:38 index.html [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# tar zcvf code.tar.gz index* index2020.html index.html [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll total 12 -rw-r--r-- 1 root root 197 Jan 21 17:38 code.tar.gz -rw-r--r-- 1 root root 45 Jan 21 17:38 index2020.html -rw-r--r-- 1 root root 40 Jan 21 17:38 index.html [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/nginx/ total 1004 -rwxr-xr-x 1 root root 461 Jan 21 16:37 docker-build.sh -rw-r--r-- 1 root root 2603 Jan 21 17:34 Dockerfile -rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz -rw-r--r-- 1 root root 1979 Jan 21 16:31 nginx.conf [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cp code.tar.gz /yinzhengjie/softwares/dockerfile/web/nginx/ [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cd /yinzhengjie/softwares/dockerfile/web/nginx/ [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ll total 1008 -rw-r--r-- 1 root root 197 Jan 21 17:39 code.tar.gz -rwxr-xr-x 1 root root 461 Jan 21 16:37 docker-build.sh -rw-r--r-- 1 root root 2603 Jan 21 17:34 Dockerfile -rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz -rw-r--r-- 1 root root 1979 Jan 21 16:31 nginx.conf [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#
2>.編寫nginx的配置文件
[root@docker101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/dockerfile/web/nginx/nginx.conf user nginx; worker_processes auto; #錯誤日志也應該指定到咱們編譯安裝的nginx目錄中 error_log /yinzhengjie/softwares/nginx/logs/error.log; pid /run/nginx.pid; #Docker最終運行Nginx建議大家把后台進程關閉,默認是"on". daemon off; events { worker_connections 1024; } http { #自定義Nginx的日志格式 log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; #同理,我們將訪問日志也放到咱們編譯安裝nginx的目錄中 access_log /yinzhengjie/softwares/nginx/logs/access_json.log my_access_json; sendfile on; keepalive_timeout 65; include mime.types; default_type text/html; charset utf-8; server { listen 80 default_server; listen [::]:80 default_server; server_name _; #別忘記修改站點代碼的根目錄也要指定到咱們編譯安裝nginx的目錄中喲~ root /yinzhengjie/softwares/nginx/html; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } } [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]#
3>.編寫快速編譯docker的腳本(我這里就是拋磚引玉了,你可以自行優化)
[root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# cat docker-build.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2020-01-18 #FileName: docker-build.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright (C): 2020 All rights reserved #******************************************************************** TAG=$1 docker image build -t nginx:${TAG} ./ [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#
4>.編寫Dockerfile
[root@docker101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/dockerfile/web/nginx/Dockerfile #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #Blog: http://www.cnblogs.com/yinzhengjie #Description: YinZhengjie's Nginx Dockerfile #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** #第一行先定義基礎鏡像,表示當前鏡像文件是基於哪個進行編輯的. FROM centos:centos7.6.1810 #指定鏡像維護者的信息. MAINTAINER Jason.Yin y1053419035@qq.com #除了安裝編譯nginx的依賴的安裝包外,還可以將一些常用的命令工具也安裝上 #類似於這樣的安裝命令(或者經常改動相對較小的命令)應該盡量往前寫,這樣在多次編譯時就不會重復執行了(因為默認會使用緩存),從而提升編譯效率. RUN yum -y install epel-release && yum -y install vim net-tools bridge-utils firewalld bc iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel zip unzip zlib-devel lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel systemd-devel bash-completion traceroute psmisc
#將"nginx-1.14.2.tar.gz"安裝包文件解壓到"/usr/local/src"目錄中 #相比COPY指令,ADD指令可以解壓"*.tar.gz"的文件,但如果你的安裝包是"*.zip"文件的話,ADD指令也不好使,得咱們自己使用unzip相關命令自行解壓,索性我上面已經安裝了unzip相關的軟件包
ADD nginx-1.14.2.tar.gz /usr/local/src #編譯安裝nginx RUN cd /usr/local/src/nginx-1.14.2 && ./configure --prefix=/yinzhengjie/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install && ln -sv /yinzhengjie/softwares/nginx/sbin/nginx /usr/sbin/nginx
#將nginx的配置文件放在鏡像的指定編譯安裝的目錄"/yinzhengjie/softwares/nginx/conf" COPY nginx.conf /yinzhengjie/softwares/nginx/conf #將測試代碼解壓到咱們編譯安裝的nginx網站默認根目錄中 ADD code.tar.gz /yinzhengjie/softwares/nginx/html #創建nginx用戶,yum方式安裝無需做此步驟,因為默認yum安裝會自動創建nginx用戶,咱們上面指令了以nginx用戶運行,因此我們需要在鏡像中創建"nginx用戶" RUN useradd nginx -s /sbin/nologin -u 2000 #定義向外暴露的端口號,多個端口用空格做間隔,啟動容器的時候"-p"需要使用此端向外端映射. EXPOSE 80/tcp 443/tcp #定義前台運行的命令,每個Docker只能有一條,如果定義了多條"CMD"指令那么最后一條CMD指令會覆蓋之前的(即只有最后一條CMD被執行). CMD ["nginx"] [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]#
5>.編譯生成nginx鏡像
[root@docker101.yinzhengjie.org.cn ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx make-v0.1-20200121-0846 5d81119e1e53 9 minutes ago 590MB <none> <none> fd9dcda01aa8 12 minutes ago 590MB <none> <none> 896b954fd6d4 14 minutes ago 590MB <none> <none> 88d854efd276 18 minutes ago 589MB <none> <none> 252190d9a459 53 minutes ago 589MB centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cd /yinzhengjie/softwares/dockerfile/web/nginx/ [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ll total 1008 -rw-r--r-- 1 root root 197 Jan 21 17:39 code.tar.gz -rwxr-xr-x 1 root root 461 Jan 21 16:37 docker-build.sh -rw-r--r-- 1 root root 2867 Jan 21 18:02 Dockerfile -rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz -rw-r--r-- 1 root root 2198 Jan 21 18:12 nginx.conf [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ./docker-build.sh make-v0.1-20200121-0846 Sending build context to Docker daemon 1.026MB Step 1/10 : FROM centos:centos7.6.1810 ---> f1cb7c7d58b7 Step 2/10 : MAINTAINER Jason.Yin y1053419035@qq.com ---> Using cache ---> 44bbf438782f Step 3/10 : RUN yum -y install epel-release && yum -y install vim net-tools bridge-utils firewalld bc iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre- devel openssl openssl-devel zip unzip zlib-devel lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel systemd-devel bash-completion traceroute psmisc ---> Using cache ---> c8282c8fcd0c Step 4/10 : ADD nginx-1.14.2.tar.gz /usr/local/src ---> Using cache ---> 117d35e96dc3 Step 5/10 : RUN cd /usr/local/src/nginx-1.14.2 && ./configure --prefix=/yinzhengjie/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module - -with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install && ln -sv /yinzhengjie/softwares/nginx/sbin/nginx /usr/sbin/nginx ---> Using cache ---> 5b687e6a0dad Step 6/10 : COPY nginx.conf /yinzhengjie/softwares/nginx/conf ---> 2891153689a3 Step 7/10 : ADD code.tar.gz /yinzhengjie/softwares/nginx/html ---> a4847d0e3b19 Step 8/10 : RUN useradd nginx -s /sbin/nologin -u 2000 ---> Running in 7562ef0c366a Removing intermediate container 7562ef0c366a ---> e700a97c15de Step 9/10 : EXPOSE 80/tcp 443/tcp ---> Running in cdc6364a25e7 Removing intermediate container cdc6364a25e7 ---> fdc19386ef1f Step 10/10 : CMD ["nginx"] ---> Running in acc980fae2ad Removing intermediate container acc980fae2ad ---> e8520d2c15ae Successfully built e8520d2c15ae Successfully tagged nginx:make-v0.1-20200121-0846 [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx make-v0.1-20200121-0846 e8520d2c15ae 3 seconds ago 590MB <none> <none> 5d81119e1e53 9 minutes ago 590MB <none> <none> fd9dcda01aa8 13 minutes ago 590MB <none> <none> 896b954fd6d4 14 minutes ago 590MB <none> <none> 88d854efd276 18 minutes ago 589MB <none> <none> 252190d9a459 54 minutes ago 589MB centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#
三.驗證編譯版本的nginx鏡像
1>.運行容器
[root@docker101.yinzhengjie.org.cn ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx make-v0.1-20200121-0846 e8520d2c15ae About an hour ago 590MB <none> <none> 5d81119e1e53 About an hour ago 590MB <none> <none> fd9dcda01aa8 About an hour ago 590MB <none> <none> 896b954fd6d4 About an hour ago 590MB <none> <none> 88d854efd276 About an hour ago 589MB <none> <none> 252190d9a459 2 hours ago 589MB centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# docker container run -it --rm -p 172.200.3.101:8080:80 --name myNginx --hostname yinzhengjie nginx:make-v0.1-20200121-0846 #運行容器后會阻塞在當前終端,需要再開啟一個終端連接該容器,如下圖所示。
2>.客戶端訪問時最好先連接容器查看日志記錄
[root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 86cbd7aa982c nginx:make-v0.1-20200121-0846 "nginx" 5 minutes ago Up 5 minutes 443/tcp, 172.200.3.101:8080->80/tcp myNginx [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# docker container inspect -f "{{.NetworkSettings.IPAddress}}" myNginx 172.17.0.2 [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 20480 172.200.3.101:8080 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash [root@yinzhengjie /]# [root@yinzhengjie /]# tail -10f /yinzhengjie/softwares/nginx/logs/access_json.log {"@timestamp":"2020-01-21T11:29:57+00:00","host":"172.17.0.2", '"clientip":"172.200.0.1",' "size":0, '"responsetime":0.000,' '"upstreamtim e":"-",' '"upstreamhost":"-",' '"http_host":"docker101.yinzhengjie.org.cn",' '"uri":"/index.html",' '"domain":"docker101.yinzhengjie.org.cn",' '"xff":"-",' '"referer":"-",' '"tcp_xff":"",' '"http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",' '"status":"304"}'{"@timestamp":"2020-01-21T11:30:31+00:00","host":"172.17.0.2", '"clientip":"172.200.0.1",' "size":45, '"responsetime":0.000,' '"upstreamti me":"-",' '"upstreamhost":"-",' '"http_host":"docker101.yinzhengjie.org.cn",' '"uri":"/index2020.html",' '"domain":"docker101.yinzhengjie.org.cn",' '"xff":"-",' '"referer":"-",' '"tcp_xff":"",' '"http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",' '"status":"200"}'