Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To debug Python in Docker, run the application with debugpy inside the container, publish its listening port, and attach your IDE to that port. The IDE also needs a path mapping between your local project and the directory containing the code in the container; without it, breakpoints may not match the files Python is executing.

How remote debugging in Docker works

The Python process runs in or alongside the container, while the IDE connects to its debugger over a network port. In the examples below, the container listens on port 5678, the conventional default in VS Code’s Python Remote Attach example. The IDE connects to the host’s published port, and a path mapping relates local source files to their container paths.

Docker’s Python guide demonstrates defining a Python application with a Dockerfile and Compose configuration. For debugging, you can add a separate Compose configuration or override so the normal service stays distinct from its debug settings.

Set up a Python container for debugging

These illustrative files assume your application can be started as the myapp Python module, uses requirements.txt, and listens for application traffic on port 8000. Adapt the module, dependency installation, application port, and paths to your project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build the image

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:5678", "--wait-for-client", "-m", "myapp"]

This Dockerfile includes the source and dependencies in the image and starts Python through debugpy. The debug port must listen on 0.0.0.0 inside the container so it can accept a connection forwarded from the host. The --wait-for-client option holds application startup until the IDE attaches.

Configure a debug Compose service

Save a debug-specific file such as docker-compose.debug.yml alongside your normal Compose configuration:

services:
  app:
    build: .
    ports:
      - "8000:8000"
      - "5678:5678"
    volumes:
      - .:/app
    command: ["python", "-m", "debugpy", "--wait-for-client", "--listen", "0.0.0.0:5678", "-m", "myapp"]

The source volume makes the current local files available at /app in the container. Publishing 5678 lets the IDE connect to the debugger; publishing 8000 makes the illustrative application port available on the host. Replace these ports and the command to match your app. VS Code’s Python debugging documentation shows the same remote-attach pattern with a Django entry point.

Attach VS Code and set a breakpoint

  1. Start the debug service. From the project directory, run docker compose -f docker-compose.debug.yml up --build. If your debug file is an override for a base Compose file, include both with -f, listing the base file first.
  2. Create an attach configuration. In VS Code, open the Run and Debug view, create or edit .vscode/launch.json, and add a Python Debugger: Remote Attach configuration. The key settings are the host, port, and source mapping:
{
  "name": "Python Debugger: Remote Attach",
  "type": "debugpy",
  "request": "attach",
  "connect": {"host": "localhost", "port": 5678},
  "pathMappings": [
    {"localRoot": "${workspaceFolder}", "remoteRoot": "/app"}
  ]
}

Use the schema generated by the Python Debugger extension if its current configuration property names differ. Here, localRoot is the project opened in VS Code, and remoteRoot is where the source appears in the container.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Attach and test. Choose the attach configuration and press F5. Set a breakpoint in a line reached after the application starts, then trigger that code. When execution pauses, inspect variables, step over or into statements, and continue as needed.

VS Code’s container documentation also describes tooling that can generate Docker tasks and launch configurations for Python projects.

Fix common Docker breakpoint problems

  • The IDE cannot connect or waits indefinitely: check that the container is running, that port 5678 is published, and that debugpy listens on 0.0.0.0 rather than only 127.0.0.1. If the app uses --wait-for-client, it will not proceed until a client attaches.
  • A breakpoint is hollow or never triggers: compare the actual container source directory with remoteRoot, and the opened local project with localRoot. The paths must refer to corresponding files. VS Code documents path mappings in its remote debugging guidance; PyCharm’s Docker Compose interpreter guidance also explains mapping project files into a container.
  • The container runs old code: confirm the process executes the files you are editing. Rebuild the image when source is copied into it, or mount the current source tree, as in the Compose example. Check the entry point and working directory as well as the mapping.
  • A web framework’s reloader produces confusing sessions: a reloader may start a child process that handles requests while the debugger is attached to its parent. Disable the reloader for the debug run or attach to the worker process that executes the code.
  • More than one service needs debugging: publish a distinct host port for each debug target and create a separate IDE attach configuration for each service.
  • The container exits immediately: inspect its logs and confirm the foreground application command is valid. Docker’s Compose quickstart covers viewing logs and running commands in a live container; these checks can reveal startup failures before you change debugger settings.

Use PyCharm with Docker Compose

PyCharm offers two relevant approaches. You can configure Docker or Docker Compose as a remote interpreter, then launch a debug run using that interpreter; this is useful when the IDE should manage running the application in the container. JetBrains documents the Compose setup in Using Docker Compose as a remote interpreter.

Alternatively, attach PyCharm to a remote target exposing a Debug Adapter Protocol (DAP) server such as debugpy. Its debugging documentation describes the standard pause, step, variable-inspection, and resume workflow. Follow the instructions for the PyCharm version and debugger setup you use; connection details and available options can depend on that setup.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose an IDE for your project

Consideration VS Code PyCharm
Setup approach Remote Attach configuration connects to the running debugpy process; VS Code container tooling can help generate Docker tasks and launch configurations. Can use Docker or Compose as a remote interpreter and launch a debug run, or attach to a DAP server such as debugpy.
Existing Compose project Publish the debug port in a debug Compose configuration, then attach to the host port. Compose can be configured as the remote interpreter for a multi-service project.
Source paths Specify localRoot and remoteRoot in the attach configuration. Configure mappings for project files in the container as part of the interpreter setup.
Multiple services Use distinct published host ports and attach configurations for separate debug targets. Compose interpreter configuration can support a multi-service project; the exact attach workflow depends on how the target is run.
Team fit Convenient if the team already standardizes on VS Code and its Python debugger. Convenient if the team already standardizes on PyCharm and its Docker interpreter workflow.

Both IDEs support breakpoint-driven inspection. For a team, the practical choice is usually the IDE already in use and the workflow members can configure consistently.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.