构建包含 nginx_upstream_check_module 的轻量级 Nginx 1.21.5 Docker 镜像

简介: 测试

构建包含 nginx_upstream_check_module 的轻量级 Nginx 1.21.5 Docker 镜像

以下是一个基于 Alpine Linux 的轻量级 Docker 镜像构建方案,包含 Nginx 1.21.5 和第三方模块 nginx_upstream_check_module。

Dockerfile

dockerfile
# 使用 Alpine Linux 作为基础镜像
FROM alpine:3.14
# 安装必要的构建工具和依赖
RUN apk add --no-cache --virtual .build-deps \
    gcc \
    libc-dev \
    make \
    openssl-dev \
    pcre-dev \
    zlib-dev \
    linux-headers \
    curl \
    gnupg \
    wget \
    && apk add --no-cache pcre \
    && rm -rf /var/cache/apk/*
# 下载并解压 Nginx 源码
ENV NGINX_VERSION 1.21.5
RUN wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \
    && tar -xzf nginx-${NGINX_VERSION}.tar.gz \
    && rm nginx-${NGINX_VERSION}.tar.gz
# 下载 nginx_upstream_check_module
RUN git clone https://github.com/yaoweibin/nginx_upstream_check_module.git \
    && cd nginx_upstream_check_module \
    && git checkout v0.3.0  # 兼容 Nginx 1.21.5 的版本
# 配置并编译 Nginx
RUN cd nginx-${NGINX_VERSION} \
    && ./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --modules-path=/usr/lib/nginx/modules \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_slice_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_realip_module \
    --with-stream_ssl_preread_module \
    --with-compat \
    --with-pcre \
    --with-pcre-jit \
    --with-ld-opt="-Wl,-rpath,/usr/lib" \
    --add-module=../nginx_upstream_check_module \
    && make -j$(getconf _NPROCESSORS_ONLN) \
    && make install
# 清理构建依赖
RUN apk del .build-deps \
    && rm -rf /nginx-${NGINX_VERSION} \
    && rm -rf /nginx_upstream_check_module
# 创建 nginx 用户和组
RUN addgroup -S nginx \
    && adduser -S -G nginx nginx
# 创建必要的目录
RUN mkdir -p /var/cache/nginx \
    && chown -R nginx:nginx /var/cache/nginx
# 复制默认配置文件
COPY nginx.conf /etc/nginx/nginx.conf
# 暴露端口
EXPOSE 80 443
# 停止信号
STOPSIGNAL SIGQUIT
# 启动命令
CMD ["nginx", "-g", "daemon off;"]

默认 nginx.conf 配置文件

nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    upstream backend {
        server 127.0.0.1:8080;
        check interval=3000 rise=2 fall=3 timeout=1000 type=http;
        check_http_send "HEAD /health HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
        }
    }
}

构建和使用说明

  1. 将上述内容保存为 Dockerfilenginx.conf 在同一目录下
  2. 构建镜像:
bash
docker build -t nginx-with-check-module .
  1. 运行容器:
bash
docker run -d -p 80:80 -p 443:443 --name my-nginx nginx-with-check-module

注意事项

  1. 这个构建使用了 Alpine Linux 作为基础镜像,保持了轻量级特性。
  2. nginx_upstream_check_module 是一个第三方模块,用于提供上游服务器的健康检查功能。
  3. 默认配置中已经包含了一个简单的健康检查配置示例。
  4. 如果需要更复杂的配置,可以修改 nginx.conf 文件或挂载自定义配置到容器中。
  5. 对于生产环境,建议使用更具体的 Nginx 和模块版本标签,而不是 master/latest。
  6. 如果需要其他 Nginx 模块,可以在 ./configure 命令中添加相应的参数。
目录
相关文章
|
4月前
|
缓存 Shell 网络安全
将应用程序打包成Docker镜像时可能遇到哪些问题?
将应用程序打包成Docker镜像时可能遇到哪些问题?
386 77
|
1月前
|
存储 持续交付 Docker
Docker:轻量级容器技术重塑应用交付
Docker:轻量级容器技术重塑应用交付
|
1月前
|
Kubernetes Cloud Native 持续交付
Docker:轻量级容器化技术解析
Docker:轻量级容器化技术解析
|
1月前
|
运维 测试技术 Docker
Docker:轻量级容器化技术革命
Docker:轻量级容器化技术革命
|
1月前
|
存储 持续交付 Docker
Docker:颠覆传统开发的轻量级容器革命
Docker:颠覆传统开发的轻量级容器革命
|
4月前
|
关系型数据库 MySQL Docker
|
8月前
|
Ubuntu NoSQL Linux
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
453 6
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
|
Shell Linux Docker
docker常用命令大全(基础、镜像、容器、数据卷)
这些命令仅仅是 Docker 命令行工具的冰山一角,但对于日常操作来说已经非常全面。通过熟练地使用这些基础命令,用户可以有效地管理 Docker 的镜像、容器、数据卷和网络。随着用户对 Docker 的深入使用,更高级的命令和选项将会变得必需,但上面列出的命令已经为用户提供了一个坚实的起点。对于初学者来说,理解和掌握这些常用命令是深入学习 Docker 的基础。
756 5
docker常用命令大全(基础、镜像、容器、数据卷)