您的浏览器不支持canvas标签,请您更换浏览器

编译文件迁移之Docker镜像瘦身

** 写了一个处理音视频的服务, 之前都是直接安装调用,每次新增服务器都要重新装一遍比较麻烦,所有有了这篇文章, 本文将基于HYPERF的基础DOCKERFILE 进行测试及搭建: HYPERF、FFMPEG、SPEEX2WAV环境 **

构建一个未瘦身的环境 dockerfile如下

FROM hyperf/hyperf:latest
LABEL maintainer="LW放下" version="1.0" license="MIT"

COPY ffmpeg-4.1 /tmp/ffmpeg-4.1
COPY lame-3.99.5 /tmp/lame-3.99.5
COPY speex-1.2.0 /tmp/speex-1.2.0
COPY wechat-speex-declib /tmp/wechat-speex-declib

RUN set -ex \
    && ls -al \
    && apk add --no-cache --update autoconf \
                   automake \
                   bash \
                   binutils \
                   bzip2 \
                   cmake \
                   curl \
                   coreutils \
                   diffutils \
                   file \
                   g++ \
                   gcc \
                   gperf \
                   libtool \
                   make \
                   tar \
                   yasm \
                   nasm \
                   zlib-dev \
                   expat-dev \
    && cd /tmp/lame-3.99.5 \
    && ./configure \
    && make \
    && make install \
    && cd /tmp/ffmpeg-4.1 \
    && ./configure --enable-libmp3lame \
    && make \
    && make install \
    && cd /tmp/speex-1.2.0 \
    && ./configure \
    && make \
    && make install \
    && cd /tmp/wechat-speex-declib \
    && make \
    && cp ./bin/speex_decode /usr/local/bin/speex2wav \
	&& apk del --purge *-dev \
    && rm -rf /var/cache/apk/* /tmp/*  \
    && php -v \
    && php -m \
    && echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR /hyperf-skeleton/

所需文件可以查看 speex2wav ffmpeg

如下为build出来的image,可以发现很大(大得惊人)

test1

开始骚操作找到相关依赖及执行文件位置,需要掌握如下技能

  • 百度
  • linux基础命令: cp、ls、ln、which、ldd、grep
  • docker基础

下面命令不知道干啥的自己百度

ffmpeg相关文件

基于刚刚的image创建一个容器

docker run --name hyperf-fmpeg-test -p 9420:9420 -it --entrypoint /bin/sh hyperf-fmpeg-test:1.0

which ffmpeg

/usr/local/bin/ffmpeg

ldd ffmpeg

/lib/ld-musl-x86_64.so.1 (0x7efd831c9000)
libz.so.1 => /lib/libz.so.1 (0x7efd81741000)
libmp3lame.so.0 => /usr/local/lib/libmp3lame.so.0 (0x7efd81690000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7efd831c9000)

ldd /usr/local/bin/speex2wav

/lib/ld-musl-x86_64.so.1 (0x7f86c0e28000)
libspeex.so.1 => /usr/local/lib/libspeex.so.1 (0x7f86c0e08000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f86c0e28000)

查看具体位置及文件

ls -al /lib/ | grep ld-musl

-rwxr-xr-x 1 root root  584304 Aug  7  2019 ld-musl-x86_64.so.1
lrwxrwxrwx 1 root root      19 Nov  6 01:35 libc.musl-x86_64.so.1 -> ld-musl-x86_64.so.1

ls -al /lib/ | grep libz

lrwxrwxrwx 1 root root      14 May  9  2019 libz.so.1 -> libz.so.1.2.11
-rwxr-xr-x 1 root root  100144 Nov 15  2018 libz.so.1.2.11

ls -al /usr/local/lib/ | grep libmp3

-rw-r--r-- 1 root root    749162 Apr 24 09:30 libmp3lame.a
-rwxr-xr-x 1 root root       943 Apr 24 09:30 libmp3lame.la
lrwxrwxrwx 1 root root        19 Apr 24 09:30 libmp3lame.so -> libmp3lame.so.0.0.0
lrwxrwxrwx 1 root root        19 Apr 24 09:30 libmp3lame.so.0 -> libmp3lame.so.0.0.0
-rwxr-xr-x 1 root root    559128 Apr 24 09:30 libmp3lame.so.0.0.0

如上所示,找到了文件我们得拿出来放到基础的hyperf环境里面

1.先拷贝所需文件到宿主机

docker cp 9aff3c919e19:/lib/ld-musl-x86_64.so.1 /www/wgc_service/ffmpeg-static/

docker cp 9aff3c919e19:/lib/libz.so.1.2.11 /www/wgc_service/ffmpeg-static/

docker cp 9aff3c919e19:/usr/local/lib/libmp3lame.a /www/wgc_service/ffmpeg-static/
docker cp 9aff3c919e19:/usr/local/lib/libmp3lame.la /www/wgc_service/ffmpeg-static/
docker cp 9aff3c919e19:/usr/local/lib/libmp3lame.so.0.0.0 /www/wgc_service/ffmpeg-static/

docker cp 9aff3c919e19:/usr/local/lib/libspeex.a /www/wgc_service/ffmpeg-static/
docker cp 9aff3c919e19:/usr/local/lib/libspeex.la /www/wgc_service/ffmpeg-static/
docker cp 9aff3c919e19:/usr/local/lib/libspeex.so.1.5.1 /www/wgc_service/ffmpeg-static/

docker cp 9aff3c919e19:/usr/local/bin/speex2wav /www/wgc_service/ffmpeg-static/

开始测试

创建基础hyperf容器

docker run -v /www/wgc_service/ffmpeg-static/:/www/wgc_service/ffmpeg-static/ -p 9404:9404 --name hyperftestnew -it --entrypoint /bin/sh hyperf/hyperf:latest

进入容器执行(其实就是还原另一个容器的一些文件路径及结构)

cp -p /www/wgc_service/ffmpeg-static/ld-musl-x86_64.so.1 /lib

cp -p /www/wgc_service/ffmpeg-static/libz.so.1.2.11 /lib

cp -p /www/wgc_service/ffmpeg-static/libmp3lame.a /usr/local/lib
cp -p /www/wgc_service/ffmpeg-static/libmp3lame.la /usr/local/lib
cp -p /www/wgc_service/ffmpeg-static/libmp3lame.so.0.0.0 /usr/local/lib

cp -p /www/wgc_service/ffmpeg-static/libspeex.a /usr/local/lib
cp -p /www/wgc_service/ffmpeg-static/libspeex.la /usr/local/lib
cp -p /www/wgc_service/ffmpeg-static/libspeex.so.1.5.1 /usr/local/lib

cp -p /www/wgc_service/ffmpeg-static/ffmpeg /usr/local/bin

cp -p /www/wgc_service/ffmpeg-static/speex2wav /usr/local/bin

ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/local/lib/libmp3lame.so.0
ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/local/lib/libmp3lame.so

ln -s /usr/local/lib/libspeex.so /usr/local/lib/libspeex.so.1.5.1
ln -s /usr/local/lib/libspeex.so.1 /usr/local/lib/libspeex.so.1.5.1

来吧, 测试一下看看行不行(注意替换路径)

ffmpeg -version
speex2wav -version
ffmpeg -i input.mp3 -af volume=2 -y out.mp3

不出意外是可以的,开始编写dockerfile吧

FROM hyperf/hyperf:latest
LABEL maintainer="LW放下" version="1.0" license="MIT"

COPY ld-musl-x86_64.so.1 /lib
COPY libz.so.1.2.11 /lib
COPY libmp3lame.a /usr/local/lib
COPY libmp3lame.la /usr/local/lib
COPY libmp3lame.so.0.0.0 /usr/local/lib
COPY ffmpeg /usr/local/bin
COPY libspeex.a /usr/local/lib
COPY libspeex.la /usr/local/lib
COPY libspeex.so.1.5.1 /usr/local/lib
COPY speex2wav /usr/local/bin/

RUN set -ex \
    && chmod +x /usr/local/bin/ffmpeg \
    && chmod +x /usr/local/bin/speex2wav \
    && ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/local/lib/libmp3lame.so.0 \
    && ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/local/lib/libmp3lame.so \
    && ln -s /usr/local/lib/libspeex.so.1.5.1 /usr/local/lib/libspeex.so \
    && ln -s /usr/local/lib/libspeex.so.1.5.1 /usr/local/lib/libspeex.so.1 \
    && ffmpeg -version \
	&& speex2wav -version \
    && php -v \
    && php -m \
    && echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR /hyperf-skeleton/

构建试试

docker build -t hyperf-fmpeg-base:1.0 -f Dockerfile .

如图所示, 可以发现少了几百M

test2

所需文件已放在github了

Dockerfiles

LW放下的博客
请先登录后发表评论
  • latest comments
  • 总共0条评论