Skip to content

Commit

Permalink
241229 增加docker部署方式
Browse files Browse the repository at this point in the history
  • Loading branch information
AFAN-LIFE committed Dec 28, 2024
1 parent 10190c5 commit 3929220
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
*.pyc
*.pyo
.env
.git/
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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教学:
Expand Down
12 changes: 12 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions sources.list
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3929220

Please sign in to comment.