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

如何使用PyCharm调试Docker-Compose环境中运行的进程

Great to hear your Docker Compose setup is running smoothly! Debugging a Django app inside a container with PyCharm on Mac is totally straightforward—here's how I usually configure it:

1. Prepare Your Docker Compose Configuration

First, make sure your Django container exposes a debug port and has the right environment settings to allow remote debugging. Update your docker-compose.yml:

services:
  django:
    build: ./django
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
      - "5678:5678"  # Map debug port to your host
    environment:
      - DEBUG=True
      - ALLOWED_HOSTS=*  # Or restrict to your Mac's IP for production, but * works for debugging
      - DEBUG_MODE=True  # Custom env var to toggle debugging
    volumes:
      - ./django:/app  # Your existing code mount (already working for you)
2. Install the Debug Package in Your Django Container

Add the PyCharm debug client to your requirements.txt (match the version to your PyCharm release—e.g., for 2023.2, use this):

pydevd-pycharm~=2023.2

Rebuild your Django image or restart the container to install it:

docker-compose up --build django
3. Configure PyCharm's Remote Debug Server
  1. Open PyCharm and go to Run > Edit Configurations
  2. Click the + button and select Python Debug Server
  3. Name your config (e.g., "Docker Django Debug")
  4. Set Host to host.docker.internal (Mac Docker uses this to reach your host machine) and Port to 5678 (matches the port in docker-compose)
  5. Copy the auto-generated debug snippet—you’ll need this for the next step.
4. Inject Debug Code Into Your Django App

Instead of hardcoding the debug setup, use the DEBUG_MODE env var to toggle it. Add this to your project’s settings.py:

import os
if os.environ.get("DEBUG_MODE") == "True":
    import pydevd_pycharm
    pydevd_pycharm.settrace(
        "host.docker.internal",
        port=5678,
        stdoutToServer=True,
        stderrToServer=True,
        suspend=False  # Set to True if you want the app to pause on startup until debugger connects
    )
5. Start Debugging
  1. In PyCharm, launch your new debug server configuration (click the green bug icon next to your config name)
  2. Start your Docker Compose stack: docker-compose up
  3. Set breakpoints in your code (click the gutter next to any line in a view, model, or utility file)
  4. Access your Django app via localhost:8000—when the code hits your breakpoint, PyCharm will take over, letting you inspect variables, step through execution, and debug like you would with a local app.
Bonus: Use PyCharm's Built-In Docker Compose Debug

For a more integrated experience, skip the manual debug server setup:

  1. Go to Run > Edit Configurations and add a Docker Compose config
  2. Select your docker-compose.yml file and choose the django service
  3. Switch to the Debug tab and check "Enable debug"
  4. Select your pre-configured Docker Python interpreter
  5. Launch this config—PyCharm handles port mapping, debug client setup, and breakpoint detection automatically.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:03:38