Azure虚拟机容器组中Streamlit实例无法通过暴露端口访问
解决Azure VM上Streamlit容器公网无法访问但本地容器组正常的问题
核心问题原因
- Streamlit默认仅绑定容器内的
127.0.0.1回环地址,即便通过ports: 81:8501配置了端口映射,外部请求也无法穿透到容器内的服务。 - 启用
network_mode: host后,容器脱离了Docker Compose默认的自定义网桥,导致无法通过服务名/hostname与组内其他容器通信。
解决方案:修改Streamlit监听地址,保留默认Docker网络
只需调整Streamlit的启动参数,让其监听容器内的所有网卡(0.0.0.0),这样既可以通过VM公网IP+映射端口访问,又能保持容器间的网络互通。
方法1:修改Dockerfile
在Streamlit启动命令中添加--server.address 0.0.0.0参数:
WORKDIR /streamlit COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY /pages ./pages COPY main_page.py main_page.py COPY get_text.py get_text.py EXPOSE 8501 CMD ["streamlit", "run", "main_page.py", "--server.address", "0.0.0.0"]
修改后重新构建镜像并部署容器组。
方法2:修改docker-compose.yml(无需重构镜像)
在streamlit_container服务中添加command字段覆盖启动命令:
services: container1: container_name: container1 hostname: name1 image: image1 environment: - RABBITMQ_DEFAULT_PASS=*** - RABBITMQ_DEFAULT_USER=*** - RABBITMQ_DEFAULT_VHOST=/ container2: container_name: container2 hostname: name2 image: image2 streamlit_container: container_name: streamlit_container hostname: streamlit_name image: streamlit_image ports: - 81:8501 command: ["streamlit", "run", "main_page.py", "--server.address", "0.0.0.0"] container3: container_name: container3 hostname: name3 image: image3 environment: - ATS_USE_GPU=1 - SA_USE_GPU=1 volumes: - /static/models:/summariser/ats_sa/models:ro runtime: nvidia
验证步骤
- 重启容器组:
docker-compose down && docker-compose up -d - 通过VM公网IP访问
http://<VM公网IP>:81,确认Streamlit页面加载正常 - 测试容器间交互(比如Streamlit调用container1的RabbitMQ服务),验证功能正常
内容的提问来源于stack exchange,提问作者mrhoody
相关产品推荐
相关产品推荐

