使用dockerfile打包ng镜像演示

使用 Dockerfile 打包 Nginx 配置与静态页面为镜像


💬 问题

bing.html 路径 /data/nginx/static/bing.html google.html 路径 /data/nginx/static/google.html nginx子配置文件路径 /etc/nginx/conf/conf.d/backend_servers.conf

请使用dockerfile 将上面的三个配置文件打包进nginxnginx:1.30的镜像中,镜像名为nginx:1.30_sun_lb


🤖 回答

根据您的需求,我将创建一个完整的Dockerfile来打包Nginx镜像,包含所有配置文件和静态页面。

📁 项目目录结构

首先创建以下目录结构:

arduino 复制代码
.
├── Dockerfile
├── conf.d/
│   └── weighted_loadbalance.conf
├── backend_conf.d/
│   └── backend_servers.conf
└── static/
    ├── bing.html
    └── google.html

📄 配置文件内容

1. 主负载均衡配置 conf.d/weighted_loadbalance.conf

nginx 复制代码
# 定义上游服务器组
upstream search_backend {
    # 权重设置为 2:1,2/3流量到 bing,1/3流量到 google
    server 127.0.0.1:8081 weight=2;  # 对应 bing.html
    server 127.0.0.1:8082 weight=1;  # 对应 google.html
    
    # 启用keepalive提升性能
    keepalive 32;
}

server {
    listen 80;
    server_name localhost;

    # 所有请求都转发到 upstream 组
    location / {
        proxy_pass http://search_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;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 启用HTTP/1.1 keepalive
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}

2. 后端服务配置 backend_conf.d/backend_servers.conf

nginx 复制代码
# 模拟 bing 后端服务 - 监听 8081 端口
server {
    listen 8081;
    server_name localhost;
    
    location / {
        try_files /bing.html =404;
    }
    
    root /data/nginx/static;
}

# 模拟 google 后端服务 - 监听 8082 端口
server {
    listen 8082;
    server_name localhost;
    
    location / {
        try_files /google.html =404;
    }
    
    root /data/nginx/static;
}

3. HTML页面文件

static/bing.html

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Bing</title>
    <style>
        body { 
            display: flex; 
            justify-content: center; 
            align-items: center; 
            height: 100vh; 
            margin: 0; 
            font-family: Arial, sans-serif;
            background: #f0f0f0;
        }
        h1 { 
            font-size: 80px; 
            color: #0067B8;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <h1>this is bing</h1>
</body>
</html>

static/google.html

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Google</title>
    <style>
        body { 
            display: flex; 
            justify-content: center; 
            align-items: center; 
            height: 100vh; 
            margin: 0; 
            font-family: Arial, sans-serif;
            background: #f0f0f0;
        }
        h1 { 
            font-size: 80px; 
            color: #4285F4;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <h1>this is google</h1>
</body>
</html>

🐳 Dockerfile

dockerfile 复制代码
# 使用官方 Nginx 1.30 镜像作为基础
FROM nginx:1.30-alpine

# 设置维护者信息
LABEL maintainer="your-email@example.com"
LABEL description="Nginx with weighted load balancing for bing and google"

# 创建静态文件目录
RUN mkdir -p /data/nginx/static

# 复制配置文件
# 主负载均衡配置
COPY conf.d/weighted_loadbalance.conf /etc/nginx/conf.d/weighted_loadbalance.conf

# 后端服务配置(监听8081和8082端口)
COPY backend_conf.d/backend_servers.conf /etc/nginx/conf.d/backend_servers.conf

# 复制静态HTML文件到指定路径
COPY static/bing.html /data/nginx/static/bing.html
COPY static/google.html /data/nginx/static/google.html

# 删除默认的nginx配置文件(可选)
RUN rm -f /etc/nginx/conf.d/default.conf

# 暴露端口
# 80: 负载均衡器入口
# 8081: bing后端服务
# 8082: google后端服务
EXPOSE 80 8081 8082

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1

# 启动Nginx(前台运行)
CMD ["nginx", "-g", "daemon off;"]

🏗️ 构建和运行

1. 构建镜像

在项目根目录执行:

bash 复制代码
docker build -t nginx:1.30_sun_lb .

2. 运行容器

bash 复制代码
# 方式1: 直接运行(推荐)
docker run -d \
  --name nginx-lb \
  -p 80:80 \
  -p 8081:8081 \
  -p 8082:8082 \
  nginx:1.30_sun_lb

# 方式2: 如需挂载外部配置文件(方便调试)
docker run -d \
  --name nginx-lb \
  -p 80:80 \
  -p 8081:8081 \
  -p 8082:8082 \
  -v $(pwd)/conf.d:/etc/nginx/conf.d:ro \
  -v $(pwd)/static:/data/nginx/static:ro \
  nginx:1.30_sun_lb

3. 使用 docker-compose(推荐)

创建 docker-compose.yml

yaml 复制代码
name: nginx-weighted-lb
services:
  nginx-lb:
    image: nginx:1.30_sun_lb
    container_name: nginx-lb
    ports:
      - "80:80"
      - "8081:8081"
      - "8082:8082"
    restart: unless-stopped
    # 如需挂载外部配置进行调试,取消注释以下内容
    # volumes:
    #   - ./conf.d:/etc/nginx/conf.d:ro
    #   - ./static:/data/nginx/static:ro

启动服务:

bash 复制代码
docker-compose up -d

✅ 验证测试

1. 检查容器状态

bash 复制代码
docker ps | grep nginx-lb

2. 测试负载均衡效果

bash 复制代码
# 发送100个请求并统计结果
for i in {1..100}; do 
    curl -s http://localhost | grep -o "this is [a-z]*"
done | sort | uniq -c

预期输出(示例):

kotlin 复制代码
   64 this is bing
   36 this is google

3. 直接测试后端服务

bash 复制代码
# 测试 bing 后端(应该总是返回 bing.html)
curl http://localhost:8081

# 测试 google 后端(应该总是返回 google.html)
curl http://localhost:8082

4. 查看Nginx日志

bash 复制代码
# 查看访问日志
docker logs nginx-lb

# 或进入容器查看
docker exec -it nginx-lb cat /var/log/nginx/access.log

📊 架构说明

scss 复制代码
客户端请求 (端口80)
       ↓
  [负载均衡器] ← nginx:1.30_sun_lb
       ↓
   upstream search_backend (权重 2:1)
    ↙              ↘
  bing (8081)   google (8082)
    ↓                ↓
  bing.html      google.html

🔧 注意事项

  1. 端口冲突:确保宿主机端口 80、8081、8082 没有被占用
  2. 权重调整 :如需修改流量比例,编辑 weighted_loadbalance.conf 中的 weight
  3. 热更新 :如需修改配置而不重建镜像,可使用 -v 挂载方式运行
  4. 日志查看 :Nginx日志位于 /var/log/nginx/access.log/var/log/nginx/error.log

这样您就得到了一个完整打包了配置和静态文件的Nginx镜像,可以直接用于生产部署!🎉

相关推荐
王中阳Go1 小时前
苏神百万年薪简历翻车:一个包装争议,给后端同学的 5 个工程视角警示
面试
明月_清风1 小时前
开发者写PPT自救指南:4类对接场景,把技术讲清楚
前端·后端·面试
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的关联容器
开发语言·c++·面试
黄敬峰2 小时前
用 NestJS 实现待办事项接口:RESTful CRUD 与错误处理一次讲透
面试
蔓越莓2 小时前
打包工具:编译器ESBuild
前端·面试
Escalating_xu3 小时前
【Linux】进程信号:从产生、阻塞与递达到 sigaction、可重入与内核返回路径
linux·运维·面试·职场和发展
JL154 小时前
Java并发编程面试全攻略-从synchronized到AQS底层原理
java·开发语言·面试·并发编程
程序员爱钓鱼4 小时前
Go 编程实战:数组 Array——固定长度的数据集合
后端·面试·go