优化 docker 构建方法

This commit is contained in:
linyqh 2024-11-10 21:25:44 +08:00
parent 84e48b5991
commit e7026941bc
4 changed files with 71 additions and 51 deletions

View File

@ -1,39 +1,63 @@
FROM python:3.10-slim-bullseye
# 构建阶段
FROM python:3.10-slim-bullseye as builder
# Set the working directory in the container
WORKDIR /NarratoAI
# 设置工作目录
WORKDIR /build
# 设置/NarratoAI目录权限为777
RUN chmod 777 /NarratoAI
ENV PYTHONPATH="/NarratoAI"
# Install system dependencies
# 安装构建依赖
RUN apt-get update && apt-get install -y \
git \
git-lfs \
&& rm -rf /var/lib/apt/lists/*
# 创建虚拟环境
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# 首先安装 PyTorch因为它是最大的依赖
RUN pip install --no-cache-dir torch torchvision torchaudio
# 然后安装其他依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 运行阶段
FROM python:3.10-slim-bullseye
# 设置工作目录
WORKDIR /NarratoAI
# 从builder阶段复制虚拟环境
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# 安装运行时依赖
RUN apt-get update && apt-get install -y \
imagemagick \
ffmpeg \
wget \
&& rm -rf /var/lib/apt/lists/*
git-lfs \
&& rm -rf /var/lib/apt/lists/* \
&& sed -i '/<policy domain="path" rights="none" pattern="@\*"/d' /etc/ImageMagick-6/policy.xml
# Fix security policy for ImageMagick
RUN sed -i '/<policy domain="path" rights="none" pattern="@\*"/d' /etc/ImageMagick-6/policy.xml
# 设置环境变量
ENV PYTHONPATH="/NarratoAI" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
# Copy only the requirements.txt first to leverage Docker cache
COPY requirements.txt ./
# 设置目录权限
RUN chmod 777 /NarratoAI
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Now copy the rest of the codebase into the image
COPY . .
# 安装 git lfs 并下载模型到指定目录
# 安装git lfs
RUN git lfs install
# Expose the port the app runs on
EXPOSE 8501
# 复制应用代码
COPY . .
# Command to run the application
CMD ["streamlit", "run", "webui.py","--browser.serverAddress=127.0.0.1","--server.enableCORS=True","--browser.gatherUsageStats=False"]
# 暴露端口
EXPOSE 8501 8080
# 使用脚本作为入口点
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]

View File

@ -1,5 +1,5 @@
[app]
project_version="0.3.4"
project_version="0.3.5"
# 支持视频理解的大模型提供商
# gemini
# NarratoAPI

View File

@ -1,30 +1,18 @@
x-common-volumes: &common-volumes
- ./:/NarratoAI
x-common: &common
build:
context: .
dockerfile: Dockerfile
image: linyq1/narratoai:latest
volumes:
- ./:/NarratoAI
environment:
- VPN_PROXY_URL=http://host.docker.internal:7890
restart: always
services:
webui:
build:
context: .
dockerfile: Dockerfile
image: linyq1/narratoai:latest
container_name: "webui"
<<: *common
container_name: webui
ports:
- "8501:8501"
command: [ "bash", "webui.sh" ]
volumes: *common-volumes
environment:
- "VPN_PROXY_URL=http://host.docker.internal:7890"
restart: always
api:
build:
context: .
dockerfile: Dockerfile
image: linyq1/narratoai:latest
container_name: "api"
ports:
- "8502:8080"
command: [ "python3", "main.py" ]
volumes: *common-volumes
environment:
- "VPN_PROXY_URL=http://host.docker.internal:7890"
restart: always
command: ["webui"]

8
docker-entrypoint.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/bash
set -e
if [ "$1" = "webui" ]; then
exec streamlit run webui.py --browser.serverAddress=127.0.0.1 --server.enableCORS=True --browser.gatherUsageStats=False
else
exec "$@"
fi