基于 Alpine 构建轻量 Nginx 错误页面 Docker 镜像

1、创建目录

root@localhost \~# mkdir demo4

root@localhost \~# cd demo4

2、编写Dockerfile

root@localhost demo4# vim .dockerignore

root@localhost demo4# cat .dockerignore

Dockerfile

root@localhost demo4# vim Dockerfile

root@localhost demo4# cat Dockerfile

FROM alpine:3.23 AS builder

RUN apk add --no-cache \

build-base \

pcre-dev \

zlib-dev \

openssl-dev \

linux-headers \

wget \

tar

ENV NGINX_VERSION=1.28.1

RUN wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \

tar -xzf nginx-${NGINX_VERSION}.tar.gz && \

rm nginx-${NGINX_VERSION}.tar.gz

WORKDIR /nginx-${NGINX_VERSION}

RUN ./configure \

--prefix=/usr/local/nginx \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_stub_status_module \

--with-threads \

--with-file-aio && \

make && \

make install

FROM alpine:3.23

COPY --from=builder /usr/local/nginx /usr/local/nginx

COPY error.html /usr/local/nginx/html/error.html

RUN apk add --no-cache pcre zlib openssl && \

ln -sf /dev/stdout /usr/local/nginx/logs/access.log && \

ln -sf /dev/stderr /usr/local/nginx/logs/error.log

RUN echo "index.html" > /usr/local/nginx/html/index.html

RUN echo 'server { error_page 404 /error.html; location = /error.html {root /usr/local/nginx/html; } }' > /usr/local/nginx/conf/default.conf

EXPOSE 80 443

ENTRYPOINT "/usr/local/nginx/sbin/nginx","-g","daemon off;"

3、编写error.html

root@localhost demo4# vim error.html

root@localhost demo4# cat error.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0">

<title>自定义错误页面</title>

<style>

body {

font-family: Arial,sans-serif;

text-align: center;

padding:50px;

}

h1{

color:#333;

}

p{

color:#666;

}

</style>

</head>

<body>

<h1>哎呀!页面未找到</h1>

<p>你所请求的页面可能已经被移除或者地址错误。</p>

</body>

</html>

4、构建镜像

root@localhost demo4# docker build -t error:1.0 .

+ Building 4.9s (15/15) FINISHED

=> internal load build definition from Dockerfile

=> => transferring dockerfile: 1.25kB

=> internal load metadata for docker.io/library/alpine

=> internal load .dockerignore

=> => transferring context: 111B

=> internal load build context

=> => transferring context: 92B

=> builder 1/5 FROM docker.io/library/alpine:3.23@sha2

=> => resolve docker.io/library/alpine:3.23@sha256:25109

=> CACHED builder 2/5 RUN apk add --no-cache build

=> CACHED builder 3/5 RUN wget http://nginx.org/downlo

=> CACHED builder 4/5 WORKDIR /nginx-1.28.1

=> CACHED builder 5/5 RUN ./configure --prefix=/us

=> CACHED stage-1 2/6 COPY --from=builder /usr/local/n

=> CACHED stage-1 3/6 COPY error.html /usr/local/nginx

=> stage-1 4/6 RUN apk add --no-cache pcre zlib openss

=> stage-1 5/6 RUN echo "index.html" > /usr/local/ngin

=> stage-1 6/6 RUN echo 'server { error_page 404 /erro

=> exporting to image

=> => exporting layers

=> => exporting manifest sha256:20f960700b575ca07d0e9828

=> => exporting config sha256:97012d6ba632c115ba828b4ff3

=> => exporting attestation manifest sha256:df85dc33aa21

=> => exporting manifest list sha256:b915f674184d47de561

=> => naming to docker.io/library/error:1.0

=> => unpacking to docker.io/library/error:1.0

5、查看镜像

root@localhost demo4# docker images

i Info → U In Use

IMAGE ID DISK USAGE CONTENT SIZE EXTRA

busybox:latest b3255e7dfbcd 6.7MB 2.22MB

elasticsearch:8.19.5 51945475f55d 2.1GB 710MB

error:1.0 b915f674184d 21.5MB 6.59MB

6、创建容器

root@localhost demo4# docker run --name error --rm -d -p 8080:80 error:1.0

a6c0fee7f5b64c3e20a8989597923034004db9147c05932b004d7c2729150f90

7、访问测试

root@localhost demo4# curl localhost:8080/error.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0">

<title>自定义错误页面</title>

<style>

body {

font-family: Arial,sans-serif;

text-align: center;

padding:50px;

}

h1{

color:#333;

}

p{

color:#666;

}

</style>

</head>

<body>

<h1>哎呀!页面未找到</h1>

<p>你所请求的页面可能已经被移除或者地址错误。</p>

</body>

</html>

8、访问日志

root@localhost demo4# docker logs error

172.17.0.1 - - 20/Feb/2026:08:15:34 +0000 "GET /error.html HTTP/1.1" 200 437 "-" "curl/7.76.1"

相关推荐
vsropy1 小时前
Ubuntu网络图标消失问题/有网络问号
linux·运维·ubuntu
fofantasy1 小时前
NSK LH12AN 微型导轨技术手册
运维·网络·数据库·经验分享·规格说明书
coderwu1 小时前
Ubuntu 24.04 终端输入 openclaw config 提示未找到命令解决办法
linux·运维·ubuntu
dxxt_yy2 小时前
千兆光/电口+OTDR一体——成都鼎讯 SZT-1000A 千兆以太网测试仪助力风电能源光缆管理
运维·服务器·能源
云原生指北2 小时前
Apple Container Machine:把 Linux 搬进 Mac
macos·docker
AI帮小忙3 小时前
Debian系linux操作系统里安装OpenClaw
linux·运维·debian
极创信息3 小时前
Linux挖矿病毒深度清理实战教程,从进程隐藏、Rootkit驻留到彻底根除
java·大数据·linux·运维·安全·tomcat·健康医疗
志栋智能5 小时前
超自动化巡检剧本(Playbook):运维经验的数字化封装
运维·自动化
ElevenS_it1885 小时前
Nginx日志监控告警实战:access_log解析+5xx突增+慢请求+异常IP自动告警完整方案(Filebeat+Zabbix)
运维·网络·tcp/ip·nginx·zabbix
liulilittle5 小时前
Linux Swap 文件配置与持久化(虚拟内存)
linux·运维·服务器