如何从Docker容器访问Streamlit应用?

fruv7luv  于 8个月前  发布在  Docker
关注(0)|答案(1)|浏览(136)

我在一个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/,我会得到以下内容。
x1c 0d1x的数据
当我在本地运行我的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

4nkexdtk

4nkexdtk1#

问题是您的应用绑定到容器IP(172.17.0.2),当您尝试访问localhost时,它会解析为127.0.0.1,但您的应用不会侦听该地址。
要使应用程序在容器内工作,它需要绑定到所有地址(0.0.0.0)。不确定您的应用程序如何工作,但看看是否有一个选项可以将绑定地址更改为0.0.0.0-这应该可以做到。

相关问题