1. 程式人生 > 其它 >Dockerfile製作nginx基礎映象

Dockerfile製作nginx基礎映象

一 工作目錄

root@k8s-harbor-01:/data/dockerfile/web/nginx# pwd
/data/dockerfile/web/nginx

二 準備nginx安裝包

root@k8s-harbor-01:/data/dockerfile/web/nginx# wget http://nginx.org/download/nginx-1.20.2.tar.gz

三 編寫Dockerfile

點選檢視程式碼
root@k8s-harbor-01:/data/dockerfile/web/nginx# cat Dockerfile 
FROM 192.168.174.120/baseimages/alpine-base:3.15

ENV NGINX_VERSION 1.20.2
ENV NJS_VERSION 0.7.0
ENV PKG_RELEASE 1

ADD nginx-1.20.2.tar.gz /tmp
COPY apk /tmp/apk
RUN set -x
&& addgroup -g 101 -S nginx
&& adduser -S -D -H -u 101 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx
&& apk add --no-cache --virtual .build-deps gcc libc-dev make openssl-dev pcre-dev zlib-dev linux-headers libxslt-dev gd-dev geoip-dev perl-dev libedit-dev bash alpine-sdk findutils openssl
&& nginxPackages="
nginx-module-xslt=${NGINX_VERSION}-r${PKG_RELEASE}
nginx-module-geoip=${NGINX_VERSION}-r${PKG_RELEASE}
nginx-module-image-filter=${NGINX_VERSION}-r${PKG_RELEASE}
nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-r${PKG_RELEASE}
"
&& KEY_SHA512="e7fa8303923d9b95db37a77ad46c68fd4755ff935d0a534d26eba83de193c76166c68bfe7f65471bf8881004ef4aa6df3e34689c305662750c0172fca5d8552a *stdin"
&& wget -O /tmp/nginx_signing.rsa.pub

https://nginx.org/keys/nginx_signing.rsa.pub
&& if [ "$(openssl rsa -pubin -in /tmp/nginx_signing.rsa.pub -text -noout | openssl sha512 -r)" = "$KEY_SHA512" ]; then
echo "key verification succeeded!";
mv /tmp/nginx_signing.rsa.pub /etc/apk/keys/;
else
echo "key verification failed!";
exit 1;
fi
&& apk add -X "
https://nginx.org/packages/alpine/v$
(egrep -o '[1]+.[0-9]+' /etc/alpine-release)/main" --no-cache $nginxPackages
&& apk add --no-cache tzdata curl ca-certificates
&& cd /tmp/nginx-1.20.2
&& ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-poll_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_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_slice_module --with-http_stub_status_module --with-stream --with-stream_ssl_module --with-cc-opt=-Wno-error --with-http_xslt_module --with-pcre --with-ld-opt= --user=nginx --group=nginx --with-threads --with-file-aio --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock
&& make
&& make install
&& ln -sf /dev/stdout /usr/local/nginx/logs/access.log
&& ln -sf /dev/stderr /usr/local/nginx/logs/error.log
&& apk del .build-deps
&& rm -rf /tmp/nginx-1.20.2
&& if [ -n "/etc/apk/keys/nginx_signing.rsa.pub" ]; then rm -f /etc/apk/keys/nginx_signing.rsa.pub; fi
&& ln -svf /usr/local/nginx/sbin/nginx /usr/sbin/nginx

STOPSIGNAL SIGQUIT

四 編寫構建指令碼

root@k8s-harbor-01:/data/dockerfile/web/nginx# cat build-command.sh 
#!/bin/bash

docker build -t 192.168.174.120/baseimages/nginx:1.20.2 .

五 構建映象

root@k8s-harbor-01:/data/dockerfile/web/nginx# ./build-command.sh 

Successfully built 9e15403c2475
Successfully tagged 192.168.174.120/baseimages/nginx:1.20.2

六 檢視映象

root@k8s-harbor-01:/data/dockerfile/web/nginx# docker images
REPOSITORY                                             TAG              IMAGE ID       CREATED          SIZE
192.168.174.120/baseimages/nginx                       1.20.2           9e15403c2475   7 minutes ago    42.3MB

七 驗證映象

7.1 啟動映象

root@k8s-harbor-01:/data/dockerfile/web/nginx# docker run -it --rm  -p 8001:80 192.168.174.120/baseimages/nginx:1.20.2 sh
/ # nginx
/ # echo "/usr/local/nginx/html" > /usr/local/nginx/html/index.html 

7.2 驗證nginx web介面

root@k8s-harbor-01:/data/dockerfile/web/nginx# curl 192.168.174.120:8001
/usr/local/nginx/html

7.3 驗證nginx日誌資訊

root@k8s-harbor-01:/data/dockerfile/web/nginx# docker run -it --rm  -p 8001:80 192.168.174.120/baseimages/nginx:1.20.2 sh
/ # nginx
/ # echo "/usr/local/nginx/html" > /usr/local/nginx/html/index.html 
/ # 192.168.174.120 - - [06/Dec/2021:18:54:43 +0800] "GET / HTTP/1.1" 200 22 "-" "curl/7.68.0"

八 上傳映象到harbor

root@k8s-harbor-01:/data/dockerfile/web/nginx# docker push 192.168.174.120/baseimages/nginx:1.20.2

  1. 0-9 ↩︎