Skip to main content

alpine最小化nginx镜像

tip

注意 当前文件下自备nginx.conf和entrypoint.sh,另外再随便弄个index.html用作测试


Dockerfile

FROM alpine:3.17 as relay_nginx                                                                                                                  

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories && apk update && \
apk add --no-cache ca-certificates && \
apk add --no-cache curl bash tzdata && \
apk add --no-cache \
gcc \
libc-dev \
make \
openssl-dev \
pcre-dev \
zlib-dev \
linux-headers \
curl \
gnupg \
libxslt-dev \
gd-dev \
geoip-dev \
wget && \
cd /tmp/ && \
wget -c http://nginx.org/download/nginx-1.22.1.tar.gz && \
tar xf nginx-1.22.1.tar.gz && \
cd nginx-1.22.1 && ./configure --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module
--with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-ipv6 && \
make && make install && \
rm -rf nginx-1.22.0* /var/cache/apk/*

FROM alpine:3.17 as alpine_nginx

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories && \
apk add zlib-dev pcre-dev bash curl && apk add --no-cache ca-certificates && \
apk add --no-cache tzdata && \
cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
rm -rf /var/cache/apk/*
COPY --from=relay_nginx /usr/local/nginx /usr/local/nginx
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY index.html /data/nginx/html/index.html
COPY entrypoint.sh /usr/bin/entrypoint.sh
RUN addgroup -g 2023 -S nginx && adduser -s /sbin/nologin -S -D -u 2023 -G nginx nginx && \
chown -R nginx.nginx /data/nginx/ /usr/local/nginx/ && \
ln -sf /dev/stdout /usr/local/nginx/logs/access.log && \
ln -sf /dev/stderr /usr/local/nginx/logs/error.log
EXPOSE 80 443
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
CMD ["/usr/local/nginx/sbin/nginx"]


entrypoint.sh

#!/bin/sh                                                                                                                                        
#
#*********************************************************
#Author: Ez4cyka
#QQ: NULL
#Date: 2023-01-29
#FileName: entrypoint.sh
#URL: https://ez4cyka.com
#Description: TODO
#Copyright: 2023 All rights reserved
#********************************************************

exec "$@"

nginx.conf

#user  nobody;
worker_processes 1;
user nginx;
daemon off;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /data/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}