Skip to content

Docker部署说明

chatop2020 edited this page Apr 26, 2023 · 4 revisions

有网友提供了DockerFile部署教程和脚本,已合并到了AKStream Dev/Smart 两个分支。

以下文章来源于畅聊了个科技 ,作者畅聊了个科技,原文传送门:【I】

具体部署说明可以点这里 部署说明

因为是一个.NET Core C# 的流媒体项目,且在音视频安防领域用c#做的程序员又很少。所以这个项目我一定要支持,加入AK社区群后,很多不会做技术或者说小白(不擅长c#)编程的程序员,在编译或者运行的时候,常常因为第一步运行都纠结苦恼。

鉴于这样一种背景,我自动畅为爱发电,决定在今天(周六)写个构建镜像的脚本,只需要执行简单的命令就可以构建镜像并且运行起来。这样就降低了使用者的门槛。

执行命令如下:


构建ak-web镜像:bash deploy.sh -web
构建ak-keeper镜像:bash deploy.sh -keeper
运行ak-web镜像:bash deploy.sh -dw
运行ak-keeper镜像:bash deploy.sh -dk

就需要几个命令就可以了。是不是很简单,今天刚编译的镜像的,因为推送dockerhub上,下载比较慢。我把一个基础镜像导出去了。通过在github下载离线包,进行加载基础镜像。然后在本地快速部署。命令我都写好了。大家也可以参考这个脚本的模式,构建你们自己的服务镜像。如果脚本配合自动发布,例如Jenkins,就可以实现流水线CICD一套流程了。

脚本如下:

deploy.sh


#!/bin/bash

#作者(author):自动畅(auto-chang)
#微信号(WeChat):自动畅(auto-chang)
#公众号(WeChat official account):畅聊了个科技(IT-chang)
#时间(Time):2023年4月22日,星期六(Saturday, April 22, 2023)
#描述(Describe:为爱发电(Power Generation for Love)

usage() {
  cat << EOF >&2
Usage: $PROGNAME
 -keeper --install-keeper
 -web --install-Web
 -dk --deploy-keeper
 -dw --deploy-Web
 -h --help
EOF
exit 1
}


if [ "$1" == "" ]; then
    usage
fi

while true; do
  case "$1" in
    -keeper | --install-keeper ) ACTION=install-keeper; shift ;;
    -web | --install-Web ) ACTION=install-Web; shift ;;
    -dk | --deploy-keeper ) ACTION=deploy-keeper; shift ;;
    -dW | --deploy-Web ) ACTION=deploy-Web; shift ;;
    -h | --help ) usage; exit 1 ;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done


APP_KEEPER_NAME=ak-keeper
APP_WEB_NAME=ak-web

echo "Welcome to the AKStream(c# NetCore Programming Language),You have chosen an $ACTION "
echo "You have chosen an $ACTION "

if [ "$ACTION" == install-keeper ]; then
    if [! -f ./ubuntu-zlm-ffmpeg-dotnet.tar ] 
    then 
         wget  https://github.com/itchangc/toolbox/blob/main/ubuntu-zlm-ffmpeg-dotnet.tar
         docker import ubuntu-zlm-ffmpeg-dotnet.tar
    fi
    docker build -f Dockerfile-Keeper -t $APP_KEEPER_NAME .
fi

if [ "$ACTION" == install-Web ]; then
    docker build -f Dockerfile-Web -t $APP_WEB_NAME .
fi

if [ "$ACTION" == deploy-keeper ]; then
  # shellcheck disable=SC2046
  docker stop $(docker ps | grep $APP_KEEPER_NAME | awk '{print $1}')
  # shellcheck disable=SC2046
  docker rm $(docker ps -a | grep $APP_KEEPER_NAME | awk '{print $1}')
  # shellcheck disable=SC2046
  docker rmi $(docker images | grep $APP_KEEPER_NAME | awk '{print $3}')

  docker run -p 6880:6880 \
    -p 10001-1010:10001-1010 \
    -p 10001-1010:10001-1010/udp \
    -p 20002-20200:20002-20200 \
    -p 20002-20200:20002-20200/udp \
    -p 6881:80 \
    -p 6882:1935 \
    -p 6883:554 \
    -p 6883:554/udp \
    -p 10000:10000 \
    -p 10000:10000/udp \
    -p 8000:8000/udp \
    -p 30000-30035:30000-30035/udp \
    -v ./AKStreamKeeper/Config/AKStreamKeeper.json:/root/AKStreamKeeper/Config/AKStreamKeeper.json \
    -v ./AKStreamKeeper/Config/logconfig.xml:/root/AKStreamKeeper/Config/logconfig.xml \
    --name=$APP_KEEPER_NAME \
    --restart=always \
    -d $APP_KEEPER_NAME
fi
if [ "$ACTION" == deploy-Web ]; then
  # shellcheck disable=SC2046
  docker stop $(docker ps | grep $APP_WEB_NAME | awk '{print $1}')
  # shellcheck disable=SC2046
  docker rm $(docker ps -a | grep $APP_WEB_NAME | awk '{print $1}')
  # shellcheck disable=SC2046
  docker rmi $(docker images | grep $APP_WEB_NAME | awk '{print $3}')

  docker run -p 5800:5800 \
    -p 6880:6880 \
    -p 5060:5060 \
    -p 5060:5060/udp \
    -v ./AKStreamWeb/Config/AKStreamWeb.json:/app/Config/AKStreamWeb.json \
    -v ./AKStreamWeb/Config/SipClientConfig.json:/app/Config/SipClientConfig.json  \
    -v ./AKStreamWeb/Config/SipServerConfig.json:/app/Config/SipServerConfig.json \
    -v ./AKStreamWeb/Config/logconfig.xml:/app/Config/logconfig.xml \
    --name=$APP_WEB_NAME \
    --restart=always \
    -d $APP_WEB_NAME
fi

echo "Successful script execution, best wishes for you"

Dockerfile-Web

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#作者(author):自动畅(auto-chang)
#微信号(WeChat):自动畅(auto-chang)
#公众号(WeChat official account):畅聊了个科技(IT-chang)
#时间(Time):2023年4月22日,星期六(Saturday, April 22, 2023)
#描述(Describe:为爱发电(Power Generation for Love)

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["AKStreamWeb/AKStreamWeb.csproj", "AKStreamWeb/"]
COPY ["LibCommon/LibCommon.csproj", "LibCommon/"]
COPY ["LibLogger/LibLogger.csproj", "LibLogger/"]
COPY ["SipSorcery/SIPSorcery.csproj", "SipSorcery/"]
COPY ["LibGB28181SipClient/LibGB28181SipClient.csproj", "LibGB28181SipClient/"]
COPY ["LibSystemInfo/LibSystemInfo.csproj", "LibSystemInfo/"]
COPY ["SystemInfoLibrary/src/SystemInfoLibrary.Standard/SystemInfoLibrary.Standard.csproj", "SystemInfoLibrary/src/SystemInfoLibrary.Standard/"]
COPY ["LibGB28181SipServer/LibGB28181SipServer.csproj", "LibGB28181SipServer/"]
COPY ["LibZLMediaKitMediaServer/LibZLMediaKitMediaServer.csproj", "LibZLMediaKitMediaServer/"]
RUN dotnet restore "AKStreamWeb/AKStreamWeb.csproj"
COPY . .
WORKDIR "/src/AKStreamWeb"
RUN dotnet build "AKStreamWeb.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "AKStreamWeb.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AKStreamWeb.dll"]

Dockerfile-Keeper


#作者(author):自动畅(auto-chang)
#微信号(WeChat):自动畅(auto-chang)
#公众号(WeChat official account):畅聊了个科技(IT-chang)
#时间(Time):2023年4月22日,星期六(Saturday, April 22, 2023)
#描述(Describe:为爱发电(Power Generation for Love)

FROM ubuntu-zlm-ffmpeg-dotnet AS keeper
WORKDIR /root/src
COPY . .
RUN mkdir -p /root/AKStreamKeeper 
WORKDIR /root/src/AKStreamKeeper
RUN ln -s $HOME/dotnet/dotnet /usr/bin/dotnet -f
RUN dotnet build "AKStreamKeeper.csproj" -c Release  -o /app/build
RUN dotnet publish  "AKStreamKeeper.csproj" -c Release  -o /app/publish
EXPOSE 80
EXPOSE 443
EXPOSE 6880
RUN mv  /app/publish/* /root/AKStreamKeeper
WORKDIR /root/AKStreamKeeper

我在脚本里写明了,为爱发电。虽然脚本看着简单,但是写起来真的很费时间,本来周六可以看个电视剧、约个朋友呢,但是心里想着做这样一件事。于是为爱发电,我为.NET Core C# 流媒体贡献不大,AK作者才是最辛苦的,一个人默默无闻的贡献着。这里给作者点赞。

好了,今天的分享就到这里,洗洗澡。明天还得上班,

最后,预祝大家五一快乐!