modify: add docker

This commit is contained in:
2025-11-02 22:30:59 +08:00
parent d8eea8e3a9
commit d94b5c5ba4
4 changed files with 81 additions and 2 deletions

36
Dockerfile Normal file
View File

@@ -0,0 +1,36 @@
# Use an official Python runtime as a parent image
FROM python:3.12-slim
# Prevents writing .pyc files and buffering stdout/stderr
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set working directory
WORKDIR /app
# Copy requirements first to leverage Docker cache
COPY requirements.txt .
# Install system dependencies (if needed) and Python deps
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Copy app code
COPY . .
# Expose Streamlit default port
EXPOSE 8501
# Streamlit config: run headless and bind to 0.0.0.0
ENV STREAMLIT_SERVER_HEADLESS=true
ENV STREAMLIT_SERVER_ENABLE_CORS=false
ENV STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION=false
ENV STREAMLIT_SERVER_PORT=8501
ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0
# Run Streamlit
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

View File

@@ -68,6 +68,25 @@ pip install streamlit-autorefresh openpyxl xlrd cryptography openai
3. Open `http://localhost:8501` in your browser. The home page should load without import errors. 3. Open `http://localhost:8501` in your browser. The home page should load without import errors.
## 5. Run with Docker (optional)
If you prefer an isolated container build, use the included `Dockerfile`:
```bash
docker build -t trafficsafeanalyzer .
docker run --rm -p 8501:8501 trafficsafeanalyzer
```
To work with local data, mount the host folder containing Excel files:
```bash
docker run --rm -p 8501:8501 \
-v "$(pwd)/sample:/app/sample" \
trafficsafeanalyzer
```
The container exposes Streamlit on port 8501 by default. Override configuration via environment variables when needed, for example `-e STREAMLIT_SERVER_PORT=8502`.
## Troubleshooting tips ## Troubleshooting tips
- **Missing package**: Re-run `pip install -r requirements.txt`. - **Missing package**: Re-run `pip install -r requirements.txt`.

View File

@@ -4,7 +4,7 @@ TrafficSafeAnalyzer delivers accident analytics and decision support through a S
## Start the app ## Start the app
1. Activate your virtual or conda environment. 1. Activate your virtual or conda environment(或在容器中运行,见下).
2. From the project root, run: 2. From the project root, run:
```bash ```bash
@@ -13,6 +13,8 @@ TrafficSafeAnalyzer delivers accident analytics and decision support through a S
3. Open `http://localhost:8501`. Keep the terminal running while you work in the browser. 3. Open `http://localhost:8501`. Keep the terminal running while you work in the browser.
> 使用 Docker运行 `docker build -t trafficsafeanalyzer .` 与 `docker run --rm -p 8501:8501 trafficsafeanalyzer` 后,同样访问 `http://localhost:8501`。
## Load input data ## Load input data
Use the sidebar form labelled “数据与筛选”. Use the sidebar form labelled “数据与筛选”.

View File

@@ -19,7 +19,7 @@
- Git - Git
- 可选Docker用于容器化部署 - 可选Docker用于容器化部署
### 安装 ### 安装(本地环境)
1. 克隆仓库: 1. 克隆仓库:
@@ -63,6 +63,28 @@ streamlit run app.py
streamlit run app.py streamlit run app.py
``` ```
### 使用 Docker 运行
项目根目录已经包含 `Dockerfile`,无需额外配置即可容器化运行:
```bash
# 构建镜像
docker build -t trafficsafeanalyzer .
# 以临时容器方式启动
docker run --rm -p 8501:8501 trafficsafeanalyzer
```
运行后访问 `http://localhost:8501` 即可。若需加载主机上的数据文件,可通过挂载方式注入:
```bash
docker run --rm -p 8501:8501 \
-v "$(pwd)/sample:/app/sample" \
trafficsafeanalyzer
```
容器内默认启用了示例 AI 凭据与 Streamlit Headless 模式,如需调整可在 `docker run` 时追加环境变量(例如 `-e STREAMLIT_SERVER_PORT=8502`)。
## 依赖项 ## 依赖项
列于 `requirements.txt` 列于 `requirements.txt`