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

Docker+Django集成ptvsd调试失败:连接被拒绝(ECONNREFUSED)

Debugging Django on GCE with ptvsd: Fixing ECONNREFUSED Error

Hey there! Let's break down why you're hitting that Failed to attach (connect ECONNREFUSED 127.0.0.1:5050) error and walk through the fixes step by step.

1. The ptvsd Debugger Isn't Actually Running (Most Likely Root Cause)

Look at your docker-compose.yml command: you're using gunicorn to launch your Django app via wsgi.py, but your ptvsd.enable_attach() call lives in manage.py. Gunicorn never executes manage.py—so the debugger never gets enabled, and nothing is listening on port 5050. That's the core reason your connection is being refused.

Fix Option 1: Use Django's runserver for Debugging (Simplest for Dev)

Swap out the gunicorn command in your docker-compose.yml with Django's built-in development server, which runs manage.py directly:

web:
    build: ./djangoserver
    # Replace the gunicorn command with this for debugging
    command: python manage.py runserver 0.0.0.0:8001
    volumes:
        - ./djangoserver:/usr/src
    ports:
        - 5050:5050
        - 8001:8001  # Optional, to access the app directly in your browser
    expose:
        - 8001
    env_file: .env.dev
    depends_on:
        - db_development_2
    stdin_open: true

Now when your container starts, manage.py runs, and ptvsd will enable the debug listener on port 5050 as expected.

Fix Option 2: Add ptvsd to wsgi.py (If You Need Gunicorn)

If you must use gunicorn for your debug session, move the ptvsd setup into your djangoserver/wsgi.py file (since that's what gunicorn executes):

import os
import ptvsd

# Enable ptvsd BEFORE initializing Django
ptvsd.enable_attach(address=('0.0.0.0', 5050))

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoserver.settings')

application = get_wsgi_application()

Critical note: Gunicorn's --noreload flag breaks ptvsd attachment—remove it from your command. Also, --nothreading can interfere with debugging, so omit that too:

command: gunicorn djangoserver.wsgi:application --bind 0.0.0.0:8001

2. Open Port 5050 in GCE's Firewall

Google Compute Engine blocks incoming traffic to most ports by default. You need to create a firewall rule to allow debug traffic to port 5050:

  • Head to the GCE Console's Firewall Rules section
  • Click "Create Firewall Rule"
  • Configure these settings:
    • Name: allow-ptvsd-debug
    • Target tags: Apply this rule to the VM running your Docker containers (or use "all instances in network" for testing)
    • Source IP ranges: Use 0.0.0.0/0 for testing (restrict this to your local public IP later for security, e.g., 192.168.1.100/32)
    • Protocols and ports: Check "tcp" and enter 5050
  • Save the rule.

3. Update VS Code's launch.json for Remote Connection

Your current launch.json uses "host": "localhost", but that points to your local machine—not the GCE VM. You have two options here:

Option A: Point Directly to the VM's Public IP

Update the host field to your GCE VM's public IP address:

{
    "name": "Attach Remote Django",
    "type": "python",
    "request": "attach",
    "pathMappings": [
        {"localRoot": "${workspaceRoot}/djangoserver", "remoteRoot": "/usr/src/"}
    ],
    "port": 5050,
    "secret": "secret",
    "host": "your-gce-vm-public-ip"  // Replace with your VM's actual public IP
}

Option B: Use an SSH Tunnel (More Secure)

Instead of opening the port to the internet, create an SSH tunnel to forward your local port 5050 to the VM's port 5050. Run this command on your local terminal:

gcloud compute ssh your-vm-name -- -L 5050:localhost:5050

With the tunnel active, you can keep "host": "localhost" in launch.json—the tunnel routes your local debug traffic to the VM's container.


Final Validation Steps

After making these changes:

  1. Rebuild and restart your containers:
    docker-compose down && docker-compose up --build
    
  2. On your GCE VM, verify the container is listening on port 5050:
    docker-compose exec web netstat -tulpn | grep 5050
    
    You should see output showing ptvsd listening on 0.0.0.0:5050.

内容的提问来源于stack exchange,提问作者Artur Drożdżyk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:18:33