Skip to content

27 生成 ffmpeg docker image

Jinxin Chen edited this page Dec 11, 2019 · 1 revision

本文介绍如何基于 .net core runtime Image 生成包含 ffmpeg 功能的 Image

背景

想要在Container中使用ffmpeg转码,有两种方法:

  1. 在Container中启动包含ffmpeg Container,执行转码
  2. 将ffmpeg构建在同一个Image中,直接使用

第1种方法的好处是,解耦,不会让目标Image变得很大,但是将docker.sock暴露给Container会有安全风险,不建议。 第2种方法尝试了ppa源安装但失败了,最后直接从Source Code安装成功

直接上Dockerfile

FROM microsoft/dotnet:2.2-runtime AS base
WORKDIR /app

FROM base as ffmpegbase
RUN apt-get update ; apt-get install -y git build-essential gcc make yasm autoconf automake cmake libtool checkinstall libmp3lame-dev pkg-config libunwind-dev zlib1g-dev libssl-dev

RUN apt-get update \
    && apt-get clean \
    && apt-get install -y --no-install-recommends libc6-dev libgdiplus wget software-properties-common

#RUN RUN apt-add-repository ppa:git-core/ppa && apt-get update && apt-get install -y git

RUN wget https://www.ffmpeg.org/releases/ffmpeg-4.0.2.tar.gz \
    && tar -xzf ffmpeg-4.0.2.tar.gz; rm -r ffmpeg-4.0.2.tar.gz \
    && cd ./ffmpeg-4.0.2 \
    && ./configure --enable-gpl --enable-libmp3lame --enable-decoder=mjpeg,png --enable-encoder=png --enable-openssl --enable-nonfree \
    && make \
    && make install \
    && cd .. \
    && rm -r ffmpeg-4.0.2

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

优化Image大小

目前通过这种方法生成的Image解压后的大小在800M左右,还有不少优化空间。

参考了大神的Dockerfile之后,结合Docker的Stage功能,最后将Image优化为200M左右的大小:

FROM jrottenberg/ffmpeg:4.0-scratch AS ffmpegbase

FROM microsoft/dotnet:2.2-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY src/VideoAgent/VideoAgent.csproj src/VideoAgent/
COPY src/VideoLib/VideoLib.csproj src/VideoLib/
RUN dotnet restore src/VideoAgent/VideoAgent.csproj
COPY . .
WORKDIR /src/src/VideoAgent
RUN dotnet build VideoAgent.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish VideoAgent.csproj -c Release -o /app

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

参考

Clone this wiki locally