前言
简介:在搭建视频流直播服务时需要用到ffmpeg、nginx、nginx-rtmp-module,从而实现推流和拉流。但传统的安装方式需要安装各种依赖,受服务器系统的影响,安装繁琐。在此编写Dockerfile用于构建一个docker镜像,实现跨平台一键式安装。
说明:
该镜像在docker镜像仓库中已经有了,地址为 https://hub.docker.com/r/alqutami/rtmp-hls/tags
就可以下载镜像了,自己制作镜像受网络影响可能会制作失败

# 执行执行拉取命令 就可以直接下载镜像了
docker pull alqutami/rtmp-hls

不废话了,直接上Dockerfiel配置
附带docker仓库地址 点我 https://hub.docker.com

mkdir /opt/docker
cd /opt/docker
touch Dockerfile
vi Dockerfile
# 以下部分为Dockerfile文件内容
ARG ALPINE_VERSION=3.8

##### Building stage #####
FROM alpine:${ALPINE_VERSION} as builder
MAINTAINER Tareq Alqutami <tareqaziz2010@gmail.com>

# Versions of nginx, rtmp-module and ffmpeg 
ARG  NGINX_VERSION=1.17.5
ARG  NGINX_RTMP_MODULE_VERSION=1.2.1
ARG  FFMPEG_VERSION=4.2.1

# Install dependencies
RUN apk update	&& \
	apk --no-cache add \
		bash build-base ca-certificates \
		openssl openssl-dev make \
		gcc libgcc libc-dev rtmpdump-dev \
		zlib-dev musl-dev pcre pcre-dev \
		yasm pkgconf pkgconfig libtheora-dev \
		libvorbis-dev libvpx-dev freetype-dev  \
		x264-dev x265-dev && \
    rm -rf /var/lib/apt/lists/*	
	
# Download nginx source
RUN mkdir -p /tmp/build && \
	cd /tmp/build && \
	wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
	tar zxf nginx-${NGINX_VERSION}.tar.gz && \
	rm nginx-${NGINX_VERSION}.tar.gz

# Download rtmp-module source
RUN cd /tmp/build && \
    wget https://github.com/arut/nginx-rtmp-module/archive/v${NGINX_RTMP_MODULE_VERSION}.tar.gz && \
    tar zxf v${NGINX_RTMP_MODULE_VERSION}.tar.gz && \
	rm v${NGINX_RTMP_MODULE_VERSION}.tar.gz
	
# Build nginx with nginx-rtmp module
RUN cd /tmp/build/nginx-${NGINX_VERSION} && \
    ./configure \
        --sbin-path=/usr/local/sbin/nginx \
        --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/nginx.pid \
        --lock-path=/var/lock/nginx.lock \
        --http-client-body-temp-path=/tmp/nginx-client-body \
        --with-http_ssl_module \
        --with-threads \
        --add-module=/tmp/build/nginx-rtmp-module-${NGINX_RTMP_MODULE_VERSION} && \
    make -j $(getconf _NPROCESSORS_ONLN) && \
    make install

# Download ffmpeg source
RUN cd /tmp/build && \
  wget http://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.gz && \
  tar zxf ffmpeg-${FFMPEG_VERSION}.tar.gz && \
  rm ffmpeg-${FFMPEG_VERSION}.tar.gz

# Build ffmpeg
RUN cd /tmp/build/ffmpeg-${FFMPEG_VERSION} && \
  ./configure \
	  --enable-version3 \
	  --enable-gpl \
	  --enable-small \
	  --enable-libx264 \
	  --enable-libx265 \
	  --enable-libvpx \
	  --enable-libtheora \
	  --enable-libvorbis \
	  --enable-librtmp \
	  --enable-postproc \
	  --enable-avresample \
	  --enable-swresample \ 
	  --enable-libfreetype \
	  --disable-debug \
	  --disable-doc \
	  --disable-ffplay \
	  --extra-libs="-lpthread -lm" && \
	make -j $(getconf _NPROCESSORS_ONLN) && \
	make install	
	
# Copy stats.xsl file to nginx html directory and clean build files
RUN cp /tmp/build/nginx-rtmp-module-${NGINX_RTMP_MODULE_VERSION}/stat.xsl /usr/local/nginx/html/stat.xsl && \
	rm -rf /tmp/build

##### Building the final image #####
FROM alpine:${ALPINE_VERSION}

# Install dependencies
RUN apk update	&& \
	apk --no-cache add \
		bash ca-certificates openssl \
		pcre libtheora libvorbis libvpx \
		librtmp x264-dev x265-dev freetype && \
    rm -rf /var/lib/apt/lists/*	

# Copy files from build stage to final stage		
COPY --from=builder /usr/local /usr/local
COPY --from=builder /etc/nginx /etc/nginx
COPY --from=builder /var/log/nginx /var/log/nginx
COPY --from=builder /var/lock /var/lock
COPY --from=builder /var/run/nginx /var/run/nginx

# Forward logs to Docker
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

# Copy  nginx config file to container
COPY conf/nginx.conf /etc/nginx/nginx.conf

# Copy  html players to container
COPY players /usr/local/nginx/html/players

EXPOSE 1935
EXPOSE 8080

CMD ["nginx", "-g", "daemon off;"]

Dockerfile编写完毕,现在可以构建镜像了

# 执行构建镜像命令。 . 表示的是Dockerfile文件的位置为当前目录下
docker build -t nginx-rtmp:latest .

镜像制作完毕,可以启动容器玩一玩了

GitHub 加速计划 / ng / nginx-rtmp-module
13.26 K
3.5 K
下载
nginx-rtmp-module: nginx-rtmp-module 是一个 Nginx 模块,用于支持 RTMP、HLS 和 MPEG-DASH 直播流媒体,提供录制、转码和HTTP回调等功能。
最近提交(Master分支:2 个月前 )
2fb11dff While rtmp module does not support SSL, starting from nginx 1.25.5 an SSL connection can be passed from nginx stream pass module. Such connections should be shut down on connection closure. An rtmps example: rtmp { server { listen 1935; # rtmp application foo { live on; } } } stream { server { listen 1936 ssl; # rtmps ssl_certificate example.com.crt; ssl_certificate_key example.com.key; pass 127.0.0.1:1935; } } 7 个月前
6f9fa49a The change repeats nginx commit cb149fa03367 and is needed for stream pass module to be able to pass connections to rtmp. 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐