FROM registry.cn-chengdu.aliyuncs.com/reghao/debian:bullseye-slim

# debian11 使用 ustc 源
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list

# 安装编译 nginx 源码需要的依赖
RUN apt clean && apt update -y && apt install -y --no-install-recommends \
    gcc make zlib1g zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev libgd-dev libexif-dev && \
    rm -rf /var/lib/apt/lists/*

# 复制 nginx 和 nginx-http-flv-module 源码
COPY nginx-1.18.0.tar.gz /nginx-1.18.0.tar.gz
COPY v1.2.12.tar.gz /v1.2.12.tar.gz
RUN tar zxf nginx-1.18.0.tar.gz && tar zxf v1.2.12.tar.gz

# 创建 nginx 用户
RUN useradd -M -s /sbin/nologin nginx
# 切换至 nginx 源码目录
WORKDIR /nginx-1.18.0

# 配置编译选项, --add-module 指定了 nginx-http-flv-module 模块
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/logs/nginx.lock \
--with-http_stub_status_module \
--with-http_ssl_module \
--add-module=/nginx-http-flv-module-1.2.12
# 编译源码
RUN make && make install

# 将 nginx 日志重定向到 stdout 和 stderr, 这样可通过 docker logs 直接查看日志
RUN ln -sf /dev/stdout /usr/local/nginx/logs/access.log && ln -sf /dev/stderr /usr/local/nginx/logs/error.log
# 创建 rtmp 推流的 flv 文件存放位置, rtmp 的 record_path 参数中配置
RUN mkdir -p /data/rtmp
# 使用自定义的 nginx.conf
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /

# 删除安装使用的文件和文件夹
RUN rm -rf nginx-1.18.0.tar.gz
RUN rm -rf nginx-1.18.0
RUN rm -rf nginx-http-flv-module-1.2.12.tar.gz
RUN rm -rf nginx-http-flv-module-1.2.12

# nginx 默认以后台模式启动, docker 未执行到自定义的 CMD 之前 nginx 的 pid 是 1
# 执行到 CMD 后 nginx 开始后台运行，bash 的 pid 变成了 1, 一旦执行完自定义 CMD，nginx 容器也就退出
# 为了保证 nginx 容器不退出, 应该关闭 nginx 的后台运行
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]