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

相关推荐
AlfredZhao2 小时前
Docker 查看容器日志:别只记 `docker logs -f`
docker
Crazy________5 小时前
k8s部署若依微服务架构流程,v3.6.6
运维·云原生·容器·kubernetes·状态模式
云飞云共享云桌面5 小时前
广东精密机械设备工厂8-10人共享一台SolidWorks设计服务器
运维·服务器·3d·自动化·汽车·制造
jieyucx5 小时前
【高级利用】条件竞争与逻辑漏洞:与服务器赛跑的艺术
android·运维·服务器·web安全·文件上传
tianyuanwo5 小时前
服务器OS组件研发效能度量可视系统——顶层方案设计与阶段性规划
运维·服务器·度量系统
初级炼丹师(爱说实话版)6 小时前
Ubuntu服务器配置docker
服务器·ubuntu·docker
黄华SJ520it6 小时前
二二复制定点裂变双轨商城系统开发:原理、架构与实战指南
运维·小程序·架构·系统开发
生活爱好者!6 小时前
NAS还能用来开视频会议?docker一键部署MiroTalk SFU
运维·docker·容器
xiaoxiangsiyan7 小时前
GitLab CI/CD 自托管(EE 企业版)+ Kubernetes Runner 集群 + ArgoCD(GitOps 部署)
运维·网络·ci/cd·容器·kubernetes·gitlab·argocd
深爱水瓶 血影S狂风7 小时前
函数调用过程探究
linux·运维·服务器