一、多阶段构建nginx镜像-编译格式

1、镜像文件说明

“skillfir/alpine:gcc”:基于alpine3.14制作成的gcc镜像,apk源调整为中科大,同时WORKDIR=/usr/src;

“skillfir/alpine:glibc”:基于alpine3.14制作而成,apk源调整为中科大,同时解决了中文乱码及时区问题。

2、Dockerfile示例

2.1 项目目录结构

.
├── default.conf
├── Dockerfile
└── nginx.conf

2.2 Dockerfile内容

#第1阶段
FROM skillfir/alpine:gcc AS builder01
LABEL AUTHER="WEIPENG <weipeng@facefinding.cn>"

# 设置变量	
ENV NGINX_VERSION 1.21.1
ARG CONFIG="\
		--prefix=/app/nginx \
		--conf-path=/app/nginx/nginx.conf \
		--sbin-path=/app/nginx/sbin/nginx \
		--error-log-path=/app/nginx/logs/error.log \
		--http-log-path=/app/nginx/logs/access.log \
		--user=nginx \
		--group=nginx \
		--with-http_ssl_module \
		--with-http_realip_module \
		--with-http_addition_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_random_index_module \
		--with-http_secure_link_module \
		--with-http_stub_status_module \
		--with-http_auth_request_module \
		--with-http_xslt_module=dynamic \
		--with-http_image_filter_module=dynamic \
		--with-http_geoip_module=dynamic \
		--with-threads \
		--with-stream \
		--with-stream_ssl_module \
		--with-stream_ssl_preread_module \
		--with-stream_realip_module \
		--with-stream_geoip_module=dynamic \
		--with-http_slice_module \
		--with-mail \
		--with-mail_ssl_module \
		--with-compat \
		--with-file-aio \
		--with-http_v2_module \
	" 
# 编译安装
RUN apk update && apk upgrade &&\
wget https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -O nginx.tar.gz &&\
tar -zxf nginx.tar.gz &&\
rm -f nginx.tar.gz &&\
cd /usr/src/nginx-$NGINX_VERSION &&\
./configure $CONFIG --with-debug &&\
make -j$(getconf _NPROCESSORS_ONLN) &&\
mv objs/nginx objs/nginx-debug && \
mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so  &&\
mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so  && \
mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so &&\
mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so &&\
make install

#第2阶段
FROM skillfir/alpine:glibc
LABEL AUTHOR="weipeng hw_weipeng@163.com" description="/app/nginx"
RUN apk update && apk upgrade &&\
    apk add --no-cache \
		openssl-dev \
		pcre-dev \
		zlib-dev \
		gd-dev &&\
	addgroup -S nginx &&\
	adduser -s /sbin/nologin -H -D -G nginx nginx
COPY --from=builder01 /app/nginx /app/nginx
COPY nginx.conf /app/nginx/nginx.conf
COPY default.conf /app/nginx/conf.d/default.conf
WORKDIR /app/nginx
EXPOSE 80
CMD ["./sbin/nginx","-g","daemon off;"]

2.3 nginx.conf配置文件

user nginx;
worker_processes auto;
pcre_jit on;
include /app/nginx/modules/*.conf;
events {
	worker_connections 1024;
}
http {
	include /app/nginx/mime.types;
	default_type application/octet-stream;
	server_tokens off;
	client_max_body_size 30m;
	sendfile on;
	tcp_nopush on;
	ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
	ssl_prefer_server_ciphers on;
	ssl_session_cache shared:SSL:2m;
	ssl_session_timeout 1h;
	ssl_session_tickets off;
	gzip_vary on;
	map $http_upgrade $connection_upgrade {
		default upgrade;
		'' close;
	}
	log_format main '$remote_addr - $remote_user [$time_local] "$request" '
			'$status $body_bytes_sent "$http_referer" '
			'"$http_user_agent" "$http_x_forwarded_for"'
			'"$request_time" "$upstream_response_time"';
	access_log /app/nginx/logs/access.log main;
	include /app/nginx/conf.d/*.conf;
}

2.4 default.conf配置文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

}

2.5 制作镜像

#制作镜像
docker built -t alpine:nginx .
#查看镜像
docker images 
	alpine           nginx     d5bb514e4046   2 hoursago     76.1MB
	<none>          <none>     7ae8802124c7   9 seconds ago    272MB   //第一 阶段产生的临时镜像

2.6 镜像测试

docker run -d --rm --name nginx -v /data/nginx/html:/usr/share/html:ro -p 80:80 alpine:nginx
echo "welcome to my nginx-test" >/data/nginx/html/index.html

image-20210809150804928

二、多阶段构建nginx镜像-在线安装

FROM skillfir/alpine:glibc
LABEL AUTHER="WEIPENG <weipeng@facefinding.cn>"
RUN apk update &&\
    apk add --no-cache nginx 
ADD nginx.conf /etc/nginx/
ADD default.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
posted on 2021-08-09 15:11  带米的笨老头  阅读(700)  评论(2)    收藏  举报