diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2945661 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +__pycache__/ +*.pyc +*.pyo +.env +.git/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f441070 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM ubuntu:jammy +ARG DEBIAN_FRONTEND=noninteractive +ENV TZ=Asia/Shanghai +CMD ["/bin/bash"] + +RUN apt update && apt install ca-certificates -y +COPY sources.list /etc/apt/sources.list +# 安装必要的工具 +RUN apt-get update && apt-get install -y \ + wget \ + sudo \ + iptables \ + ufw + +# 安装conda环境到/opt/miniconda +RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda3_latest.sh && \ + chmod +x miniconda3_latest.sh && \ + ./miniconda3_latest.sh -b -p /opt/miniconda && \ + echo "export PATH=/opt/miniconda/bin:\$PATH" >> ~/.bashrc + +# 配置 Miniconda 的环境变量 +ENV PATH="/opt/miniconda/bin:${PATH}" + +# 复制依赖文件并安装 Python 包 +COPY requirements.txt /root/ +RUN /opt/miniconda/bin/pip install -r /root/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \ No newline at end of file diff --git a/README.md b/README.md index 2da2a33..f97347b 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,50 @@ streamlit run main.py 默认在`http://localhost:8501/`启动 +## docker部署使用 + +### 镜像获取 + +网络条件允许可以直接docker下拉镜像: + +```shell +docker pull afanlife/macropage:v1.1 +``` + +网络条件受限,可以切换到本工程目录后,根据Dockerfile进行build: + +```shell +docker build -t macropage:v1.1 . +``` + +### 容器启动 + +#### 方式1:基于compose启动(推荐) + +基于本地已经编写好的compose.yaml直接启动 + +```shell +docker compose up -d # 启动并创建所有容器 +docker compose down # 停止并清除所有容器 +``` + + +#### 方式2:手动启动容器 + +可以切换到本工程目录后,执行以下代码进行项目启动,启动成功即可在`http://localhost:8501/` 查看本地部署实例 + +```shell +docker run -d -p 8501:8501 -v .:/opt/code --name macropage-app macropage:v1.1 bash -c "cd /opt/code && streamlit run main.py" +``` + +#### 相关辅助指令 + +```shell +docker logs macropage-app # 启动失败可以查看容器内部的运行日志 +docker exec -it macropage-app /bin/bash # 启动后可以进入容器后查看 +exit # 容器内退出 +``` + ## 深入学习项目 - 扫码加入知识星球:AFAN的金融科技,收看专栏中的宏观经济数据往期分析,以及直播回放中的streamlit教学: diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..d381b1b --- /dev/null +++ b/compose.yaml @@ -0,0 +1,12 @@ +services: + macropage: + image: macropage:v1.1 + container_name: macropage-app + # network_mode: host # 不要使用host模式,会出现异常,更推荐使用bridge模式端口映射 + volumes: + - .:/opt/code # 设置代码挂载目录 + restart: always + ports: + - "8501:8501" # 确保映射了端口 + working_dir: /opt/code # 设置容器的工作目录 + command: ["/opt/miniconda/bin/streamlit", "run", "main.py"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f9596d3..a22e40f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,5 @@ pandas==2.1.1 cryptography==41.0.4 openpyxl==3.1.2 streamlit-echarts==0.4.0 -altair==5.2.0 +altair=docker=5.2.0 tushare \ No newline at end of file diff --git a/sources.list b/sources.list new file mode 100644 index 0000000..c0a2765 --- /dev/null +++ b/sources.list @@ -0,0 +1,6 @@ +# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse +# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换 +deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse \ No newline at end of file