基于 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"

相关推荐
AOwhisky5 小时前
Linux(CentOS)系统管理入门笔记(第四期)——文件系统(下篇):文件与目录操作实战——cpmvmkdirrmln
linux·运维·笔记·centos·云计算·文件系统
XTIOT6666 小时前
CRPT 诚实标识采集落地技术实践:分工况硬件选型与合规数据标准化解决方案
大数据·运维·人工智能·嵌入式硬件·物联网
想你依然心痛7 小时前
嵌入式容器:Docker与BalenaOS在边缘设备上的实践——容器运行时与OTA
运维·docker·容器
竣达技术7 小时前
NAS / 服务器断电数据丢失?UPS 配套 USB关机保护板,市电断电池低自动提示服务器/电脑关机
运维·服务器·电脑
Lottie20267 小时前
京东电商进阶运营:精准定价+安全铺货+竞品监控+利润管控全自动化方案
运维·安全·自动化
章老师说8 小时前
NGINX官方谈Lua风险:这其实是两条网关技术路线之争
运维·nginx·负载均衡·lua·openresty
不瘦80斤不改名8 小时前
HalfCart:基于LBS的1\.5km本地拼单系统全栈实践与踩坑复盘
python·docker·typescript·pycharm
wbs_scy8 小时前
仿 muduo 高并发服务器项目:实现 Any 通用容器并理解 std::any
运维·服务器
做个文艺程序员8 小时前
Linux第11篇:存储管理——磁盘分区、LVM 与文件系统
linux·运维·服务器
懂AI的老郑8 小时前
工业检测场景下智能体的应用:从自动化到智能化
运维·自动化