You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP虚拟机Docker部署网站首页正常,其他页面无法访问求助

问题描述

在Google Cloud Platform(GCP)虚拟机中部署基于Docker容器的网站,首页可正常访问,但访问其他页面(例如:http://34.130.236.145:25556/metainfo/1)时出现连接错误。

错误信息

情况1:后端地址设为GCP公网IP时

requests.exceptions.ConnectionError: HTTPConnectionPool(host=34.130.236.145, 
port=25555): Max retries exceeded with url: /metainfo/1 (Caused by 
NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7cee6c119100>: 
Failed to establish a new connection: [Errno 111] Connection refused'))

情况2:后端地址设为backend时

requests.exceptions.ConnectionError: HTTPConnectionPool(host='backend', port=25555): 
Max retries exceeded with url: /bitmap/1 (Caused by NameResolutionError(" 
<urllib3.connection.HTTPConnection object at 0x7f6c74909f10>: Failed to resolve 
'backend' ([Errno -2] Name or service not known)"))

相关代码

前端路由代码

@app.route('/metainfo/<file_number>')
def getMetaPageInfo(file_number): #pragma: no cover
    response = requests.get('http://' + BACK_END_IP + ':' + BACK_END_PORT + '/metainfo/' 
    + file_number)
    data = None
    if response.status_code == 200:
            data = json.loads(response.content.decode('utf-8'))
    else:
            data = {'message': 'Impossible de contacter l\'api'}

    return page_template('Méta-Information', data)

后端地址配置代码

app = Flask('memoryanalysisfe')
TEMPLATE_FOLDER = app.root_path + '/project/app/templates/'
BACK_END_IP = 'backend'
BACK_END_PORT = '25555'

Docker相关配置

前端Dockerfile

FROM demers/python420w4a
COPY ./requirements.txt /
RUN pip3 install -r /requirements.txt
RUN apt-get update -y
RUN apt-get install -y xxd
WORKDIR /var/lib
CMD ["./run.sh", "./project/app/memoryfront.py", "25556"]

后端Dockerfile

FROM demers/python420w4a
COPY ./requirements.txt /
RUN pip3 install -r /requirements.txt
RUN apt-get update -y
RUN apt-get install -y xxd
WORKDIR /var/lib
CMD ["./run.sh", "./project/main/memoryapi.py", "25555"]

启动脚本startDocker.sh

#!/bin/bash
sudo aa-remove-unknown
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
vol_name=memory_ad_vol
api_cont_name=memoryapi_ad_cnt
api_image_name=memoryapi_ad_img
website_cont_name=memorywebsite_ad_cnt
website_image_name=memorywebsite_ad_img
echo "Début du lancement de l'api et du Site Web dans Docker"
echo "Le nom pour l'image de l'api: $api_image_name"
echo "Le nom pour l'image du Site Web: $website_image_name"
echo "Le nom du volume où vont ce trouver les images et conteneurs: $vol_name"
echo "Le nom pour le conteneur de l'api: $api_cont_name"
echo "Le nom pour le conteneur du Site Web: $website_cont_name"
###CLEANUP
echo "Stoping everything"
docker-compose -f "$SCRIPT_DIR/docker-compose.yml" down
###REBUILD
echo "Creating everything"
docker-compose -f "$SCRIPT_DIR/docker-compose.yml" up -d

docker-compose.yml文件

version: '3.8'
services:
  memory_Backend:
    build:
      context: ./
      dockerfile: ./project/docker/dockerfilebackend
    image: memoryapi_ad_img
    volumes:
      - ./:/var/lib
    ports:
      - 25555:25555
  memory_Frontend:
    build:
      context: ./
      dockerfile: ./project/docker/dockerfilefrontend
    image: memorywebsite_ad_img
    volumes:
      - ./:/var/lib
    ports:
      - 25556:25556

问题排查与解决

核心问题分析

  1. 域名解析失败:前端配置的BACK_END_IP = 'backend'和docker-compose中定义的后端服务名memory_Backend不匹配,Docker DNS无法识别backend这个域名。
  2. 公网IP连接拒绝:后端服务可能仅绑定了127.0.0.1,导致容器外部(包括前端容器通过公网IP访问)无法连接;或者GCP虚拟机防火墙未开放25555端口。

具体解决步骤

  1. 修正后端地址配置
    修改前端的后端地址为docker-compose中的服务名memory_Backend:

    BACK_END_IP = 'memory_Backend'
    BACK_END_PORT = '25555'
    

    Docker Compose会自动为同一网络内的服务创建DNS解析,使用服务名即可直接访问后端容器。

  2. 确保后端服务监听0.0.0.0
    检查后端Flask应用的启动代码,确保绑定到0.0.0.0(允许所有IP访问),而不是127.0.0.1(仅本地访问):

    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=25555)
    
  3. 重启容器生效
    修改配置后,重新构建并启动容器:

    docker-compose down && docker-compose up -d --build
    
  4. 可选:检查GCP防火墙规则
    如果需要从外部直接访问后端API,确保GCP虚拟机的防火墙规则允许25555端口的入站流量,目标范围设置为需要访问的IP或0.0.0.0/0(生产环境不建议)。

验证方法

进入前端容器,测试后端连接:

# 进入前端容器
docker exec -it memorywebsite_ad_cnt bash
# 测试DNS解析
ping memory_Backend
# 测试API访问
curl http://memory_Backend:25555/metainfo/1

如果能正常返回数据,说明配置已修复。

内容的提问来源于stack exchange,提问作者LADsy8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 22:07:52