构建base镜像,只有当依赖发生变化时再进行重新构建,否则只使用之前的base镜像进行构建,能够使本地使用缓存的镜像

This commit is contained in:
Chingfeng Li
2025-10-20 15:07:02 +08:00
parent e5e9f2bdea
commit e4bf5d2d21
3 changed files with 76 additions and 32 deletions
+50
View File
@@ -0,0 +1,50 @@
name: Build Base Image
on:
push:
branches:
- main
- master
paths:
- 'main/xiaozhi-server/requirements.txt'
- 'Dockerfile-server-base'
- '.github/workflows/build-base-image.yml'
jobs:
build-base:
name: Build and push server base image
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.TOKEN }}
- name: Build and push server-base
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile-server-base
push: true
tags: ghcr.io/${{ github.repository }}:server-base
platforms: linux/amd64
cache-from: type=gha,scope=server-base
cache-to: type=gha,mode=max,scope=server-base
build-args: |
BUILDKIT_PROGRESS=plain
- name: Output image info
run: |
echo "✅ Base image built and pushed successfully!"
echo "📦 Tag: ghcr.io/${{ github.repository }}:server-base"
+2 -32
View File
@@ -1,36 +1,6 @@
# 第一阶段:构建Python依赖
FROM python:3.10-slim AS builder
# 生产镜像,仅包含应用代码
FROM ghcr.io/xinnan-tech/xiaozhi-esp32-server:server-base
WORKDIR /app
# 配置pip使用国内镜像源(阿里云)并设置超时和重试
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
pip config set global.trusted-host mirrors.aliyun.com && \
pip config set global.timeout 120 && \
pip config set install.retries 5
COPY main/xiaozhi-server/requirements.txt .
# 安装Python依赖,使用并行下载
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
pip install --no-cache-dir -r requirements.txt --default-timeout=120 --retries 5
# 第二阶段:生产镜像
FROM python:3.10-slim
WORKDIR /opt/xiaozhi-esp32-server
# 安装系统依赖
RUN apt-get update && \
apt-get install -y --no-install-recommends libopus0 ffmpeg && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# 从构建阶段复制Python包和前端构建产物
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --from=builder /usr/local/bin/mcp-proxy /usr/local/bin/mcp-proxy
# 复制应用代码
COPY main/xiaozhi-server .
# 启动应用
+24
View File
@@ -0,0 +1,24 @@
# Dockerfile-server-base
# 基础镜像,包含系统依赖和Python包
FROM python:3.10-slim
# 安装系统依赖
RUN apt-get update && \
apt-get install -y --no-install-recommends libopus0 ffmpeg && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# 配置pip使用国内镜像源(阿里云)并设置超时和重试
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
pip config set global.trusted-host mirrors.aliyun.com && \
pip config set global.timeout 120 && \
pip config set install.retries 5
WORKDIR /opt/xiaozhi-esp32-server
# 复制requirements.txt
COPY main/xiaozhi-server/requirements.txt .
# 安装Python依赖
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
pip install --no-cache-dir -r requirements.txt --default-timeout=120 --retries 5