Scott's Blog

学则不固, 知则不惑

0%

Docker 部署 Streamlit 应用

使用 Docker 部署 Streamlit 并设置 Oracle 数据库连接。

Streamlit 的部署其实很简单,只需要拉取 Python 基础包,装好你需要的包即可。

我的 dockerfile 中,因为还需要让 strealit 连接 oracle, 所以有 set up Oracle 的步骤。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
FROM python:3.8.13
LABEL MAINTAINER="scottzhang.pro"
COPY . /app/streamlit_app

# Set up Oracle
WORKDIR /opt/oracle
RUN apt-get update && apt-get install -y libaio1 wget unzip \
&& cp /app/streamlit_app/instantclient/oracle_configs/instantclient-basic-linux.x64-12.2.0.1.0.zip . \
&& unzip instantclient-basic-linux.x64-12.2.0.1.0.zip \
&& rm -f instantclient-basic-linux.x64-12.2.0.1.0.zip \
&& cd /opt/oracle/instantclient* \
&& rm -f *jdbc* *occi* *mysql* *README *jar uidrvci genezi adrci \
&& echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-instantclient.conf \
&& ldconfig \
&& mkdir -p /opt/oracle/instantclient_12_2/network/admin \
&& cp /app/streamlit_app/instantclient/oracle_configs/tnsnames.ora /opt/oracle/instantclient_12_2/network/admin/ \
&& cp /app/streamlit_app/instantclient/oracle_configs/sqlnet.ora /opt/oracle/instantclient_12_2/network/admin/

# Set work dir
WORKDIR "/app/streamlit_app"
# Install python packages
# RUN pip install -r /app/streamlit_app/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
# query_helper and fc_auto is a package I developed, that is used as a Git submodule
# when i pull all code to local (include this docker file), these submodules also will download to local
RUN pip install -r /app/streamlit_app/requirements.txt \
&& pip install -e query_helper \
&& pip install -e fc_auto

# Expose port
EXPOSE 80
CMD ["streamlit", "run", "main.py"]

最后将 streamlit 暴露在 80 端口,就可以访问了。

Enjoy!