Frigate 0.18.2 Docker Compose 部署指南

Frigate v0.18.2 从零到一 Compose 部署实测:frigate+mediamtx+mosquitto 组合、ffmpeg 循环推流 RTSP 模拟摄像头源、检测/MQTT/快照/录像端到端验证与排障实录(2026-10-27,测试源为本地推流模拟)。

Frigate流媒体自托管Docker Compose2026/10/27

Frigate 0.18.2 Docker Compose 部署指南

目标:在一台 Ubuntu Linux 服务器上,用 Docker Compose 从零到一跑起一套完整的 Frigate v0.18.2 监控检测环境,包括官方推荐配套的 MQTT 代理(Mosquitto)、RTSP 流媒体服务器(MediaMTX),并用 ffmpeg 循环推流一段测试视频充当「模拟摄像头」,端到端验证拉流、AI 检测、MQTT 事件、快照与录像全链路。本篇实测的测试源为本地推流模拟(Intel 开源样例视频循环推 RTSP),无真实摄像头硬件,读者的真实摄像头只需把模拟源地址替换为自己的 RTSP 地址即可。

一、环境信息

本篇全部命令与回显在下列实测环境中逐条跑出(2026-10-27):

项目值
操作系统Ubuntu 22.04(Linux x86_64)
服务器配置6 核 CPU / 19GB 内存 / 无独立显卡
DockerDocker Engine 28 + Docker Compose v2
Frigateghcr.io/blakeblackshear/frigate:0.18.2(镜像构建时间 2026-10-15)
RTSP 服务器bluenviron/mediamtx:1.21.3-ffmpeg
MQTT 代理eclipse-mosquitto:2.0(实测容器内报 mosquitto version 2.0.22)
模拟源视频person-bicycle-car-detection.mp4(Intel intel-iot-devkit/sample-videos 开源样例,H.264 768x432@12fps + AAC,时长 53.9 秒)
Compose 项目名frigate-0.18.2-stack
Docker 网络frigate-net,10.89.65.0/24
宿主端口8138 → Frigate Web UI(5000)、8139 → Frigate RTSP(8554)、8144 → WebRTC(8555)
实测目录/opt/frigate(文档内路径,读者按需自定)

💡 技术拓展:视频监控里常说的「拉流」与「推流」——

方式含义谁发起典型场景
拉流(pull)服务端主动去取流Frigate 连接摄像头摄像头提供 RTSP 服务(最常见)
推流(push)流主动送到服务端摄像头/编码器推送摄像头内网隔离、公网推 RTMP

Frigate 属于「拉流」架构:配置文件里写 RTSP 地址,Frigate 的 ffmpeg 输入端主动连接。本篇的模拟摄像头即用 ffmpeg 把测试视频推到 MediaMTX,MediaMTX 对 Frigate 扮演一台「提供 RTSP 服务的摄像头」。

二、镜像选型说明

四个服务均选用官方/上游镜像,版本与选型理由如下:

服务镜像与 tag选型理由
frigateghcr.io/blakeblackshear/frigate:0.18.2官方唯一发布渠道(ghcr.io),0.18.2 为 2026-10-15 发布的最新稳定版;官方另有 tensorrt/rk/rocm/synaptics 等 7 类变体 tag,仅在需要对应加速器时选用
mediamtxbluenviron/mediamtx:1.21.3-ffmpegMediaMTX 是 RTSP/RTMP/HLS/WebRTC 全协议流服务器;选 -ffmpeg 变体因为镜像内置 ffmpeg,同一镜像可同时充当「RTSP 服务器」与「模拟摄像头推流器」,省一个镜像
mosquittoeclipse-mosquitto:2.0MQTT 官方代理镜像的仓库名是 eclipse-mosquitto(老仓库 eclipse/mosquitto 已标记 DEPRECATED,实测 pull 新名才正确);2.0 tag 实测为 2.0.22
模拟摄像头bluenviron/mediamtx:1.21.3-ffmpeg(复用)覆盖 entrypoint 直接跑 ffmpeg 循环推流,模拟一台持续在线的摄像头;生产环境去掉该服务、换成真实摄像头 RTSP 地址

⚠️ 注意:镜像 tag 一律固定到具体版本号(如 0.18.2、1.21.3),不要用 latest,保证任何时候重建环境结果可复现。

三、前置条件

  1. 一台 Ubuntu 22.04 服务器,已安装 Docker Engine 与 Docker Compose v2(docker compose version 能正常输出版本)。
  2. 端口 8138/8139/8144 未被占用(ss -ltn 检查;8138-8152 是本文约定端口段,若被占用可自行替换为空闲端口)。
  3. 服务器可访问外网拉取镜像(实验环境 Docker 已配置镜像加速与代理;直连 ghcr.io 缓慢时可为 Docker daemon 配置代理,本文镜像实际拉取均成功)。
  4. 约 20GB 磁盘(frigate 镜像 8.7GB + 录像/快照数据预留)。

四、部署步骤

4.1 创建目录结构

所有配置与数据统一收敛到一个部署目录(下文以 /opt/frigate 为例),便于持久化与备份:

mkdir -p /opt/frigate/{config,mosquitto,media,source}
cd /opt/frigate
tree -L 1 .

实测回显:

.
├── config
├── media
├── mosquitto
└── source

4.2 准备模拟摄像头测试视频

下载 Intel 开源的检测测试样例视频( BSD-3 许可,包含行人、自行车、汽车三类目标,正好覆盖默认检测模型的主要类别)。该步骤已实测走代理下载成功(官方 GitHub 源当时可直连但缓慢,改走代理):

cd /opt/frigate/source
curl -x http://192.168.1.62:7897 -L -o person-bicycle-car-detection.mp4 \
  "https://github.com/intel-iot-devkit/sample-videos/raw/master/person-bicycle-car-detection.mp4"
ls -la person-bicycle-car-detection.mp4

实测回显:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 5889k  100 5889k    0     0  3455k      0  0:00:01  0:00:01 --:--:-- 8068k
-rw-r--r-- 1 root root 6031199 Oct 27 10:45 person-bicycle-car-detection.mp4

用 ffprobe 核对素材参数(mediamtx 镜像自带 ffprobe,宿主机未装 ffmpeg 时可借用):

docker run --rm -v /opt/frigate/source:/s:ro --entrypoint ffprobe \
  bluenviron/mediamtx:1.21.3-ffmpeg \
  -v error -show_entries format=duration:stream=codec_name,width,height,avg_frame_rate \
  -of default=noprint_wrappers=1 /s/person-bicycle-car-detection.mp4

实测回显:

codec_name=h264
width=768
height=432
avg_frame_rate=12/1
codec_name=aac
avg_frame_rate=0/0
duration=53.916667

⚠️ 网络说明:若 GitHub 直连可用,去掉 -x http://192.168.1.62:7897 参数即可;代理地址是实验环境实测可用渠道,读者应替换为自己的代理。

4.3 编写 Mosquitto 认证配置

Frigate 通过 MQTT 发布事件,Mosquitto 开启密码认证(保持与 Frigate 配置里的账号一致)。创建 mosquitto/mosquitto.conf:

listener 1883
allow_anonymous false
password_file /mosquitto/config/passwd
persistence false

逐项说明:listener 1883 监听容器内 1883 端口;allow_anonymous false 禁止匿名连接;password_file 指向密码文件;persistence false 测试环境不做消息持久化(生产建议开启并挂载持久化目录)。

生成密码文件(演示账号 admin / q1w2e3r4):

cd /opt/frigate
docker run --rm -v "$PWD/mosquitto:/mosquitto/config" \
  eclipse-mosquitto:2.0 \
  mosquitto_passwd -c -b /mosquitto/config/passwd admin q1w2e3r4
chmod 644 mosquitto/passwd
cat mosquitto/passwd

实测回显:

admin:$7$101$9go1Xt/3hpNewHpn$BHl+9O0KNn2pp9kD9tKJdUctM4F4PXXDAJ5jUyEGdyJSBzFRkfo7RwygTQ7Gpf6Cz+TdkKl7gofhtY5YAlAMTQ==

$7$ 开头表示 mosquitto 2.x 默认的 PBKDF2-SHA512 哈希,密码文件不存明文。

4.4 编写 Frigate 配置文件

创建 config/config.yml(0.18 配置 schema 版本 0.18-1):

go2rtc:
  streams:
    cam1:
      - rtsp://mediamtx:8554/cam1

mqtt:
  enabled: true
  host: mosquitto
  port: 1883
  user: admin
  password: q1w2e3r4
  topic_prefix: frigate

detectors:
  cpu0:
    type: cpu
    num_threads: 2

cameras:
  cam1:
    enabled: true
    ffmpeg:
      inputs:
        - path: rtsp://mediamtx:8554/cam1
          input_args: preset-rtsp-generic
          roles:
            - detect
            - record
            - restream
    detect:
      enabled: true
      width: 1280
      height: 720
      fps: 5
    objects:
      track:
        - person
        - bicycle
        - car
    record:
      enabled: true
      alerts:
        retain:
          days: 1
          mode: motion
      detections:
        retain:
          days: 1
          mode: motion
    snapshots:
      enabled: true
      retain:
        default: 7

version: 0.18-1

配置逐项说明:

  • go2rtc.streams.cam1:把模拟摄像头的流注册进 Frigate 内置的 go2rtc 流网关,用于浏览器低延迟直播与 RTSP 对外再分发。⚠️ 实测坑:0.18 中只给摄像头 input 加 restream 角色、不在顶层注册 go2rtc 流时,/api/go2rtc/streams 返回空对象 {},必须两者同时配置。
  • mqtt.*:MQTT 连接信息,host 写 Compose 网络内的服务名 mosquitto,账号密码与 4.3 生成的保持一致;topic_prefix 默认 frigate。
  • detectors.cpu0:CPU 检测器(无 GPU/加速器时的默认选择)。⚠️ 官方与运行日志均明确:CPU 检测器仅供测试试用,生产建议 Coral/OpenVINO/TensorRT。
  • cameras.cam1.ffmpeg.inputs:拉流地址 rtsp://mediamtx:8554/cam1(Compose 内部网络里 mediamtx 即流服务器主机名);input_args: preset-rtsp-generic 是官方内置的通用 RTSP 接入参数组;三个角色(roles)分别启用检测、录像、再分发。
  • detect:检测分辨率与帧率。这里 1280x720@5fps——检测帧率低于主码流帧率即可,CPU 算力消耗与该值正相关。
  • objects.track:要跟踪的目标类别(默认模型覆盖 COCO 90 类)。
  • record.alerts/detections.retain:告警与检测事件录像的保留策略。⚠️ 实测坑:旧版写法 record.retain.days/mode 在 0.18 已不允许(启动即报 Config Validation Errors: Extra inputs are not permitted),事件录像保留策略必须写在 alerts/detections 子节。
  • snapshots.retain.default:快照默认保留天数。
  • version: 0.18-1:配置 schema 版本标记,FrIgate 迁移器据此判断是否需要自动转换配置。

4.5 编写 docker-compose.yml

创建 /opt/frigate/docker-compose.yml(显式声明项目名 frigate-0.18.2-stack,避免与其他项目的同名服务互相干扰):

name: frigate-0.18.2-stack

networks:
  frigate-net:
    driver: bridge
    ipam:
      config:
        - subnet: 10.89.65.0/24

services:
  mosquitto:
    image: eclipse-mosquitto:2.0
    container_name: frigate-0.18.2-mosquitto
    restart: unless-stopped
    networks:
      frigate-net:
        aliases:
          - mosquitto
    volumes:
      - ./mosquitto:/mosquitto/config

  mediamtx:
    image: bluenviron/mediamtx:1.21.3-ffmpeg
    container_name: frigate-0.18.2-mediamtx
    restart: unless-stopped
    networks:
      frigate-net:
        aliases:
          - mediamtx

  camsource:
    image: bluenviron/mediamtx:1.21.3-ffmpeg
    container_name: frigate-0.18.2-camsource
    restart: unless-stopped
    entrypoint: ["ffmpeg"]
    command:
      - -re
      - -stream_loop
      - "-1"
      - -i
      - /source/person-bicycle-car-detection.mp4
      - -c:v
      - libx264
      - -preset
      - veryfast
      - -tune
      - zerolatency
      - -g
      - "25"
      - -b:v
      - 1500k
      - -maxrate
      - 1500k
      - -bufsize
      - 3000k
      - -pix_fmt
      - yuv420p
      - -f
      - rtsp
      - rtsp://mediamtx:8554/cam1
    volumes:
      - ./source:/source:ro
    depends_on:
      - mediamtx
    networks:
      frigate-net:

  frigate:
    image: ghcr.io/blakeblackshear/frigate:0.18.2
    container_name: frigate-0.18.2-app
    restart: unless-stopped
    shm_size: "256mb"
    stop_grace_period: 30s
    depends_on:
      - mosquitto
      - mediamtx
      - camsource
    environment:
      FRIGATE_RTSP_PASSWORD: ""
    volumes:
      - ./config:/config
      - ./media:/media/frigate
    ports:
      - "8138:5000"
      - "8139:8554"
      - "8144:8555"
    networks:
      frigate-net:
        aliases:
          - frigate

关键点解读:

  • camsource 就是模拟摄像头:entrypoint 覆盖为 ffmpeg,-re 按原速读文件、-stream_loop -1 无限循环,转码 1500k 码率后推到 rtsp://mediamtx:8554/cam1。等价的命令行形式(便于理解,实际部署用上面的 compose):
ffmpeg -re -stream_loop -1 -i /source/person-bicycle-car-detection.mp4 \
  -c:v libx264 -preset veryfast -tune zerolatency -g 25 \
  -b:v 1500k -maxrate 1500k -bufsize 3000k -pix_fmt yuv420p \
  -f rtsp rtsp://mediamtx:8554/cam1
  • shm_size: 256mb:Frigate 用共享内存放解码帧。⚠️ 实测坑:官方示例默认 128mb,本环境单路 1280x720@10fps 时启动日志直接告警「The current SHM size of 128.0MB is too small, recommend increasing it to at least 146MB」,改为 256mb 后告警消失。
  • 网络:固定子网 10.89.65.0/24(自定义网段避免与其他 Docker 网段冲突;读者环境如冲突可换空闲网段)。frigate/mediamtx/mosquitto 三服务通过别名互相解析。
  • 端口映射:8138(Web UI)、8139(对外 RTSP 再分发)、8144(WebRTC UDP/TCP)。MediaMTX 与 Mosquitto 只在内部网络使用,不映射到宿主机。
  • 数据持久化:./config(配置 + SQLite 数据库 + 模型缓存)与 ./media(录像/快照)都落在部署目录。

4.6 启动与状态确认

cd /opt/frigate
docker compose up -d
docker compose ps

实测回显:

 Container frigate-0.18.2-mosquitto Started
 Container frigate-0.18.2-mediamtx Started
 Container frigate-0.18.2-camsource Started
 Container frigate-0.18.2-app Started
NAME                       STATUS
frigate-0.18.2-app         Up 30 seconds (healthy)
frigate-0.18.2-camsource   Up About a minute
frigate-0.18.2-mediamtx    Up About a minute
frigate-0.18.2-mosquitto   Up About a minute

四个服务全部 Up,frigate 自带健康检查显示 healthy。查看 MediaMTX 日志确认模拟摄像头的流已就绪:

docker logs frigate-0.18.2-mediamtx 2>&1 | tail -6

实测回显:

INF [RTSP] started with listeners on :8554 (TCP/RTSP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
INF [RTMP] started with listener on :1935 (TCP/RTMP)
INF [HLS] started with listener on :8888 (TCP/HTTP)
INF [WebRTC] started with listeners on :8889 (TCP/HTTP), :8189 (UDP/ICE)
INF [RTSP] [conn 10.89.65.4:46582] opened
INF [path cam1] stream is available and online, 2 tracks (H264, MPEG-4 Audio)

path cam1 ... online, 2 tracks (H264, MPEG-4 Audio) 表示 cam1 路径已有 H.264 视频 + AAC 音频两路轨道在线。

查看 Frigate 日志确认配置校验与检测流水线启动:

docker logs frigate-0.18.2-app 2>&1 | grep -E 'Starting Frigate|Camera processor|Capture process|ERROR' | head -5

实测回显:

frigate.app                    INFO    : Starting Frigate (0.18.2-cfa08c5)
frigate.camera.maintainer      INFO    : Camera processor started for cam1: 697
frigate.camera.maintainer      INFO    : Capture process started for cam1: 762

无 ERROR、cam1 的采集与处理进程都已启动。

4.7 检测与事件验证

等待约 1 分钟(视频里行人反复入画),用 REST API 验证检测统计:

curl -s http://127.0.0.1:8138/api/stats | python3 -c "
import json,sys
d=json.load(sys.stdin)
print('version:', d['service']['version'])
c=d['cameras']['cam1']
print('camera_fps:', c['camera_fps'], '| process_fps:', c['process_fps'], '| detection_fps:', c['detection_fps'])
"

实测回显:

version: 0.18.2-cfa08c5
camera_fps: 5.3 | process_fps: 4.6 | detection_fps: 17.7

camera_fps 约 5.3 说明摄像头帧率采集正常,detection_fps 17.7 表示 CPU 检测器吞吐正常。查看已产生的事件:

curl -s "http://127.0.0.1:8138/api/events?limit=5" | python3 -c "
import json,sys
for e in json.load(sys.stdin):
    print(e['id'], e['camera'], e['label'], 'score=%.2f' % e['data']['score'], e['start_time'] and 'ended' or 'active')
"

实测回显(节选):

1793070035.895638-kynwky cam1 person score=0.82 ended 1793070046.214243
1793070020.637265-nruz0s cam1 person score=0.82 ended 1793070030.318983
1793069981.139479-3r8icb cam1 person score=0.81 ended 1793069992.814086
1793069966.374918-kuycz7 cam1 person score=0.82 ended 1793069976.626176
1793069953.874141-9lctzs cam1 bicycle score=0.75 ended 1793069955.801494

行人(person,置信度 0.81-0.82)与自行车(bicycle,0.75)事件均已产生。快照与录像验证:

curl -s -o /dev/null -w 'latest.jpg  HTTP %{http_code} %{content_type} %{size_download}B\n' \
  http://127.0.0.1:8138/api/cam1/latest.jpg
curl -s "http://127.0.0.1:8138/api/cam1/recordings/summary" | python3 -m json.tool | head -12
ls /opt/frigate/media/recordings/2026-10-27/
du -sh /opt/frigate/media/recordings

实测回显:

latest.jpg  HTTP 200 image/jpeg 32604B
[
    {
        "events": 6,
        "hours": [
            {
                "hour": "03",
                "events": 2,
                "motion": 4614,
                "objects": 39,
                "duration": 58
            }
        ],
        "day": "2026-10-27"
    }
]
2026-10-27:
02
03
22M	/opt/data/frigate/media/recordings

latest.jpg 返回 200 与 32604 字节 JPEG;录像按「日期/小时/摄像头」三级目录落盘(02、03 两个时段),总计 22MB。

4.8 MQTT 事件验证

在 Mosquitto 容器内订阅 frigate/# 全部主题观察 45 秒(模拟源持续有人入画,必出事件):

docker exec frigate-0.18.2-mosquitto \
  timeout 45 mosquitto_sub -h localhost -u admin -P q1w2e3r4 -t 'frigate/#' -v | head -16

实测回显(节选,person/snapshot 主题载荷为 JPEG 二进制故显示乱码属正常):

frigate/cam1/enabled/state ON
frigate/cam1/recordings/state ON
frigate/cam1/snapshots/state ON
frigate/cam1/detect/state ON
frigate/cam1/motion/state ON
frigate/cam1/birdseye/state ON
frigate/cam1/motion_threshold/state 30
frigate/cam1/person/snapshot JFIF...(JPEG 二进制)

状态主题(enabled/recordings/snapshots/detect/motion)与目标快照主题(person/snapshot)都在正常发布。

4.9 Web UI 登录

浏览器访问 http://<服务器IP>:8138。Frigate 0.18 内置认证(auth.enabled 默认开启),管理用户默认为 admin。⚠️ 实测坑:Frigate 的 UI 密码策略要求至少 12 个字符,8 位演示密码 q1w2e3r4 会被拒绝(POST /api/users 时实测返回 {"message":"Password must be at least 12 characters long"});本篇 UI 演示账号定为 admin / q1w2e3r4Ab12(12 位),MQTT 的 admin/q1w2e3r4 不受该策略限制(Mosquitto 无此要求)。

实测登录接口验证:

curl -s -i -X POST http://127.0.0.1:8138/api/login \
  -H 'Content-Type: application/json' \
  -d '{"user":"admin","password":"q1w2e3r4Ab12"}' | head -1

实测回显:

HTTP/1.1 200 OK

⚠️ 认证边界说明(实测结论):Frigate 0.18 的 REST API(/api/* 路径)默认匿名可访问;强制走登录态的是媒体流与文件路径(/stream/、/clips/、/recordings/、/ws 等由内置 nginx 挂 auth_request 拦截)。生产环境如需 API 鉴权,应在前置反代或防火墙层面限制 /api/* 暴露面。

五、数据持久化说明

挂载宿主目录(本文)容器内路径内容
配置/opt/frigate/config/configconfig.yml、go2rtc 配置、frigate.db(事件元数据 SQLite)、model_cache
媒体/opt/frigate/media/media/frigateclips/(快照)、recordings/(录像)、exports/
Mosquitto 配置/opt/frigate/mosquitto/mosquitto/configmosquitto.conf、passwd

录像目录结构为 media/recordings/<日期>/<小时>/<摄像头>/<分片>.mp4(实测示例 recordings/2026-10-27/02/cam1/59.01.mp4),按小时分目录、每分钟一个分片文件。删除某段录像直接删对应目录文件即可,Frigate 维护进程会自动同步索引。

验证持久化:重启整个栈后事件与录像仍在——

cd /opt/frigate
docker compose down && docker compose up -d
sleep 60
curl -s "http://127.0.0.1:8138/api/events/summary" | head -c 200; echo

实测回显(down/up 后事件元数据完好):

[{"camera": "cam1", "label": "bicycle", "sub_label": null, "data": {"box": [...

六、环境变量表

Frigate 官方镜像常用环境变量(完整清单见官方文档 environment-vars 页):

变量默认值说明本篇取值
FRIGATE_RTSP_PASSWORD无为所有形如 {FRIGATE_RTSP_PASSWORD} 占位的 RTSP 地址统一注入密码""(模拟源无密码)
CONFIG_FILE/config/config.yml配置文件路径默认
FRIGATE_MQTT_USERNAME / FRIGATE_MQTT_PASSWORD无MQTT 凭据注入(避免写在 config.yml)未用(演示环境直接写在配置)
TZ无时区(影响界面时间显示)未设(读宿主时区)

💡 技术拓展:敏感凭据注入有三种常见做法——写在 config.yml(简单但配置文件需加密保管)、环境变量注入(compose environment 段)、Docker secrets(swarm/k8s 场景)。生产建议环境变量或 secrets,演示环境写配置文件可读性最好。本篇为演示环境,MQTT 凭据直接落 config.yml。

七、配置参数说明与配置文件挂载

部署相关的核心配置项汇总(完整配置参数的逐项修改-验证闭环见《Frigate 0.18.2 配置参数详解》):

配置项作用本篇实测验证方式
go2rtc.streams注册对外再分发的流GET /api/go2rtc/streams 返回 cam1 producers
mqtt.host/user/password/topic_prefixMQTT 连接与主题前缀mosquitto_sub 订阅 frigate/# 有状态与事件消息
detectors.cpu0.num_threadsCPU 检测线程数/api/stats 的 detection_fps 吞吐变化
cameras.<name>.ffmpeg.inputs拉流地址与角色MediaMTX 日志 online + camera_fps 正常
cameras.<name>.detect检测分辨率/帧率fps 改 10 后 camera_fps 5.3 → 10.1
record.alerts/detections.retain事件录像保留recordings 目录按天/小时生成
snapshots.retain快照保留media/clips/thumbs 生成缩略图

配置文件挂载即 4.5 的 ./config:/config:修改 config/config.yml 后执行 docker compose restart frigate 生效(涉及录像目录等深层次变更时用 docker compose up -d frigate 重建容器)。0.18 的 Web UI 内置配置编辑器,保存后同样触发重启加载。

八、等价 docker run

不用 Compose 时的等价单命令(便于理解各服务参数;生产推荐 Compose):

# MQTT 代理
docker run -d --name frigate-0.18.2-mosquitto \
  -v /opt/frigate/mosquitto:/mosquitto/config \
  eclipse-mosquitto:2.0

# RTSP 流服务器(模拟摄像头服务端)
docker run -d --name frigate-0.18.2-mediamtx \
  bluenviron/mediamtx:1.21.3-ffmpeg

# 模拟摄像头推流
docker run -d --name frigate-0.18.2-camsource \
  --entrypoint ffmpeg \
  -v /opt/frigate/source:/source:ro \
  --network container:frigate-0.18.2-mediamtx \
  bluenviron/mediamtx:1.21.3-ffmpeg \
  -re -stream_loop -1 -i /source/person-bicycle-car-detection.mp4 \
  -c:v libx264 -preset veryfast -tune zerolatency -g 25 \
  -b:v 1500k -pix_fmt yuv420p -f rtsp rtsp://127.0.0.1:8554/cam1

# Frigate 主服务
docker run -d --name frigate-0.18.2-app \
  --shm-size 256mb \
  -p 8138:5000 -p 8139:8554 -p 8144:8555 \
  -v /opt/frigate/config:/config \
  -v /opt/frigate/media:/media/frigate \
  ghcr.io/blakeblackshear/frigate:0.18.2

⚠️ 等价命令未包含自定义网络与别名,四个独立容器间需用 IP 或 --link 方式互通,因此生产环境仍以 Compose(自带内部 DNS)为准。

九、常用运维命令

cd /opt/frigate
docker compose ps                 # 四服务状态(frigate 应显示 healthy)
docker compose logs -f frigate    # 跟踪 Frigate 日志
docker compose restart frigate    # 修改 config.yml 后重启生效
docker compose stop camsource     # 停掉模拟摄像头(Frigate 会显示流断开)
docker compose start camsource    # 恢复推流
docker compose down               # 停止并移除栈(数据在挂载目录不受影响)
docker compose up -d              # 重新拉起

升级到新版本:修改 docker-compose.yml 中 frigate 镜像 tag 为目标版本(如 0.18.3),执行:

docker compose pull frigate && docker compose up -d frigate

Frigate 会在启动时自动做配置 schema 迁移(实测 0.18.2 首启日志:Checking if frigate config needs migration... copying config as backup...,旧配置自动备份为 backup_config.yaml)。

十、常见问题

Q1:启动报 Bind for 0.0.0.0:8138 failed: port is already allocated? 端口被其他进程/其他 compose 项目占用。ss -ltnp | grep 8138 找到占用者,或换用空闲端口(本文约定 8128-8152 段内取空闲者)。实测本环境 8142 被占用后改用 8138 解决。

Q2:Frigate 启动即打印 Your config file is not valid! 与 Extra inputs are not permitted? 0.18 配置 schema 与旧版不兼容,最常见两处:①record.retain 下的 days/mode 需迁移到 record.alerts.retain / record.detections.retain;②顶层多了 0.18 不认识的键。错误日志会精确到行号(实测示例:Line #: 36, Key: cameras -> cam1 -> record -> retain),按行号修正即可。

Q3:日志告警 The current SHM size of 128.0MB is too small? 共享内存不足,compose 中把 shm_size 从 128mb 提到 256mb(实测告警消失)。多路高分辨率时按官方公式继续加大。

Q4:/api/go2rtc/streams 返回空 {},Web UI 里 cam1 没有直播画面? 只配置了 input 的 restream 角色不够,必须在顶层 go2rtc.streams 显式注册流(见 4.4 配置说明)。实测两者同时配置后返回 {"cam1": {"producers": [{"url": "rtsp://mediamtx:8554/cam1"}]}}。

Q5:拉 eclipse-mosquitto 镜像报错或拉到老版本? Mosquitto 官方镜像仓库名是 eclipse-mosquitto(Docker Hub 单段名,2026-10 实测 2.0 tag 内为 2.0.22)。旧仓库 eclipse/mosquitto 已 DEPRECATED(其 tags 页搜索结果明确标注),勿再使用。

Q6:mqtt 配置写错时事件表现是什么? Frigate 与 MQTT 断连时不会报进程错误,但 MQTT 主题全部无消息、Home Assistant 集成不可用;frigate/# 订阅不到 frigate/cam1/enabled/state ON 等状态消息即为 MQTT 未连上。先确认 mosquitto 容器 Up、账号密码与 passwd 文件一致、host 写的是服务名。

Q7:想接入真实摄像头? 把 cameras.cam1.ffmpeg.inputs.path 与 go2rtc.streams 里的 rtsp://mediamtx:8554/cam1 换成摄像头的 RTSP 地址(如 rtsp://用户名:密码@摄像头IP:554/stream1),并删除 camsource 服务即可。主流品牌 RTSP 路径可查 Frigate 官方文档的 camera-setup 页。

十一、参考链接