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

相关推荐
佳児素花痴╮1 小时前
Linux IO模型详解:从阻塞到多路复用
linux·运维·服务器
乐维_lwops2 小时前
网管平台是什么?网管工具如何选?企业网络运维的完整指南
运维·网络·网管平台
manongdashu1882 小时前
应对“SEO需要支付一年服务费”的捆绑消费,静态建站配套终身低成本服务器,全程无需额外运维开销。的方法。
运维·服务器·前端·seo·静态网站
2301_780789662 小时前
WAF与Web应用安全的深度融合:从边界防护到业务风控
运维·服务器·网络·云原生·ddos
二宝哥2 小时前
Rocky Linux 10.2 安装 PostgreSQL 15 完整教程
linux·运维·postgresql
不会就选b2 小时前
Linux之线程池(二)
linux·运维·服务器
ylj_dev2 小时前
从 0 构建 AI Workload Platform(八):受限执行环境与 Kubernetes
docker·容器·kubernetes·分布式系统·ai工作流·容器安全·ai infra
工业HMI实战笔记2 小时前
【拯救HMI】:工业自动化交互革命:HMI 触摸屏如何成为产线智能中枢
运维·自动化·交互
烬羽3 小时前
Dockerfile 是"做奶茶的配方"?用 todos 全栈项目看懂 Docker 构建与发布
nginx·docker·nestjs
mengge.cloud3 小时前
Linux小白入门教程(基础命令篇)
linux·运维·服务器