我正在 Docker 容器中运行基本的 Streamlit 聊天机器人。运行 docker 容器后,我在终端中看到以下内容。
Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.
You can now view your Streamlit app in your browser.
Network URL: http://172.17.0.2:8501
External URL: http://49.36.212.205:8501
现在,如果我尝试打开 http:/localhost:8051/ 我会得到以下信息。
当我在本地运行 Streamlit 应用程序(没有 Docker)时,不会出现此问题。
遵循我的 Dockerfile 的内容。
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.9-slim
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
EXPOSE 8501
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser
WORKDIR /home/appuser/app
COPY main.py /home/appuser/app
RUN chown -R appuser /home/appuser/app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
ENTRYPOINT [ "streamlit", "run" ]
CMD ["main.py"]
以下是我用来构建映像和运行容器的命令。
docker build -t test_streamlit . -f Dockerfile
docker run -p 8051:8051 --name test-1 test_streamlit
问题是您的应用程序绑定到容器 IP (
172.17.0.2
),当您尝试访问 localhost
时,它会解析为 127.0.0.1
,但您的应用程序未侦听该地址。
要使应用程序在容器内运行,它需要绑定到所有地址 (
0.0.0.0
)。不确定您的应用程序如何工作,但看看是否有一个选项可以将绑定地址更改为 0.0.0.0
- 这应该可以解决问题。
您可以使用此命令设置服务器地址和端口号:
streamlit run app.py --server.address 0.0.0.0 --server.port 8051
。
它对我有用。