Featured image of post freeswitch1.10.7缩减镜像包大小

freeswitch1.10.7缩减镜像包大小

背景

最近想升级freeswitch的版本, 需要制作freeswitch1.10.7的镜像,记录一下制作过程。

freeswitch的官方安装文档为: linux安装

使用的基础镜像为:debian:bullseye

特殊要求

  1. lua5.3
  2. 使用openh264编码

说明: freeswitch1.10.7版本默认使用的是lua5.2,所以在编译mod_lua时需要指定lua5.3的路径。

制作准备

因为要使用依赖安装, 所以需要使用token

  1. 注册账号: signalwire
  2. 生成token, 方法为: how-to-create-a-FSA-API-Key-access-token

Dockerfile

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
# ===============================
# 1. Builder 阶段:编译 freeswitch
# ===============================
FROM debian:bullseye AS builder

WORKDIR /build

# 使用阿里云 Debian 源
RUN echo 'deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n\
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main\n\
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main\n\
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib' \
> /etc/apt/sources.list

# 基础工具
RUN apt-get update && apt-get install -y \
    wget curl zip gnupg lsb-release build-essential \
    pkg-config libtool automake autoconf nasm apt-utils git cmake \
    && rm -rf /var/lib/apt/lists/*

# 拷贝源码
COPY . .

# 添加 FreeSWITCH SignalWire 官方源(需要账号密码,pat_xxxxxxxxxxxxxx为token)
RUN  wget --http-user=signalwire --http-password=pat_xxxxxxxxxxxxxx -O /usr/share/keyrings/signalwire-freeswitch-repo.gpg https://freeswitch.signalwire.com/repo/deb/debian-release/signalwire-freeswitch-repo.gpg \
    && echo "machine freeswitch.signalwire.com login signalwire password pat_xxxxxxxxxxxxxx" > /etc/apt/auth.conf \
    && echo "deb [signed-by=/usr/share/keyrings/signalwire-freeswitch-repo.gpg] https://freeswitch.signalwire.com/repo/deb/debian-release $(lsb_release -sc) main" \
    > /etc/apt/sources.list.d/freeswitch.list \
    && echo "deb-src [signed-by=/usr/share/keyrings/signalwire-freeswitch-repo.gpg] https://freeswitch.signalwire.com/repo/deb/debian-release $(lsb_release -sc) main" \
    >> /etc/apt/sources.list.d/freeswitch.list

# 安装 freeswitch 构建依赖
RUN apt-get update && apt-get build-dep -y freeswitch \
    && apt-get remove --purge liblua5.2-dev liblua5.2-0 -y \
    && apt-get install -y liblua5.3-0 liblua5.3-dev lua5.3 libks \
    && rm -rf /var/lib/apt/lists/*
# 编译第三方依赖 (openh264 / luasocket)
RUN git clone https://github.com/cisco/openh264.git
    && cd openh264-2.6.0 && make ENABLE64BIT=Yes -j$(nproc) && make install \
    && cd .. && git clone https://github.com/lunarmodules/luasocket.git \
    && cd luasocket && make linux -j$(nproc) && make install \
    && cd .. && git clone https://github.com/lunarmodules/lua-iconv.git && cd lua-iconv \
    && make LUA_INCLUDE=/usr/include/lua5.3 && make install LUA_LIBDIR=/usr/local/lib/lua/5.3 \
    && ldconfig

ENV CPPFLAGS="-I/usr/include/lua5.3"
RUN ln -s /usr/lib/x86_64-linux-gnu/liblua5.3.so /usr/lib/x86_64-linux-gnu/liblua.so

# 编译 freeswitch
RUN cd freeswitch \
    && ./bootstrap.sh \
    && cp /build/modules.conf . \
    && ./configure --disable-signalwire \
    && make -j$(nproc) && make install

# 精简二进制文件(去掉调试符号)
RUN strip /usr/local/freeswitch/bin/* || true \
    && strip /usr/local/freeswitch/mod/*.so || true

# ===============================
# 2. Library-check 阶段:检测依赖
# ===============================
FROM builder AS libcheck

COPY check-libs.sh /check-libs.sh 
RUN chmod +x /check-libs.sh && /check-libs.sh 

# ===============================
# 3. Runtime 阶段:运行 freeswitch
# ===============================
FROM debian:bullseye-slim AS runtime

# 使用阿里云 Debian 源
RUN echo 'deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n\
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main\n\
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main\n\
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib' \
> /etc/apt/sources.list

RUN apt-get update && apt-get install -y \
    liblua5.3-0 lua5.3 libsqlite3-0 vim curl wget \
    && rm -rf /var/lib/apt/lists/*

# 拷贝运行需要的库
COPY --from=libcheck /libs/ /usr/local/lib/
RUN mv /usr/local/lib/share/lua /usr/local/share/
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

# 拷贝 freeswitch
COPY --from=builder /usr/local/freeswitch /usr/local/freeswitch
COPY --from=builder /build/conf conf

# 修改配置和删除安装包
RUN cp conf/vars.xml /usr/local/freeswitch/conf/ \
    && mv /usr/local/freeswitch/conf/sip_profiles/internal-ipv6.xml  /usr/local/freeswitch/conf/sip_profiles/internal-ipv6.xml.noload \
    && mv /usr/local/freeswitch/conf/sip_profiles/external-ipv6.xml  /usr/local/freeswitch/conf/sip_profiles/external-ipv6.xml.noload 

WORKDIR /usr/local/freeswitch
# 运行
CMD ["/usr/local/freeswitch/bin/freeswitch", "-nonat"]

说明: lua-iconvlua的一个扩展库, 如果你不需要可以删掉。 如果需要,下载到源码之后还要改它的Makefile里的LUAPKG = lua5.2LUAPKG = lua5.3

check-libs.sh的脚本为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
set -e

OUT_DIR="/libs"
mkdir -p "$OUT_DIR/share/lua/5.3"

# 要检查依赖的二进制/模块
TARGETS="
/usr/local/freeswitch/bin/freeswitch
/usr/local/freeswitch/bin/fs_cli
/usr/local/freeswitch/mod/*.so
"

for f in $TARGETS; do
    if [ -f "$f" ]; then
        echo ">>>> ldd $f"
        for so in $(ldd "$f" | awk '{print $3}' | grep "^/"); do
            cp -v "$so" "$OUT_DIR" || true
        done
    fi
done

# 额外拷贝自编译库(openh264)
if [ -d /usr/local/lib ]; then
    cp -rv /usr/local/lib/* "$OUT_DIR" || true
fi

# 额外拷贝 luasocket 脚本模块
if [ -d /usr/local/share/lua/5.3 ]; then
    cp -rv /usr/local/share/lua/5.3/* "$OUT_DIR/share/lua/5.3/" || true
fi

freeswitch执行./configure之前,修改modules.conf:

1
2
3
4
5
6
#applications/mod_signalwire
formats/mod_shout
formats/mod_vlc
asr_tts/mod_unimrcp
codecs/mod_openh264
#codecs/mod_h26x

另外把freeswitch配置中需要修改的文件统一放在了conf目录下了, 所以会有COPY --from=builder /build/conf conf,以及RUN cp conf/vars.xml /usr/local/freeswitch/conf/

这里我主要改了vars.xmlautoload_configs/modules.conf.xml:

  1. vars.xml:
1
2
<X-PRE-PROCESS cmd="stun-set" data="external_rtp_ip=$${local_ip_v4}"/>
<X-PRE-PROCESS cmd="stun-set" data="external_sip_ip=$${local_ip_v4}"/>
  1. autoload_configs/modules.conf.xml:
1
2
<!-- <load module="mod_signalwire"/> -->
<load module="mod_openh264"/>

总结

目前最终生成出来的镜像大小为:365MB, 如果直接使用编译时Builder的镜像,大小为:3.3G。 使用此方法可以极大缩小镜像包大小。

本博客已稳定运行
发表了59篇文章 · 总计100.71k字
本站总访问量 次 · 您是本站第 位访问者
粤ICP备2025368587号-1| 使用 Hugo 构建
主题 StackJimmy 设计