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

相关推荐
考虑考虑2 分钟前
docker compose V2版本新属性
运维·后端·自动化运维
小小的木头人2 小时前
Ubuntu Samba修改端口绕过445封禁挂载
运维·ubuntu
March.s2 小时前
Docker 容器技术完全指南:从生态到架构的深度解析
docker
wdfk_prog3 小时前
ROS教程:01 搭建 ROS1 Noetic Docker 开发环境
运维·缓存·docker·容器·ros
SEO_juper3 小时前
Java 并发编程实战:从线程基础到高并发架构
运维·人工智能·爬虫·chatgpt·seo
szephyr4 小时前
Docker + Compose 实战:把本地项目一键搬到云服务器,附完整配置
运维·docker·容器·部署·云服务器
wdfk_prog4 小时前
ros教程02:创建 catkin Workspace、Package 与第一个 ROS1 C++ Node
运维·缓存·docker·容器·ros
亚川楼宇自控系统数据中心厂家5 小时前
高校 / 医院 / 政务云数据中心 IBMS 系统怎么落地?
运维
陈陈CHENCHEN5 小时前
【Linux】服务器根目录磁盘扩容操作记录
linux·运维·服务器
见闻小天地7 小时前
从晶片工艺到可靠性验证:晶威特TF-3215型RTC晶振全面解析
运维·业界资讯