Docker学习之基于CentOS7.8.2003基础镜像下nginx-rtmp-ffmpeg流服务镜像创建
文章目录
基于CentOS7.8.2003基础镜像下nginx-rtmp-ffmpeg流服务镜像创建
Docker CentOS 镜像容器启动
启动Docker Engine
[root@VM-0-8-centos /]# systemctl start docker
列出CentOS系统镜像列表
[root@VM-0-8-centos /]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6149 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 119 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 115 [OK]
centos/systemd systemd enabled base container. 87 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 81
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 47
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 46
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 12
guyton/centos6 From official centos6 container with full up… 10 [OK]
centos/tools Docker image that has systems administration… 6 [OK]
drecom/centos-ruby centos ruby 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
mcnaughton/centos-base centos base image 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
拉取CentOS镜像(这里我拉取得是CentOS 7.8.2003)
[root@VM-0-8-centos /]# docker pull centos:7.8.2003
Using default tag: latest
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
启动CentOS 7.8.2003镜像容器
[root@VM-0-8-centos /]# docker run -it -d -p 11935:1935 -p 180:80 -p 1443:443 centos:7.8.2003 bash
be7b7f9c83a79b2a8ec2cb6a2fbab5a09e1ee04e0ea587bcc25419a6ffd593b9
查看运行中容器并进入
[root@VM-0-8-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34091d225d00 7e6257c9f8d8 "bash" 29 minutes ago Up 29 minutes 0.0.0.0:180->80/tcp, 0.0.0.0:1443->443/tcp, 0.0.0.0:11935->1935/tcp inspiring_matsumoto
[root@VM-0-8-centos /]# docker exec -it 34091d225d00 bash
查看系统版本
[root@34091d225d00 /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
停止Docker容器
docker stop [CONTAINER ID]
安装ffmpeg
注: 此 ffmpeg 安装方式适用于CentOS 7
准备环境
安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持:
[root@34091d225d00 nginx-rtmp-ffmpeg]# yum install -y epel-release
...........
Complete!
#如果出现缺少Code提示,可以:
sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
安装完成之后,可以查看是否安装成功
[root@34091d225d00 /]# yum repolist
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10070
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13426
extras/7/x86_64 CentOS-7 - Extras 413
updates/7/x86_64 CentOS-7 - Updates 1125
repolist: 27758
安装Nux-Dextop源
由于CentOS没有官方FFmpeg rpm软件包,所以使用第三方YUM源(Nux Dextop)完成此工作。
#导入一个Code
[root@34091d225d00 nginx-rtmp-ffmpeg]# rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
#安装nux-dextop 源
[root@34091d225d00 nginx-rtmp-ffmpeg]# rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
Retrieving http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:nux-dextop-release-0-5.el7.nux ################################# [100%]
#查看repo源是否安装成功
[root@34091d225d00 nginx-rtmp-ffmpeg]# yum repolist
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* nux-dextop: li.nux.ro
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10070
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13426
extras/7/x86_64 CentOS-7 - Extras 413
nux-dextop/x86_64 Nux.Ro RPMs for general desktop use 2724
updates/7/x86_64 CentOS-7 - Updates 1125
repolist: 27758
安装ffmpeg
#yum安装ffmpeg:
[root@34091d225d00 nginx-rtmp-ffmpeg]# yum install ffmpeg ffmpeg-devel -y
#安装完成后检查ffmpeg 版本:
[root@34091d225d00 /]# ffmpeg -version
ffmpeg version 2.8.15 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
检查ffmpeg编码
[root@34091d225d00 /]# ffmpeg -codecs
由于我需要编码aac的音频,所以查看编码方式:

Docker 的CentOS镜像容器中安装 ffmpeg 没有问题。
安装 nginx-rtmp
安装Nginx编译需要的依赖
[root@34091d225d00 /]# yum -y install gcc* zlib zlib-devel openssl openssl-devel pcre-devel
创建data/nginx-rtmp-ffmpeg目录
[root@34091d225d00 /]# cd /dev
[root@34091d225d00 dev]# mkdir -p ./data/nginx-rtmp-ffmpeg
[root@34091d225d00 dev]# cd ./data/nginx-rtmp-ffmpeg/
[root@34091d225d00 nginx-rtmp-ffmpeg]#
安装wget
[root@34091d225d00 nginx-rtmp-ffmpeg]# yum install wget
下载nginx安装包以及nginx-rtmp-modul模块
由于需要编译安装第三方模块,所以选择下载安装包安装。
Nginx下载地址:https://nginx.org/
选择合适的版本,这里我现在稳定版本:nginx-1.18.0.tar.gz 。
可以使用wget命令安装,也可以从官网下载安装包安装。
[root@34091d225d00 nginx-rtmp-ffmpeg]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@34091d225d00 nginx-rtmp-ffmpeg]# wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
[root@34091d225d00 nginx-rtmp-ffmpeg]# ls
nginx-1.18.0.tar.gz v1.2.1.tar.gz
解压安装压缩包
[root@34091d225d00 nginx-rtmp-ffmpeg]# tar -zxvf nginx-1.18.0.tar.gz
[root@34091d225d00 nginx-rtmp-ffmpeg]# tar -zxvf v1.2.1.tar.gz
[root@34091d225d00 nginx-rtmp-ffmpeg]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz nginx-rtmp-module-1.2.1 v1.2.1.tar.gz
进入nginx-1.18.0.tar.gz安装包
[root@34091d225d00 nginx-rtmp-ffmpeg]# cd nginx-1.18.0/
配置模块
[root@34091d225d00 nginx-1.18.0]# ./configure --prefix=/usr/local/src/nginx --add-module=../nginx-rtmp-module-1.2.1 --with-http_ssl_module
.............
adding module in ../nginx-rtmp-module-1.2.1
+ ngx_rtmp_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/src/nginx"
nginx binary file: "/usr/local/src/nginx/sbin/nginx"
nginx modules path: "/usr/local/src/nginx/modules"
nginx configuration prefix: "/usr/local/src/nginx/conf"
nginx configuration file: "/usr/local/src/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/src/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/src/nginx/logs/error.log"
nginx http access log file: "/usr/local/src/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
编译并安装
[root@34091d225d00 nginx-1.18.0]# make
.................
sed -e "s|%%PREFIX%%|/usr/local/src/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/src/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/src/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/src/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/data/nginx-rmpt/nginx-1.18.0'
[root@34091d225d00 nginx-1.18.0]# make install
.........
test -d '/usr/local/src/nginx/logs' \
|| mkdir -p '/usr/local/src/nginx/logs'
make[1]: Leaving directory `/dev/data/nginx-rtmp-ffmpeg/nginx-1.18.0'
这个是我最开始直接拉取centos镜像,没有指定版本,拉取得是能拉取的最新的版本,我看了一下是centos8,所以安装nginx-rtmp-module时出现以下错误,这里记录以下
centos8 安装nginx-rtmp-module 报错:
解决办法:
打开/usr/local/src/nginx-rtmp-module/ngx_rtmp_eval.c文件
vim /usr/local/src/nginx-rtmp-module/ngx_rtmp_eval.c
在第169行(空行)插入 /* fall through */ ,保存退出
再执行make一切正常
启动nginx
[root@34091d225d00 sbin]# /usr/local/src/nginx/sbin//nginx
[root@34091d225d00 sbin]# ps -ef | grep nginx
root 10523 1 0 17:29 ? 00:00:00 nginx: master process ./nginx
nobody 10524 10523 0 17:29 ? 00:00:00 nginx: worker process
root 10537 17663 0 17:29 pts/0 00:00:00 grep --color=auto nginx
访问

因为我启动容器的时候映射了端口,将
宿主机的180端口映射到了容器的80端口,所以通过http协议访问容器的nginx(也就是nginx的默认的http的80端口)就需要访问宿主机的180端口。
Docker 的CentOS镜像容器中安装 nginx 没有问题。
nginx-rtmp 流服务配置
nginx.conf 配置
以下是nginx-rtmp流服务的nginx.conf配置
daemon off;
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application stream {
live on;
exec ffmpeg -i rtmp://localhost:1935/stream/$name
-c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1280x720 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name ;
}
application hls {
live on;
hls on;
hls_fragment_naming system;
hls_fragment 2;
hls_playlist_length 20;
hls_path /usr/local/src/nginx/data/hls;
hls_nested on;
hls_cleanup on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/json;
gzip_disable "MSIE [[1-6]]\.";
gzip_vary on;
access_log logs/access.log;
log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_session_cache shared:SSL:10m;
#ssl_session_timeout 10m;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /hls {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 200;
}
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/src/nginx/data;
}
location /live {
alias /usr/local/src/nginx/data/hls;
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
}
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/src/nginx/certs/server.crt;
ssl_certificate_key /usr/local/src/nginx/certs/server.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
location /hls {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 200;
}
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/src/nginx/data;
}
location /live {
alias /usr/local/src/nginx/data/hls;
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
}
}
方式一:手动修改nginx.conf文件
进入容器中的nginx的conf目录,手动替换配置内容
[root@34091d225d00 /]# cd /usr/local/src/nginx/conf
[root@34091d225d00 /]# vi nginx.conf
将上面的配置内容替换掉原nginx.conf内容:
方式二:替换nginx.conf文件
-
将上述配置内容放到宿主机某一目录下,文件名为nginx.conf
-
在宿主机中执行以下命令
[root@VM-0-8-centos ~]# docker cp /usr/local/src/nginx/conf/nginx.conf 34091d225d00:/usr/local/src/nginx/conf
我将文件命名为nginx.conf 放到了
/usr/local/src/nginx/conf目录下。
创建流媒体存储文件目录
[root@34091d225d00 /]# cd /usr/local/src/nginx
[root@34091d225d00 nginx]# mkdir -p ./data/hls
启动nginx
[root@34091d225d00 nginx]# ./sbin/nginx
报错:
原因是没有用于https的ssl证书
如果你不需要用到https,那么将上面的nginx.conf文件的配置内容中的HTTPS server 配置部分注释或删除后启动即可。
如果需要HTTPS server的配置,那么操作如下:
在容器内创建放置证书的目录:
[root@34091d225d00 /]# cd /usr/local/src/nginx [root@34091d225d00 nginx]# mkdir certs [root@34091d225d00 nginx]# ls certs client_body_temp conf data fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@34091d225d00 nginx]# cd certs/ [root@34091d225d00 certs]# pwd /usr/local/src/nginx/certs这里我将证书目录放置到nginx安装目录下
将可用的ssl证书放到宿主机的某一目录下,使用以下命令复制到容器中刚创建的证书目录certs下:
[root@VM-0-8-centos /]# docker cp /usr/local/src/nginx/certs/server.crt 34091d225d00:/usr/local/src/nginx/certs [root@VM-0-8-centos /]# docker cp /usr/local/src/nginx/certs/server.key 34091d225d00:/usr/local/src/nginx/certs这里我把证书放到了宿主机的
/usr/local/src/nginx/certs目录下查看容器中创建的证书目录certs:
[root@34091d225d00 /]# cd /usr/local/src/nginx/certs [root@34091d225d00 certs]# ls server.crt server.key可以发现已经拥有证书了
PS: OpenSSL证书生成方式见文章 创建HTTPS访问SSL免费证书 。
再次启动nginx
[root@34091d225d00 /]# /usr/local/src/nginx/sbin/nginx
[root@34091d225d00 /]# ps -ef | grep nginx
root 4001 0 0 08:55 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 4002 4001 0 08:55 ? 00:00:00 nginx: worker process
nobody 4003 4001 0 08:55 ? 00:00:00 nginx: cache manager process
root 4005 14 0 08:55 pts/1 00:00:00 grep --color=auto nginx
访问
由于我启动容器的时候映射了端口,将
宿主机的1443端口映射到了容器的443端口,将宿主机的180端口映射到了容器的80端口,所以https与http协议访问容器的nginx(也就是nginx的默认的https的443端口与默认的http的80端口)就需要访问宿主机的1443端口与宿主机的180端口。
浏览器输入: https://58.87.107.146:1443

浏览器输入: http://58.87.107.146:180

保存Docker镜像并上传至Docker Hub
- 运行
docker ps查看容器,这里就是要获取到 CONTAINER ID 嘛

-
commit 一个容器 命令:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]][root@34091d225d00 /]# docker commit -a "dancefingers" -m "centos7 nginx-rtmp-ffmpeg Integrated image" 34091d225d00 dancefingers/nginx-rtmp-ffmpeg-centos7:latest
-a : 作者
-m : 提交描述
-
登录docker hub帐号 命令:
docker login -
将刚才的镜像push 到你的 docker hub 那里去 命令:
docker push [OPTIONS] NAME[:TAG][root@VM-0-8-centos ~]# docker push dancefingers/nginx-rtmp-ffmpeg-centos7 The push refers to repository [docker.io/dancefingers/nginx-rtmp-ffmpeg-centos7] bdfba0f05da4: Pushed 613be09ab3c0: Layer already exists latest: digest: sha256:19ca428d439a0ff6bc4cb75db5b98fe91ce78ca2bd3daec54b82137eebd1d063 size: 742

以上就是在CentOS7镜像容器中搭建nginx-rtmp-ffmpeg流服务器的全过程。
不过这里可以看到最终的镜像有点大,2.225GB。。。网速垃圾下push的我想死…push了4次,每次都是差一丢丢断掉了,擦擦的…
原因是在CentOS7中搭建时,会记录你的下载安装所有命令,即使你将安装包删除了也没用,所以要想缩小这个镜像,要么使用挂载的存储路径,要么使用Dockerfile构建并在过程中把使用完的依赖和临时文件卸载删除,有兴趣的可以自行百度。
推拉流验证
下载安装OBS

配置OBS

下载安装VLC

配置VLC

00015
推流

拉流






浙公网安备 33010602011771号